Spaces:
Sleeping
Sleeping
File size: 4,220 Bytes
d787c8d 8c77163 6da9730 48d443e 5b78aca b68acbf c050621 8c77163 c050621 b68acbf c050621 edab6f8 76befd3 48d443e c050621 b68acbf c050621 99040a9 c050621 5b78aca c050621 5b78aca edab6f8 a168d8d c050621 edab6f8 a168d8d d87bf59 a168d8d edab6f8 8c77163 7581542 8c77163 7581542 d87bf59 7581542 d87bf59 8c77163 7581542 8c77163 7581542 8c77163 7581542 8c77163 |
1 2 3 4 5 6 7 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import os
from smolagents import InferenceClientModel, ToolCallingAgent
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
from gaia_tools import RunPythonFileTool, WikipediaSearchTool, ReverseTextTool
class GaiaAgent:
def __init__(self):
print("Gaia Agent Initialized")
self.model = InferenceClientModel(
model_id="meta-llama/Meta-Llama-3-70B-Instruct",
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
)
self.tools = [
DuckDuckGoSearchTool(),
VisitWebpageTool(),
RunPythonFileTool,
WikipediaSearchTool,
ReverseTextTool
]
self.agent = ToolCallingAgent(
tools = self.tools,
model = self.model
)
def __call__(self, question: str) -> str:
print(f"Agent received question (first 50 chars): {question[:50]}...")
prompt = f"""
You are a helpful agent that must provide exact answers to questions. Do not explain or format your answer in any way.
CRITICAL: If the question starts with a period or looks backwards, use ReverseTextTool to reverse it first.
For Wikipedia research:
- ALWAYS search for the main Wikipedia page of the subject first
- Use WikipediaSearchTool with the exact name (e.g., "Mercedes Sosa")
- Look specifically in the "Discography" or "Albums" section
- Count only items explicitly labeled as "studio albums"
- Exclude live albums, compilation albums, or singles
- For Featured Articles, search "Wikipedia Featured Articles [month] [year]"
For text puzzles:
- If reversed, use ReverseTextTool then solve the resulting question
- Simple word/logic puzzles can be solved directly
Question: {question}
SEARCH CONSTRAINTS:
- Use exact names and specific Wikipedia sections
- Be precise about album types (studio vs. live vs. compilation)
- For date ranges, include both start and end years
- Always verify information from the main Wikipedia article
Only output the final answer (number, word, or name).
"""
try:
result = self.agent.run(prompt)
print(f"Raw result from agent: {result}")
print(f"Result type: {type(result)}")
# Handle different result types more robustly
if isinstance(result, dict):
if "answer" in result:
return str(result["answer"]).strip()
elif "content" in result:
return str(result["content"]).strip()
elif len(result) == 0:
# Empty dictionary - likely a tool calling issue
return "ERROR: Empty response from agent"
else:
# Try to find any string value in the dict
for key, value in result.items():
if isinstance(value, str) and value.strip():
return value.strip()
return f"ERROR: No valid content in response: {result}"
elif isinstance(result, str):
return result.strip()
elif isinstance(result, list):
# Look for assistant messages in conversation format
for item in reversed(result):
if isinstance(item, dict):
if item.get("role") == "assistant" and "content" in item:
return item["content"].strip()
elif "content" in item:
return item["content"].strip()
# If no assistant message found, try to extract any string
for item in reversed(result):
if isinstance(item, str) and item.strip():
return item.strip()
return str(result)
elif isinstance(result, (int, float)):
return str(result)
else:
return str(result)
except Exception as e:
print(f"Exception during agent run: {e}")
return f"AGENT ERROR: {e}"
|