-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
41 lines (35 loc) · 1.52 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Add your utilities or helper functions to this file.
import os
from dotenv import load_dotenv, find_dotenv
# these expect to find a .env file at the directory above the lesson. # the format for that file is (without the comment) #API_KEYNAME=AStringThatIsTheLongAPIKeyFromSomeService
def load_env():
_ = load_dotenv(find_dotenv())
def get_openai_api_key():
load_env()
openai_api_key = os.getenv("OPENAI_API_KEY")
return openai_api_key
def get_serper_api_key():
load_env()
openai_api_key = os.getenv("SERPER_API_KEY")
return openai_api_key
# break line every 80 characters if line is longer than 80 characters
# don't break in the middle of a word
def pretty_print_result(result):
parsed_result = []
for line in result.split('\n'):
if len(line) > 80:
words = line.split(' ')
new_line = ''
for word in words:
if len(new_line) + len(word) + 1 > 80:
parsed_result.append(new_line)
new_line = word
else:
if new_line == '':
new_line = word
else:
new_line += ' ' + word
parsed_result.append(new_line)
else:
parsed_result.append(line)
return "\n".join(parsed_result)