shigeru saito commited on
Commit
c9a66e4
1 Parent(s): 07a5f9a

すべてのメッセージを出力するように修正。

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -13,12 +13,23 @@ assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
13
 
14
  import json
15
 
16
- def assistant_response(prompt):
17
- client = openai.OpenAI()
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- print("### Step 1: Get the Assistant's ID ###")
20
- assistant = client.beta.assistants.retrieve(assistant_id)
21
- print(assistant)
22
 
23
  "### Step 2: Create a Thread ###"
24
  empty_thread = client.beta.threads.create()
@@ -64,7 +75,7 @@ def assistant_response(prompt):
64
  run = wait_on_run(run, thread)
65
  print(run)
66
 
67
- print("### Step 8: Retrieve the Assistant's Response ###")
68
  messages = client.beta.threads.messages.list(
69
  thread_id=thread.id
70
  )
@@ -72,15 +83,33 @@ def assistant_response(prompt):
72
  messages_str = json.dumps(messages.dict(), indent=2)
73
  print(codecs.decode(messages_str, 'unicode-escape'))
74
 
75
- answer = messages.data[0].content[0].text.value
76
- print(answer)
77
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # Gradio インターフェースの設定
80
  iface = gr.Interface(
 
 
81
  fn=assistant_response,
82
  inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
83
- outputs="text"
84
  )
85
 
86
  # アプリケーションの起動
 
13
 
14
  import json
15
 
16
+ assistant = None
17
+ client = openai.OpenAI()
18
+
19
+ print("### Step 1: Get the Assistant's ID ###")
20
+ assistant = client.beta.assistants.retrieve(assistant_id)
21
+ print(assistant)
22
+ assistant_name = assistant.name
23
+ assistant_description = assistant.description
24
+ assistant_model = assistant.model
25
+ assistant_tools = assistant.tools
26
+ assistant_file_ids = assistant.file_ids
27
+
28
+ if assistant_description is None:
29
+
30
+ assistant_description += f"このアシスタントは、OpenAI APIで {assistant_model} を使用して作成されました。"
31
 
32
+ def assistant_response(prompt):
 
 
33
 
34
  "### Step 2: Create a Thread ###"
35
  empty_thread = client.beta.threads.create()
 
75
  run = wait_on_run(run, thread)
76
  print(run)
77
 
78
+ print("### Step 8: Retrieve the Messages ###")
79
  messages = client.beta.threads.messages.list(
80
  thread_id=thread.id
81
  )
 
83
  messages_str = json.dumps(messages.dict(), indent=2)
84
  print(codecs.decode(messages_str, 'unicode-escape'))
85
 
86
+ print("### Step 9: Retrieve the Assistant's Response ###")
87
+ answers = []
88
+ for message in messages.data:
89
+ if message.role == "assistant":
90
+ if message.content[0].type == "text":
91
+ answers.append(message.content[0].text.value + "\n\n")
92
+ else:
93
+ answers.append("Content is not text.\n\n")
94
+ elif message.role == "user":
95
+ break
96
+
97
+ # answersを逆順にする
98
+ answers.reverse()
99
+
100
+ return "".join(answers)
101
+
102
+
103
+ title = "OpenAPI Assistant API: " + assistant_name
104
+ description = assistant_description
105
 
106
  # Gradio インターフェースの設定
107
  iface = gr.Interface(
108
+ title=title,
109
+ description=description,
110
  fn=assistant_response,
111
  inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
112
+ outputs=gr.Textbox(),
113
  )
114
 
115
  # アプリケーションの起動