nakamura196 commited on
Commit
b5cd9e9
·
1 Parent(s): 86de97d
Files changed (1) hide show
  1. app.py +101 -0
app.py CHANGED
@@ -127,6 +127,107 @@ assistant = create_assistant(client, vector_store_id)
127
  thread = create_thread(client)
128
 
129
  with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  chatbot = gr.Chatbot(type="messages")
131
  msg = gr.Textbox()
132
  clear = gr.ClearButton([msg, chatbot])
 
127
  thread = create_thread(client)
128
 
129
  with gr.Blocks() as demo:
130
+ import gradio as gr
131
+ from openai import AzureOpenAI
132
+ import os
133
+ from dotenv import load_dotenv
134
+ import time
135
+
136
+ def load_environment():
137
+ """Load environment variables."""
138
+ load_dotenv(override=True)
139
+
140
+ def initialize_openai_client():
141
+ """Initialize the Azure OpenAI client."""
142
+ return AzureOpenAI(
143
+ azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
144
+ api_key=os.getenv("AZURE_OPENAI_API_KEY"),
145
+ api_version="2024-10-01-preview"
146
+ )
147
+
148
+ def create_assistant(client, vector_store_id):
149
+ """Create an assistant with specified configuration."""
150
+ return client.beta.assistants.create(
151
+ model="gpt-4o",
152
+ instructions="",
153
+ tools=[{
154
+ "type": "file_search",
155
+ "file_search": {"ranking_options": {"ranker": "default_2024_08_21", "score_threshold": 0}}
156
+ }],
157
+ tool_resources={"file_search": {"vector_store_ids": [vector_store_id]}},
158
+ temperature=0
159
+ )
160
+
161
+ def create_thread(client):
162
+ """Create a new thread."""
163
+ return client.beta.threads.create()
164
+
165
+ def clear_thread(_):
166
+ """Clear the chat history and reset the thread."""
167
+ global thread
168
+ thread = create_thread(client)
169
+ return [], ""
170
+
171
+ def get_chatbot_response(client, thread_id, assistant_id, message):
172
+ """Get chatbot response for a given message."""
173
+ client.beta.threads.messages.create(
174
+ thread_id=thread_id,
175
+ role="user",
176
+ content=message # Ensure the content is an object with a `text` key
177
+ )
178
+
179
+ run = client.beta.threads.runs.create(
180
+ thread_id=thread_id,
181
+ assistant_id=assistant_id
182
+ )
183
+
184
+ while run.status in ["queued", "in_progress", "cancelling"]:
185
+ time.sleep(1)
186
+ run = client.beta.threads.runs.retrieve(
187
+ thread_id=thread_id,
188
+ run_id=run.id
189
+ )
190
+
191
+ if run.status == "completed":
192
+ messages = client.beta.threads.messages.list(thread_id=thread_id)
193
+
194
+ for msg in messages:
195
+ main_text = msg.content[0].text.value
196
+ return main_text
197
+
198
+ return "Unable to retrieve a response." # Fallback response
199
+
200
+ def chatbot_response(history, message):
201
+ """Wrapper function to generate chatbot response."""
202
+ global thread
203
+ # Get response from the API
204
+ assistant_response = get_chatbot_response(client, thread.id, assistant.id, message)
205
+
206
+ # Update chat history
207
+ history.append({"role": "user", "content": message})
208
+ history.append({"role": "assistant", "content": assistant_response})
209
+
210
+ return history, ""
211
+
212
+ # Load environment variables
213
+ load_environment()
214
+
215
+ # Initialize OpenAI client
216
+ client = initialize_openai_client()
217
+
218
+ # Define vector store ID
219
+ vector_store_id = os.getenv("AZURE_OPENAI_VECTOR_STORE_ID")
220
+
221
+ # Create assistant and thread
222
+ assistant = create_assistant(client, vector_store_id)
223
+ thread = create_thread(client)
224
+
225
+ with gr.Blocks() as demo:
226
+ gr.Markdown("""
227
+ # Azure OpenAI Assistants API x Gradio x Zenn
228
+ This is a Gradio demo of Retrieval-Augmented Generation (RAG) using the Azure OpenAI Assistants API, applied to [Zenn articles](https://zenn.dev/nakamura196).
229
+ """)
230
+
231
  chatbot = gr.Chatbot(type="messages")
232
  msg = gr.Textbox()
233
  clear = gr.ClearButton([msg, chatbot])