Spaces:
Sleeping
Sleeping
Update agents.py
Browse files
agents.py
CHANGED
@@ -1,202 +1,152 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
from
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
response = requests.get(questions_url, timeout=15)
|
63 |
-
response.raise_for_status()
|
64 |
-
questions_data = response.json()
|
65 |
-
if not questions_data:
|
66 |
-
print("Fetched questions list is empty.")
|
67 |
-
return "Fetched questions list is empty or invalid format.", None
|
68 |
-
print(f"Fetched {len(questions_data)} questions.")
|
69 |
-
except requests.exceptions.RequestException as e:
|
70 |
-
print(f"Error fetching questions: {e}")
|
71 |
-
return f"Error fetching questions: {e}", None
|
72 |
-
except requests.exceptions.JSONDecodeError as e:
|
73 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
74 |
-
print(f"Response text: {response.text[:500]}")
|
75 |
-
return f"Error decoding server response for questions: {e}", None
|
76 |
-
except Exception as e:
|
77 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
78 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
79 |
-
|
80 |
-
# 3. Run your Agent
|
81 |
-
results_log = []
|
82 |
-
answers_payload = []
|
83 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
84 |
-
for item in questions_data:
|
85 |
-
task_id = item.get("task_id")
|
86 |
-
question_text = item.get("question")
|
87 |
-
if not task_id or question_text is None:
|
88 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
89 |
-
continue
|
90 |
-
try:
|
91 |
-
submitted_answer = agent(question_text)
|
92 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
93 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
94 |
-
except Exception as e:
|
95 |
-
print(f"Error running agent on task {task_id}: {e}")
|
96 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
97 |
-
|
98 |
-
if not answers_payload:
|
99 |
-
print("Agent did not produce any answers to submit.")
|
100 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
101 |
-
|
102 |
-
# 4. Prepare Submission
|
103 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
104 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
105 |
-
print(status_update)
|
106 |
-
|
107 |
-
# 5. Submit
|
108 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
109 |
-
try:
|
110 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
111 |
-
response.raise_for_status()
|
112 |
-
result_data = response.json()
|
113 |
-
final_status = (
|
114 |
-
f"Submission Successful!\n"
|
115 |
-
f"User: {result_data.get('username')}\n"
|
116 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
117 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
118 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
119 |
-
)
|
120 |
-
print("Submission successful.")
|
121 |
-
results_df = pd.DataFrame(results_log)
|
122 |
-
return final_status, results_df
|
123 |
-
except requests.exceptions.HTTPError as e:
|
124 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
125 |
-
try:
|
126 |
-
error_json = e.response.json()
|
127 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
128 |
-
except requests.exceptions.JSONDecodeError:
|
129 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
130 |
-
status_message = f"Submission Failed: {error_detail}"
|
131 |
-
print(status_message)
|
132 |
-
results_df = pd.DataFrame(results_log)
|
133 |
-
return status_message, results_df
|
134 |
-
except requests.exceptions.Timeout:
|
135 |
-
status_message = "Submission Failed: The request timed out."
|
136 |
-
print(status_message)
|
137 |
-
results_df = pd.DataFrame(results_log)
|
138 |
-
return status_message, results_df
|
139 |
-
except requests.exceptions.RequestException as e:
|
140 |
-
status_message = f"Submission Failed: Network error - {e}"
|
141 |
-
print(status_message)
|
142 |
-
results_df = pd.DataFrame(results_log)
|
143 |
-
return status_message, results_df
|
144 |
-
except Exception as e:
|
145 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
146 |
-
print(status_message)
|
147 |
-
results_df = pd.DataFrame(results_log)
|
148 |
-
return status_message, results_df
|
149 |
-
|
150 |
-
|
151 |
-
# --- Build Gradio Interface using Blocks ---
|
152 |
-
with gr.Blocks() as demo:
|
153 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
154 |
-
gr.Markdown(
|
155 |
-
"""
|
156 |
-
**Instructions:**
|
157 |
-
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
158 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
159 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
160 |
-
---
|
161 |
-
**Disclaimers:**
|
162 |
-
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
163 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
164 |
-
"""
|
165 |
-
)
|
166 |
-
|
167 |
-
gr.LoginButton()
|
168 |
-
|
169 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
170 |
-
|
171 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
172 |
-
# Removed max_rows=10 from DataFrame constructor
|
173 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
174 |
-
|
175 |
-
run_button.click(
|
176 |
-
fn=run_and_submit_all,
|
177 |
-
outputs=[status_output, results_table]
|
178 |
-
)
|
179 |
|
180 |
if __name__ == "__main__":
|
181 |
-
|
182 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
183 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
184 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
185 |
-
|
186 |
-
if space_host_startup:
|
187 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
188 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
189 |
-
else:
|
190 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
191 |
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
198 |
|
199 |
-
|
|
|
200 |
|
201 |
-
|
202 |
-
|
|
|
|
1 |
import os
|
2 |
+
|
3 |
+
from langgraph.graph import StateGraph, START, MessagesState
|
4 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
5 |
+
|
6 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
+
from langchain_groq import ChatGroq
|
8 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
9 |
+
|
10 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
11 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
12 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
13 |
+
|
14 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
15 |
+
from langchain_core.tools import tool
|
16 |
+
|
17 |
+
from supabase.client import create_client, Client
|
18 |
+
|
19 |
+
|
20 |
+
# Load environment variables
|
21 |
+
|
22 |
+
# ---- Basic Arithmetic Utilities ---- #
|
23 |
+
@tool
|
24 |
+
def multiply(a: int, b: int) -> int:
|
25 |
+
"""Returns the product of two integers."""
|
26 |
+
return a * b
|
27 |
+
|
28 |
+
@tool
|
29 |
+
def add(a: int, b: int) -> int:
|
30 |
+
"""Returns the sum of two integers."""
|
31 |
+
return a + b
|
32 |
+
|
33 |
+
@tool
|
34 |
+
def subtract(a: int, b: int) -> int:
|
35 |
+
"""Returns the difference between two integers."""
|
36 |
+
return a - b
|
37 |
+
|
38 |
+
@tool
|
39 |
+
def divide(a: int, b: int) -> float:
|
40 |
+
"""Performs division and handles zero division errors."""
|
41 |
+
if b == 0:
|
42 |
+
raise ValueError("Division by zero is undefined.")
|
43 |
+
return a / b
|
44 |
+
|
45 |
+
@tool
|
46 |
+
def modulus(a: int, b: int) -> int:
|
47 |
+
"""Returns the remainder after division."""
|
48 |
+
return a % b
|
49 |
+
|
50 |
+
|
51 |
+
# ---- Search Tools ---- #
|
52 |
+
@tool
|
53 |
+
def search_wikipedia(query: str) -> str:
|
54 |
+
"""Returns up to 2 documents related to a query from Wikipedia."""
|
55 |
+
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
56 |
+
return {"wiki_results": "\n\n---\n\n".join(
|
57 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}'
|
58 |
+
for doc in docs
|
59 |
+
)}
|
60 |
+
|
61 |
+
@tool
|
62 |
+
def search_web(query: str) -> str:
|
63 |
+
"""Fetches up to 3 web results using Tavily."""
|
64 |
+
results = TavilySearchResults(max_results=3).invoke(query=query)
|
65 |
+
return {"web_results": "\n\n---\n\n".join(
|
66 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}'
|
67 |
+
for doc in results
|
68 |
+
)}
|
69 |
+
|
70 |
+
@tool
|
71 |
+
def search_arxiv(query: str) -> str:
|
72 |
+
"""Retrieves up to 3 papers related to the query from ArXiv."""
|
73 |
+
results = ArxivLoader(query=query, load_max_docs=3).load()
|
74 |
+
return {"arvix_results": "\n\n---\n\n".join(
|
75 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}'
|
76 |
+
for doc in results
|
77 |
+
)}
|
78 |
+
|
79 |
+
|
80 |
+
system_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools. Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
81 |
+
|
82 |
+
FINAL ANSWER: [YOUR FINAL ANSWER]
|
83 |
+
|
84 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
85 |
+
- If you are asked for a number, don't use a comma in the number and avoid units like $ or % unless specified otherwise.
|
86 |
+
- If you are asked for a string, avoid using articles and abbreviations (e.g. for cities), and write digits in plain text unless specified otherwise.
|
87 |
+
- If you are asked for a comma-separated list, apply the above rules depending on whether each item is a number or string.
|
88 |
+
|
89 |
+
Your answer should start only with "FINAL ANSWER: ", followed by your result.""")
|
90 |
+
|
91 |
+
toolset = [
|
92 |
+
multiply,
|
93 |
+
add,
|
94 |
+
subtract,
|
95 |
+
divide,
|
96 |
+
modulus,
|
97 |
+
search_wikipedia,
|
98 |
+
search_web,
|
99 |
+
search_arxiv,
|
100 |
+
]
|
101 |
+
|
102 |
+
|
103 |
+
# ---- Graph Construction ---- #
|
104 |
+
def create_agent_flow(provider: str = "groq"):
|
105 |
+
"""Constructs the LangGraph conversational flow with tool support."""
|
106 |
+
|
107 |
+
if provider == "google":
|
108 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
109 |
+
elif provider == "groq":
|
110 |
+
llm = ChatGroq(api_key="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" , model="qwen-qwq-32b", temperature=0)
|
111 |
+
elif provider == "huggingface":
|
112 |
+
llm = ChatHuggingFace(llm=HuggingFaceEndpoint(
|
113 |
+
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
|
114 |
+
temperature=0
|
115 |
+
))
|
116 |
else:
|
117 |
+
raise ValueError("Unsupported provider. Choose from: 'google', 'groq', 'huggingface'.")
|
118 |
+
|
119 |
+
llm_toolchain = llm.bind_tools(toolset)
|
120 |
+
|
121 |
+
# Assistant node behavior
|
122 |
+
def assistant_node(state: MessagesState):
|
123 |
+
response = llm_toolchain.invoke(state["messages"])
|
124 |
+
return {"messages": [response]}
|
125 |
+
|
126 |
+
|
127 |
+
# Build the conversational graph
|
128 |
+
graph01 = StateGraph(MessagesState)
|
129 |
+
graph01.add_node("assistant", assistant_node)
|
130 |
+
graph01.add_node("tools", ToolNode(toolset))
|
131 |
+
graph01.add_edge(START, "assistant")
|
132 |
+
graph01.add_conditional_edges("assistant", tools_condition)
|
133 |
+
graph01.add_edge("tools", "assistant")
|
134 |
+
|
135 |
+
return graph01.compile()
|
136 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
if __name__ == "__main__":
|
139 |
+
question = "What is the capital of France?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
+
# Build the graph
|
142 |
+
compiled_graph = create_agent_flow(provider="groq")
|
143 |
+
|
144 |
+
# Prepare input messages
|
145 |
+
messages = [system_message, HumanMessage(content=question)]
|
|
|
146 |
|
147 |
+
# Run the graph
|
148 |
+
output_state = compiled_graph.invoke({"messages": messages})
|
149 |
|
150 |
+
# Print the final output
|
151 |
+
for m in output_state["messages"]:
|
152 |
+
print(m.content)
|