Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,28 @@ import os
|
|
| 5 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 6 |
|
| 7 |
def chat(user_input, history=[]):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
for h in history:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
messages.append({"role": "user", "content": user_input})
|
| 16 |
|
| 17 |
-
response = client.chat.completions.create(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
reply = response.choices[0].message.content
|
| 23 |
-
history.append((user_input, reply))
|
| 24 |
|
| 25 |
-
return history, history
|
| 26 |
|
| 27 |
-
except Exception as e:
|
| 28 |
-
return str(e), history
|
| 29 |
|
| 30 |
|
| 31 |
iface = gr.ChatInterface(fn=chat)
|
| 32 |
-
iface.launch()
|
|
|
|
| 5 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 6 |
|
| 7 |
def chat(user_input, history=[]):
|
| 8 |
+
try:
|
| 9 |
+
messages = [{"role": "system", "content": "You are Jarvis, a smart AI assistant."}]
|
| 10 |
|
| 11 |
+
for h in history:
|
| 12 |
+
messages.append({"role": "user", "content": h[0]})
|
| 13 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 14 |
|
| 15 |
+
messages.append({"role": "user", "content": user_input})
|
| 16 |
|
| 17 |
+
response = client.chat.completions.create(
|
| 18 |
+
model="gpt-4o-mini",
|
| 19 |
+
messages=messages
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
reply = response.choices[0].message.content
|
| 23 |
+
history.append((user_input, reply))
|
| 24 |
|
| 25 |
+
return history, history
|
| 26 |
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return str(e), history
|
| 29 |
|
| 30 |
|
| 31 |
iface = gr.ChatInterface(fn=chat)
|
| 32 |
+
iface.launch()
|