Spaces:
Runtime error
Runtime error
shigeru saito
commited on
Commit
•
0e519fe
1
Parent(s):
c9a66e4
チャット形式に変更
Browse files- app.py +42 -107
- llm/mock.py +50 -0
- llm/openai.py +124 -0
app.py
CHANGED
@@ -1,116 +1,51 @@
|
|
1 |
-
import codecs
|
2 |
-
import json
|
3 |
-
import time
|
4 |
-
import openai
|
5 |
import gradio as gr
|
6 |
-
import
|
7 |
-
from dotenv import load_dotenv
|
8 |
|
9 |
-
|
10 |
-
load_dotenv()
|
11 |
-
openai.api_key = os.getenv('OPENAI_API_KEY')
|
12 |
-
assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
|
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 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
)
|
64 |
-
|
65 |
-
print("### Step 7: Wait for the Assistant to Finish ###")
|
66 |
-
def wait_on_run(run, thread):
|
67 |
-
while run.status == "queued" or run.status == "in_progress":
|
68 |
-
run = client.beta.threads.runs.retrieve(
|
69 |
-
thread_id=thread.id,
|
70 |
-
run_id=run.id,
|
71 |
-
)
|
72 |
-
time.sleep(0.5)
|
73 |
-
return run
|
74 |
-
|
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 |
-
)
|
82 |
-
|
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 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
description = assistant_description
|
105 |
|
106 |
-
|
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 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
|
|
3 |
|
4 |
+
from llm.openai import Llm
|
|
|
|
|
|
|
5 |
|
6 |
+
# # mock for testing
|
7 |
+
# from llm.mock import Llm
|
8 |
|
9 |
+
llm = Llm()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def assistant_response(prompt):
|
12 |
+
answer = llm.chatcompletion(prompt)
|
13 |
+
return answer
|
14 |
+
|
15 |
+
def respond(message, chat_history):
|
16 |
+
answer = llm.chatcompletion(message)
|
17 |
+
print(answer)
|
18 |
+
chat_history.append((message, answer))
|
19 |
+
return "", chat_history
|
20 |
+
|
21 |
+
title = "OpenAPI Assistant API: " + llm.assistant.name
|
22 |
+
|
23 |
+
if llm.assistant.description is None:
|
24 |
+
model = llm.assistant.model
|
25 |
+
description = f"このデモはOpenAPI Assistant APIのデモです。テキストボックスにテキストを入力すると、{model}モデルが応答します。"
|
26 |
+
else:
|
27 |
+
description = llm.assistant.description
|
28 |
+
|
29 |
+
# 実行例のリスト (現在使用してない)
|
30 |
+
import csv
|
31 |
+
examples = []
|
32 |
+
with open('flagged/log.csv', 'r', encoding='utf-8') as file:
|
33 |
+
reader = csv.DictReader(file)
|
34 |
+
examples = [row['prompt'] for row in reader]
|
35 |
+
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown(
|
38 |
+
f"""
|
39 |
+
# {title}
|
40 |
+
{description}
|
41 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
chatbot = gr.Chatbot()
|
44 |
+
msg = gr.Textbox()
|
45 |
+
clear = gr.ClearButton([msg, chatbot])
|
46 |
+
examples=examples
|
|
|
47 |
|
48 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|
llm/mock.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import codecs
|
2 |
+
import json
|
3 |
+
import time
|
4 |
+
import openai
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
class Llm:
|
11 |
+
def __init__(self):
|
12 |
+
print("### Step 1: Get the Assistant's ID ###")
|
13 |
+
|
14 |
+
# OpenAI API キーの設定
|
15 |
+
self.client = openai.OpenAI()
|
16 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
17 |
+
self.assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
18 |
+
|
19 |
+
self.assistant = self.client.beta.assistants.retrieve(self.assistant_id)
|
20 |
+
print(self.assistant)
|
21 |
+
assistant_description = self.assistant.description
|
22 |
+
self.assistant.model = "mock"
|
23 |
+
assistant_model = self.assistant.model
|
24 |
+
|
25 |
+
if assistant_description is None:
|
26 |
+
|
27 |
+
assistant_description = f"このアシスタントは、OpenAI APIで {assistant_model} を使用して作成されました。"
|
28 |
+
|
29 |
+
def setup(self):
|
30 |
+
load_dotenv()
|
31 |
+
self.api_key = os.getenv('OPENAI_API_KEY')
|
32 |
+
self.assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
33 |
+
self.client = openai.OpenAI()
|
34 |
+
|
35 |
+
def retrieve_assistant(self):
|
36 |
+
self.assistant = self.client.beta.assistants.retrieve(self.assistant_id)
|
37 |
+
return self.assistant
|
38 |
+
|
39 |
+
def chatcompletion(self, prompt):
|
40 |
+
import random
|
41 |
+
import csv
|
42 |
+
|
43 |
+
with open('flagged/log.csv', 'r') as file:
|
44 |
+
reader = csv.DictReader(file)
|
45 |
+
logs = [row for row in reader]
|
46 |
+
|
47 |
+
random_log = random.choice(logs)
|
48 |
+
answer = random_log['output']
|
49 |
+
|
50 |
+
return answer
|
llm/openai.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import codecs
|
2 |
+
import json
|
3 |
+
import time
|
4 |
+
import openai
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
class Llm:
|
11 |
+
def __init__(self):
|
12 |
+
print("### Step 1: Get the Assistant's ID ###")
|
13 |
+
|
14 |
+
# OpenAI API キーの設定
|
15 |
+
self.client = openai.OpenAI()
|
16 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
17 |
+
self.assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
18 |
+
|
19 |
+
self.assistant = self.client.beta.assistants.retrieve(self.assistant_id)
|
20 |
+
print(self.assistant)
|
21 |
+
assistant_description = self.assistant.description
|
22 |
+
assistant_model = self.assistant.model
|
23 |
+
|
24 |
+
if assistant_description is None:
|
25 |
+
|
26 |
+
assistant_description = f"このアシスタントは、OpenAI APIで {assistant_model} を使用して作成されました。"
|
27 |
+
|
28 |
+
|
29 |
+
def setup(self):
|
30 |
+
load_dotenv()
|
31 |
+
self.api_key = os.getenv('OPENAI_API_KEY')
|
32 |
+
self.assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
33 |
+
self.client = openai.OpenAI()
|
34 |
+
|
35 |
+
def retrieve_assistant(self):
|
36 |
+
self.assistant = self.client.beta.assistants.retrieve(self.assistant_id)
|
37 |
+
return self.assistant
|
38 |
+
|
39 |
+
# def openai_completion(self, prompt):
|
40 |
+
# import random
|
41 |
+
# import csv
|
42 |
+
|
43 |
+
# with open('flagged/log.csv', 'r') as file:
|
44 |
+
# reader = csv.DictReader(file)
|
45 |
+
# logs = [row for row in reader]
|
46 |
+
|
47 |
+
# random_log = random.choice(logs)
|
48 |
+
# answer = random_log['output']
|
49 |
+
|
50 |
+
# return answer
|
51 |
+
|
52 |
+
def chatcompletion(self, prompt):
|
53 |
+
|
54 |
+
try:
|
55 |
+
"### Step 2: Create a Thread ###"
|
56 |
+
empty_thread = self.client.beta.threads.create()
|
57 |
+
thread_id = empty_thread.id
|
58 |
+
print(empty_thread)
|
59 |
+
|
60 |
+
print("### Step 3: Add a Message to the Thread ###")
|
61 |
+
thread = self.client.beta.threads.retrieve(thread_id)
|
62 |
+
print(thread)
|
63 |
+
|
64 |
+
print("### Step 4: Add a Message to the Thread ###")
|
65 |
+
thread_message = self.client.beta.threads.messages.create(
|
66 |
+
thread_id,
|
67 |
+
role="user",
|
68 |
+
content=prompt,
|
69 |
+
)
|
70 |
+
message_id = thread_message.id
|
71 |
+
print(thread_message)
|
72 |
+
|
73 |
+
print("### Step 5: Retrieve the Message ###")
|
74 |
+
message = self.client.beta.threads.messages.retrieve(
|
75 |
+
message_id=message_id,
|
76 |
+
thread_id=thread_id,
|
77 |
+
)
|
78 |
+
print(message)
|
79 |
+
|
80 |
+
print("### Step 6: Run the Assistant ###")
|
81 |
+
run = self.client.beta.threads.runs.create(
|
82 |
+
thread_id=thread.id,
|
83 |
+
assistant_id=self.assistant.id,
|
84 |
+
)
|
85 |
+
|
86 |
+
print("### Step 7: Wait for the Assistant to Finish ###")
|
87 |
+
def wait_on_run(run, thread):
|
88 |
+
while run.status == "queued" or run.status == "in_progress":
|
89 |
+
run = self.client.beta.threads.runs.retrieve(
|
90 |
+
thread_id=thread.id,
|
91 |
+
run_id=run.id,
|
92 |
+
)
|
93 |
+
time.sleep(0.5)
|
94 |
+
return run
|
95 |
+
|
96 |
+
run = wait_on_run(run, thread)
|
97 |
+
print(run)
|
98 |
+
|
99 |
+
print("### Step 8: Retrieve the Messages ###")
|
100 |
+
messages = self.client.beta.threads.messages.list(
|
101 |
+
thread_id=thread.id
|
102 |
+
)
|
103 |
+
|
104 |
+
messages_str = json.dumps(messages.dict(), indent=2)
|
105 |
+
print(codecs.decode(messages_str, 'unicode-escape'))
|
106 |
+
|
107 |
+
print("### Step 9: Retrieve the Assistant's Response ###")
|
108 |
+
answers = []
|
109 |
+
for message in messages.data:
|
110 |
+
if message.role == "assistant":
|
111 |
+
if message.content[0].type == "text":
|
112 |
+
answers.append(message.content[0].text.value + "\n\n")
|
113 |
+
else:
|
114 |
+
answers.append("Content is not text.\n\n")
|
115 |
+
elif message.role == "user":
|
116 |
+
break
|
117 |
+
|
118 |
+
# answersを逆順にする
|
119 |
+
answers.reverse()
|
120 |
+
|
121 |
+
return "".join(answers)
|
122 |
+
except Exception as e:
|
123 |
+
print(f"スレッドの作成中にエラーが発生しました: {e}")
|
124 |
+
raise
|