Create utils/helper.py
Browse files- utils/helper.py +23 -0
utils/helper.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
|
3 |
+
|
4 |
+
class ChatBot:
|
5 |
+
def __init__(self):
|
6 |
+
self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
7 |
+
self.history = [{"role": "system", "content": "You are a helpful assistant."}]
|
8 |
+
|
9 |
+
def generate_response(self, prompt: str) -> str:
|
10 |
+
self.history.append({"role": "user", "content": prompt})
|
11 |
+
|
12 |
+
completion = self.client.chat.completions.create(
|
13 |
+
model="gpt-3.5-turbo", # NOTE: feel free to change it to gpt-4, or gpt-4o
|
14 |
+
messages=self.history
|
15 |
+
)
|
16 |
+
|
17 |
+
response = completion.choices[0].message.content
|
18 |
+
self.history.append({"role": "assistant", "content": response})
|
19 |
+
|
20 |
+
return response
|
21 |
+
|
22 |
+
def get_history(self) -> list:
|
23 |
+
return self.history
|