Spaces:
Runtime error
Runtime error
cool-radio
commited on
Commit
•
b959451
1
Parent(s):
71b563a
Update app.py
Browse filesopenai.api_key
app.py
CHANGED
@@ -5,16 +5,22 @@ import os
|
|
5 |
# Setup and initialization
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
iface = gr.Interface(
|
20 |
fn=chatbot_response,
|
|
|
5 |
# Setup and initialization
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
+
from openai import OpenAI
|
9 |
+
|
10 |
+
client = OpenAI(openai.api_key=os.getenv("OPENAI_API_KEY"))
|
11 |
+
|
12 |
+
def openai_chat(prompt, chat_history):
|
13 |
+
"""Generic function to handle chatting with OpenAI's GPT model."""
|
14 |
+
try:
|
15 |
+
response = client.engines.gpt_3_5_turbo.completions.create(
|
16 |
+
prompt=prompt,
|
17 |
+
max_tokens=150
|
18 |
+
)
|
19 |
+
bot_message = response.choices[0].text.strip()
|
20 |
+
chat_history.append({"role": "assistant", "content": bot_message})
|
21 |
+
return '', chat_history
|
22 |
+
except Exception as e:
|
23 |
+
return f"An error occurred: {str(e)}", chat_history
|
24 |
|
25 |
iface = gr.Interface(
|
26 |
fn=chatbot_response,
|