-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (32 loc) · 1.1 KB
/
main.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
# APIKEY
import openai
import os
from colorama import Fore, Style
# get api key
path = os.path.join(os.path.dirname(__file__), 'api_key.txt')
API_KEY=open(path, "r").read()
openai.api_key = API_KEY
messages = []
print(Fore.BLUE + Style.BRIGHT + "\n-----------Welcome to GPT-----------")
print(Style.RESET_ALL + "Type 'exit' to exit")
while True:
text = input(f'\n{Fore.BLUE + Style.BRIGHT + "[USER]"}:{Fore.GREEN + Style.BRIGHT + " "}')
if text == 'exit':
break
# making message
dic = {"role": "user", "content": text}
messages.append(dic)
# getting response
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages,
)
# getting usage
usage = response['usage']['total_tokens']
# appending to context of conversation
response_dic = response['choices'][-1]['message']
messages.append(response_dic)
# getting response text
response_text = response_dic['content']
print(f'{Fore.RED + Style.BRIGHT + "[GPT]"}: {Fore.WHITE + Style.BRIGHT + response_text}')
print(Style.RESET_ALL + f"usage: {usage}")