-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbo.py
executable file
·52 lines (40 loc) · 1.35 KB
/
turbo.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
42
43
44
45
46
47
48
49
50
51
52
import sys
from dotenv import load_dotenv
import openai
import os
import emoji
load_dotenv()
openai.api_key = os.getenv('OPENAI_KEY')
# read the contents of the buffer from standard input
buffer = sys.stdin.read()
def assistant(buffer):
# program needs to have prevention from a timeout to openAI
retry_count = 0
max_retries = 9999
while retry_count <= max_retries:
try:
# call openai api
response = openai.ChatCompletion.create(
# model type
model="gpt-3.5-turbo-16k",
messages=[
{"role": "user", "content": buffer},
],
temperature=1,
max_tokens=15999,
)
response_dict = response.get("choices")
if response_dict and len(response_dict) > 0:
prompt_response = response_dict[0]["message"]["content"]
return prompt_response
except openai.error.InvalidRequestError as e:
print(f"API request [InvalidRequestError] failed with error: {e}")
smiley = emoji.emojize(":smiling_face_with_smiling_eyes:")
return assistant(prompt=smiley)
except Exception:
retry_count += 1
output = assistant(buffer)
print('\n\n## Question\n\n')
print(f"{buffer}")
print('\n\n## Answer\n\n')
print(f"{output}")