-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
51 lines (41 loc) · 1.39 KB
/
translator.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
import os
import sys
import openai
import textwrap
openai.api_key = os.getenv('OPENAI_API_KEY')
MODEL = "gpt-3.5-turbo"
messages = [{"role": "system", "content": "You are a helpful assistant and a good translator."}]
def chat_completion():
while True:
prompt = input('\nYou: ')
messages.append(
{"role": "user", "content": prompt }
)
completion = openai.ChatCompletion.create(
model = MODEL,
max_tokens = 250,
messages = messages
)
response = completion['choices'][0]['message']['content']
response = textwrap.fill(response, width=50)
messages.append(
{"role": "assistant", "content": "response" }
)
if prompt in ["bye","exit","goodbye","end"]:
print('\nChatGPT: Goodbye. See you next time!')
sys.exit()
else:
print(f'\nChatGPT:',response)
continue
def main():
os.system('cls' if os.name == 'nt' else 'clear')
app_name = "ChatAI (Translator)\n\nTo END session, type: 'bye, goodbye, end or exit"
print(f'{"-" * 48}')
print(f'{" " * 12}{app_name}{" " * 12}')
print(f'{"-" * 48}')
if not openai.api_key:
print("ERROR: No OpenAI API key found. Please set the OPENAI_API_KEY environment variable.")
sys.exit()
chat_completion()
if __name__ == '__main__':
main()