Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from openai import OpenAI
|
2 |
import gradio as gr
|
3 |
import os
|
@@ -7,10 +8,13 @@ api_key = os.getenv("OPENAI_API_KEY")
|
|
7 |
client = OpenAI(api_key=api_key)
|
8 |
|
9 |
def chat_with_openai(user_input, history):
|
10 |
-
if history is None
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
# ユーザーの入力を履歴に追加
|
15 |
history.append({"role": "user", "content": user_input})
|
16 |
|
@@ -21,17 +25,22 @@ def chat_with_openai(user_input, history):
|
|
21 |
)
|
22 |
|
23 |
# アシスタントの応答を取得
|
24 |
-
assistant_message = completion.choices[0].message.content
|
25 |
|
26 |
# 履歴にアシスタントの応答を追加
|
27 |
history.append({"role": "assistant", "content": assistant_message})
|
28 |
|
29 |
-
#
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
-
|
34 |
-
chatbot = gr.Chatbot(type='messages')
|
35 |
state = gr.State([])
|
36 |
|
37 |
with gr.Row():
|
|
|
1 |
+
|
2 |
from openai import OpenAI
|
3 |
import gradio as gr
|
4 |
import os
|
|
|
8 |
client = OpenAI(api_key=api_key)
|
9 |
|
10 |
def chat_with_openai(user_input, history):
|
11 |
+
if history is None:
|
12 |
+
history = []
|
13 |
+
|
14 |
+
# 最初のメッセージの場合、システムメッセージの代わりにユーザーメッセージとして追加
|
15 |
+
if len(history) == 0:
|
16 |
+
history.append({"role": "user", "content": "You are a helpful assistant."})
|
17 |
+
|
18 |
# ユーザーの入力を履歴に追加
|
19 |
history.append({"role": "user", "content": user_input})
|
20 |
|
|
|
25 |
)
|
26 |
|
27 |
# アシスタントの応答を取得
|
28 |
+
assistant_message = completion.choices[0].message.content
|
29 |
|
30 |
# 履歴にアシスタントの応答を追加
|
31 |
history.append({"role": "assistant", "content": assistant_message})
|
32 |
|
33 |
+
# チャットボットに表示するメッセージを整形
|
34 |
+
messages_to_display = []
|
35 |
+
for i in range(1, len(history)-1, 2):
|
36 |
+
user_msg = history[i]["content"]
|
37 |
+
assistant_msg = history[i+1]["content"]
|
38 |
+
messages_to_display.append((user_msg, assistant_msg))
|
39 |
+
|
40 |
+
return messages_to_display, history
|
41 |
|
42 |
with gr.Blocks() as demo:
|
43 |
+
chatbot = gr.Chatbot()
|
|
|
44 |
state = gr.State([])
|
45 |
|
46 |
with gr.Row():
|