Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
import os
|
|
|
4 |
|
5 |
|
6 |
client = OpenAI(
|
@@ -9,11 +10,11 @@ client = OpenAI(
|
|
9 |
)
|
10 |
|
11 |
def generate_response(message, history):
|
|
|
12 |
if history:
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
history_openai_format.append({"role": "user", "content": message})
|
18 |
|
19 |
completion = client.chat.completions.create(
|
@@ -31,7 +32,8 @@ def generate_response(message, history):
|
|
31 |
response += chunk.choices[0].delta.content
|
32 |
|
33 |
history_openai_format.append({"role": "assistant", "content": response})
|
34 |
-
|
|
|
35 |
|
36 |
iface = gr.ChatInterface(
|
37 |
generate_response,
|
@@ -43,7 +45,7 @@ iface = gr.ChatInterface(
|
|
43 |
"Kuantum bilgisayarların geleceği hakkında ne düşünüyorsun?"
|
44 |
],
|
45 |
cache_examples=False,
|
46 |
-
type='
|
47 |
)
|
48 |
|
49 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
+
from gradio.components import ChatMessage
|
5 |
|
6 |
|
7 |
client = OpenAI(
|
|
|
10 |
)
|
11 |
|
12 |
def generate_response(message, history):
|
13 |
+
history_openai_format = []
|
14 |
if history:
|
15 |
+
for chat_message in history:
|
16 |
+
history_openai_format.append(chat_message.dict())
|
17 |
+
|
|
|
18 |
history_openai_format.append({"role": "user", "content": message})
|
19 |
|
20 |
completion = client.chat.completions.create(
|
|
|
32 |
response += chunk.choices[0].delta.content
|
33 |
|
34 |
history_openai_format.append({"role": "assistant", "content": response})
|
35 |
+
chat_history = [ChatMessage(role=msg["role"], content=msg["content"]) for msg in history_openai_format]
|
36 |
+
return response, chat_history
|
37 |
|
38 |
iface = gr.ChatInterface(
|
39 |
generate_response,
|
|
|
45 |
"Kuantum bilgisayarların geleceği hakkında ne düşünüyorsun?"
|
46 |
],
|
47 |
cache_examples=False,
|
48 |
+
type='chat'
|
49 |
)
|
50 |
|
51 |
iface.launch()
|