Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,24 +9,20 @@ class chatgpt:
|
|
| 9 |
|
| 10 |
# Getting responses using the OpenAI API
|
| 11 |
def answer_chatgpt(self, api_key, message, history):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return self.history, self.history
|
| 27 |
-
except* TypeError as error:
|
| 28 |
-
gr.Error(f"{error.exceptions}")
|
| 29 |
-
|
| 30 |
|
| 31 |
def Clean(self):
|
| 32 |
self.history.clear()
|
|
|
|
| 9 |
|
| 10 |
# Getting responses using the OpenAI API
|
| 11 |
def answer_chatgpt(self, api_key, message, history):
|
| 12 |
+
self.history = history or []
|
| 13 |
+
prompt = f"{message}"
|
| 14 |
+
self.conversation.append({"role": "user", "content": f"{prompt}"})
|
| 15 |
+
# OPENAI API KEY
|
| 16 |
+
openai.api_key = api_key
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model=self.model_id,
|
| 19 |
+
messages=self.conversation
|
| 20 |
+
)
|
| 21 |
+
# Displaying the answer on the screen
|
| 22 |
+
answer = response["choices"][0]["message"]["content"]
|
| 23 |
+
self.history.append((message, answer))
|
| 24 |
+
self.conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})
|
| 25 |
+
return self.history, self.history
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def Clean(self):
|
| 28 |
self.history.clear()
|