Spaces:
Sleeping
Sleeping
Updated call method in BasicAgent and system prompt
Browse files- app.py +76 -12
- system_prompt.txt +3 -1
app.py
CHANGED
@@ -2,8 +2,9 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
-
from typing import List
|
6 |
from dotenv import load_dotenv
|
|
|
7 |
|
8 |
# LlamaIndex Imports
|
9 |
from llama_index.core.llms import LLM
|
@@ -70,15 +71,15 @@ class BasicAgent:
|
|
70 |
|
71 |
def _build_agent(self) -> ReActAgent:
|
72 |
"""Build and return the agent."""
|
73 |
-
# Load system prompt from file
|
74 |
try:
|
75 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
76 |
system_prompt = f.read()
|
|
|
|
|
77 |
except Exception as e:
|
78 |
print(f"Error loading system prompt: {e}")
|
79 |
-
system_prompt = "
|
80 |
-
You can use tools when necessary to look up information, perform calculations, or process special text formats.
|
81 |
-
Always provide concise, accurate answers based on the question asked."""
|
82 |
|
83 |
return ReActAgent.from_tools(
|
84 |
tools=self.tools,
|
@@ -93,12 +94,31 @@ Always provide concise, accurate answers based on the question asked."""
|
|
93 |
try:
|
94 |
# Process the question
|
95 |
response = self.agent.query(question)
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
except Exception as e:
|
100 |
print(f"Error generating answer: {e}")
|
101 |
-
|
|
|
102 |
|
103 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
104 |
"""
|
@@ -153,6 +173,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
153 |
# 3. Run your Agent
|
154 |
results_log = []
|
155 |
answers_payload = []
|
|
|
|
|
|
|
156 |
print(f"Running agent on {len(questions_data)} questions...")
|
157 |
for item in questions_data:
|
158 |
task_id = item.get("task_id")
|
@@ -161,17 +184,58 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
161 |
print(f"Skipping item with missing task_id or question: {item}")
|
162 |
continue
|
163 |
try:
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
except Exception as e:
|
168 |
print(f"Error running agent on task {task_id}: {e}")
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
if not answers_payload:
|
172 |
print("Agent did not produce any answers to submit.")
|
173 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# 4. Prepare Submission
|
176 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
177 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
+
from typing import List, Dict, Any
|
6 |
from dotenv import load_dotenv
|
7 |
+
import json
|
8 |
|
9 |
# LlamaIndex Imports
|
10 |
from llama_index.core.llms import LLM
|
|
|
71 |
|
72 |
def _build_agent(self) -> ReActAgent:
|
73 |
"""Build and return the agent."""
|
74 |
+
# Load system prompt from file and append output format requirements
|
75 |
try:
|
76 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
77 |
system_prompt = f.read()
|
78 |
+
# Append output format to system prompt
|
79 |
+
system_prompt = f"{system_prompt}\n\nIMPORTANT OUTPUT FORMAT:\n{OUTPUT_FORMAT}"
|
80 |
except Exception as e:
|
81 |
print(f"Error loading system prompt: {e}")
|
82 |
+
system_prompt = f"You are an intelligent agent designed to answer a wide variety of questions.\n\nIMPORTANT OUTPUT FORMAT:\n{OUTPUT_FORMAT}"
|
|
|
|
|
83 |
|
84 |
return ReActAgent.from_tools(
|
85 |
tools=self.tools,
|
|
|
94 |
try:
|
95 |
# Process the question
|
96 |
response = self.agent.query(question)
|
97 |
+
answer_text = str(response)
|
98 |
+
|
99 |
+
# Extract the FINAL ANSWER part if it exists
|
100 |
+
if "FINAL ANSWER:" in answer_text:
|
101 |
+
reasoning_trace = answer_text.split("FINAL ANSWER:")[0].strip()
|
102 |
+
model_answer = answer_text.split("FINAL ANSWER:")[1].strip()
|
103 |
+
|
104 |
+
# Include the reasoning trace in the response but formatted for JSON
|
105 |
+
result = {
|
106 |
+
"model_answer": model_answer,
|
107 |
+
"reasoning_trace": reasoning_trace
|
108 |
+
}
|
109 |
+
|
110 |
+
# Return just the answer part for direct evaluation
|
111 |
+
print(f"Agent generated answer: {model_answer[:50]}..." if len(model_answer) > 50 else f"Agent generated answer: {model_answer}")
|
112 |
+
return json.dumps(result)
|
113 |
+
else:
|
114 |
+
# If no FINAL ANSWER pattern, return the whole response
|
115 |
+
print(f"No 'FINAL ANSWER' found in response. Returning full response.")
|
116 |
+
return json.dumps({"model_answer": answer_text, "reasoning_trace": ""})
|
117 |
+
|
118 |
except Exception as e:
|
119 |
print(f"Error generating answer: {e}")
|
120 |
+
error_msg = f"I encountered an error while answering your question: {str(e)}"
|
121 |
+
return json.dumps({"model_answer": error_msg, "reasoning_trace": ""})
|
122 |
|
123 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
124 |
"""
|
|
|
173 |
# 3. Run your Agent
|
174 |
results_log = []
|
175 |
answers_payload = []
|
176 |
+
# Also create JSONL file for submission
|
177 |
+
jsonl_output = []
|
178 |
+
|
179 |
print(f"Running agent on {len(questions_data)} questions...")
|
180 |
for item in questions_data:
|
181 |
task_id = item.get("task_id")
|
|
|
184 |
print(f"Skipping item with missing task_id or question: {item}")
|
185 |
continue
|
186 |
try:
|
187 |
+
# Get agent response which is now a JSON string
|
188 |
+
agent_response_json = agent(question_text)
|
189 |
+
agent_response = json.loads(agent_response_json)
|
190 |
+
|
191 |
+
model_answer = agent_response.get("model_answer", "")
|
192 |
+
reasoning_trace = agent_response.get("reasoning_trace", "")
|
193 |
+
|
194 |
+
# Format for submission payload
|
195 |
+
submitted_answer = model_answer
|
196 |
+
|
197 |
+
# Add to answers payload
|
198 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
199 |
+
|
200 |
+
# Add to results log for display
|
201 |
+
results_log.append({
|
202 |
+
"Task ID": task_id,
|
203 |
+
"Question": question_text,
|
204 |
+
"Submitted Answer": submitted_answer,
|
205 |
+
"Reasoning": reasoning_trace[:100] + "..." if len(reasoning_trace) > 100 else reasoning_trace
|
206 |
+
})
|
207 |
+
|
208 |
+
# Add to JSONL output
|
209 |
+
jsonl_output.append({
|
210 |
+
"task_id": task_id,
|
211 |
+
"model_answer": model_answer,
|
212 |
+
"reasoning_trace": reasoning_trace
|
213 |
+
})
|
214 |
+
|
215 |
except Exception as e:
|
216 |
print(f"Error running agent on task {task_id}: {e}")
|
217 |
+
error_msg = f"AGENT ERROR: {e}"
|
218 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
|
219 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": error_msg})
|
220 |
+
jsonl_output.append({
|
221 |
+
"task_id": task_id,
|
222 |
+
"model_answer": error_msg,
|
223 |
+
"reasoning_trace": ""
|
224 |
+
})
|
225 |
|
226 |
if not answers_payload:
|
227 |
print("Agent did not produce any answers to submit.")
|
228 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
229 |
|
230 |
+
# Save JSONL output to file
|
231 |
+
try:
|
232 |
+
with open("submissions.jsonl", "w") as f:
|
233 |
+
for item in jsonl_output:
|
234 |
+
f.write(json.dumps(item) + "\n")
|
235 |
+
print("Saved submissions to submissions.jsonl")
|
236 |
+
except Exception as e:
|
237 |
+
print(f"Error saving submissions.jsonl: {e}")
|
238 |
+
|
239 |
# 4. Prepare Submission
|
240 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
241 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
system_prompt.txt
CHANGED
@@ -26,6 +26,8 @@ When using tools:
|
|
26 |
- For recent events or specialized topics, use web search
|
27 |
- If a question is unclear, ask for clarification
|
28 |
|
29 |
-
Always provide concise, accurate answers that directly address the question. Format your response according to any specific instructions in the question.
|
|
|
|
|
30 |
|
31 |
Remember: The quality of your answers will be evaluated based on accuracy, relevance, and adherence to specified formats.
|
|
|
26 |
- For recent events or specialized topics, use web search
|
27 |
- If a question is unclear, ask for clarification
|
28 |
|
29 |
+
Always provide concise, accurate answers that directly address the question. Format your response according to any specific instructions in the question.
|
30 |
+
|
31 |
+
IMPORTANT: After providing your thoughts and reasoning about the question, ALWAYS end your response with "FINAL ANSWER: [your answer]" where your answer should be as concise as possible. If asked for a number, don't use commas or units. If asked for text, avoid articles and abbreviations unless specifically requested.
|
32 |
|
33 |
Remember: The quality of your answers will be evaluated based on accuracy, relevance, and adherence to specified formats.
|