-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (39 loc) · 1.25 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
41
42
43
44
45
46
47
48
import os
from openai import OpenAI, Stream
from openai.types.chat import ChatCompletionChunk
class ChatBot:
def __init__(self, model: str = "gpt-4"):
self.messages = []
self.openai_client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
self.model = model
def chat(self, message: str) -> Stream[ChatCompletionChunk] | str:
try:
self.messages.append({'role': 'user', 'content': message})
stream = self.openai_client.chat.completions.create(
model=self.model,
messages=self.messages,
stream=True,
)
return stream
except Exception as e:
str(e)
def main():
print("Welcome to ChatGPT! Type 'bye' to exit.\n")
chatbot = ChatBot()
while True:
# Get user input
user_input = input("You:\n")
print()
if user_input.lower() == 'bye':
break
# Get response from OpenAI
print("ChatGPT:")
stream = chatbot.chat(user_input)
if type(stream) is str:
print(stream)
continue
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
print("\n")
if __name__ == "__main__":
main()