|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import os |
|
|
import time |
|
|
from typing import Optional |
|
|
import requests |
|
|
from zex import zex_vs_gaia_graph |
|
|
from langgraph.errors import GraphRecursionError |
|
|
|
|
|
def run_and_submit_all(question): |
|
|
|
|
|
print(f"input question is {question}") |
|
|
|
|
|
init_state = { |
|
|
"task_id": "no-task-id", |
|
|
"question": question, |
|
|
"file_name": "", |
|
|
"file_url": "", |
|
|
} |
|
|
print(f"\n try to answer : : {question}\n") |
|
|
|
|
|
config = {"recursion_limit": 12} |
|
|
final_answer = "" |
|
|
message_list = [] |
|
|
try: |
|
|
compiled_graph = zex_vs_gaia_graph.invoke(init_state,config=config) |
|
|
print(compiled_graph) |
|
|
final_answer = compiled_graph["messages"][-1].content |
|
|
for m in compiled_graph['messages']: |
|
|
role = m.type |
|
|
content = m.content |
|
|
tokens = 0 |
|
|
if len(content) > 100: |
|
|
content = content[:100] |
|
|
if role == "ai" and m.response_metadata['finish_reason'] == "tool_calls" : |
|
|
content = str(m.tool_calls) |
|
|
tokens = m.response_metadata['token_usage']['total_tokens'] |
|
|
if role == "ai" and m.response_metadata['finish_reason'] == "stop" : |
|
|
content = str(m.content) |
|
|
tokens = m.response_metadata['token_usage']['total_tokens'] |
|
|
message_list.append([role,content,tokens]) |
|
|
except GraphRecursionError as e: |
|
|
print("too much recursion: ", e) |
|
|
|
|
|
except Exception as e: |
|
|
print(init_state) |
|
|
print("other exception:", e) |
|
|
|
|
|
|
|
|
submitted_answer = final_answer |
|
|
|
|
|
return final_answer, message_list |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# Agent Answering Questions") |
|
|
gr.Markdown( |
|
|
""" |
|
|
**Instructions:** |
|
|
1. try questions like: |
|
|
- What is the latest version of MySQL ? |
|
|
- What is the latest version of postgresql ? |
|
|
- Which database is better, MySQL or PostgreSQL? Why? |
|
|
2. It may take some time to answer, so please be patient. |
|
|
3. This is a learning project by orczhou. |
|
|
""" |
|
|
) |
|
|
|
|
|
q_input = gr.Textbox(label="Input the question", lines=3, interactive=True) |
|
|
|
|
|
|
|
|
run_button = gr.Button("Summit") |
|
|
|
|
|
status_output = gr.Textbox(label="Result:", lines=5, interactive=False) |
|
|
|
|
|
results_table = gr.DataFrame( |
|
|
label="The steps agent run", |
|
|
headers=["Action by", "details","tokens"], |
|
|
wrap=True |
|
|
) |
|
|
|
|
|
run_button.click( |
|
|
fn=run_and_submit_all, |
|
|
inputs = [q_input], |
|
|
outputs=[status_output,results_table] |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(debug=True, share=False, server_name='0.0.0.0') |
|
|
|