Spaces:
Sleeping
Sleeping
session id update
Browse files- chatbot-gradio.py +12 -8
chatbot-gradio.py
CHANGED
|
@@ -161,9 +161,12 @@ kernel.add_plugin(ConverterPlugin(logger=logger), "CosmosDBPlugin")
|
|
| 161 |
kernel.add_plugin(ChatMemoryPlugin(logger=logger), "ChatMemoryPlugin")
|
| 162 |
kernel.add_plugin(NL2SQLPlugin(), "NL2SQLPlugin")
|
| 163 |
|
|
|
|
|
|
|
|
|
|
| 164 |
# Updated query handler using function calling
|
| 165 |
-
async def handle_query(user_input: str):
|
| 166 |
-
session_id =
|
| 167 |
|
| 168 |
settings = AzureChatPromptExecutionSettings(
|
| 169 |
function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True)
|
|
@@ -245,7 +248,6 @@ async def handle_query(user_input: str):
|
|
| 245 |
)
|
| 246 |
|
| 247 |
func_name = result.model_dump()["metadata"]["messages"]["messages"][2]["items"][0]["name"]
|
| 248 |
-
print(func_name)
|
| 249 |
log_func = kernel.get_function("ChatMemoryPlugin", "log_interaction")
|
| 250 |
await log_func.invoke(
|
| 251 |
kernel=kernel,
|
|
@@ -440,6 +442,8 @@ def toggle_panel():
|
|
| 440 |
return gr.Column(visible=panel_visible)
|
| 441 |
|
| 442 |
with gr.Blocks(css=custom_css) as demo:
|
|
|
|
|
|
|
| 443 |
# Toggle button (floating action button)
|
| 444 |
toggle_btn = gr.Button("💬", elem_id="chatbot-toggle-btn")
|
| 445 |
|
|
@@ -462,16 +466,16 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 462 |
|
| 463 |
|
| 464 |
# Function to handle messages
|
| 465 |
-
async def respond(message, chat_history):
|
| 466 |
-
response = await handle_query(message)
|
| 467 |
-
# Convert existing history to OpenAI format if it's in tuples
|
| 468 |
|
| 469 |
# Add new messages
|
| 470 |
chat_history.append({"role": "user", "content": message})
|
| 471 |
chat_history.append({"role": "assistant", "content": response})
|
| 472 |
return "", chat_history
|
| 473 |
-
|
| 474 |
-
|
|
|
|
| 475 |
toggle_btn.click(toggle_panel, outputs=chat_panel)
|
| 476 |
|
| 477 |
demo.launch()
|
|
|
|
| 161 |
kernel.add_plugin(ChatMemoryPlugin(logger=logger), "ChatMemoryPlugin")
|
| 162 |
kernel.add_plugin(NL2SQLPlugin(), "NL2SQLPlugin")
|
| 163 |
|
| 164 |
+
def initialize_session():
|
| 165 |
+
return str(uuid.uuid4())
|
| 166 |
+
|
| 167 |
# Updated query handler using function calling
|
| 168 |
+
async def handle_query(user_input: str, session_state):
|
| 169 |
+
session_id = session_state
|
| 170 |
|
| 171 |
settings = AzureChatPromptExecutionSettings(
|
| 172 |
function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True)
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
func_name = result.model_dump()["metadata"]["messages"]["messages"][2]["items"][0]["name"]
|
|
|
|
| 251 |
log_func = kernel.get_function("ChatMemoryPlugin", "log_interaction")
|
| 252 |
await log_func.invoke(
|
| 253 |
kernel=kernel,
|
|
|
|
| 442 |
return gr.Column(visible=panel_visible)
|
| 443 |
|
| 444 |
with gr.Blocks(css=custom_css) as demo:
|
| 445 |
+
session_state = gr.State(value=initialize_session())
|
| 446 |
+
|
| 447 |
# Toggle button (floating action button)
|
| 448 |
toggle_btn = gr.Button("💬", elem_id="chatbot-toggle-btn")
|
| 449 |
|
|
|
|
| 466 |
|
| 467 |
|
| 468 |
# Function to handle messages
|
| 469 |
+
async def respond(message, chat_history, session_state):
|
| 470 |
+
response = await handle_query(message, session_state)
|
|
|
|
| 471 |
|
| 472 |
# Add new messages
|
| 473 |
chat_history.append({"role": "user", "content": message})
|
| 474 |
chat_history.append({"role": "assistant", "content": response})
|
| 475 |
return "", chat_history
|
| 476 |
+
|
| 477 |
+
send.click(respond, [msg, chatbot, session_state], [msg,chatbot])
|
| 478 |
+
msg.submit(respond, [msg, chatbot, session_state], [msg, chatbot])
|
| 479 |
toggle_btn.click(toggle_panel, outputs=chat_panel)
|
| 480 |
|
| 481 |
demo.launch()
|