Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,9 +1,30 @@
|
|
1 |
-
from smolagents import CodeAgent,
|
2 |
-
# from tools.final_answer import FinalAnswerTool
|
3 |
import asyncio
|
4 |
import os
|
5 |
import yaml
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
class MagAgent:
|
8 |
def __init__(self):
|
9 |
"""Initialize the MagAgent with search tools."""
|
@@ -21,9 +42,10 @@ class MagAgent:
|
|
21 |
self.agent = CodeAgent(
|
22 |
model= model,
|
23 |
tools=[
|
24 |
-
|
25 |
WikipediaSearchTool(),
|
26 |
-
|
|
|
27 |
]
|
28 |
)
|
29 |
print("MagAgent initialized.")
|
@@ -35,13 +57,15 @@ class MagAgent:
|
|
35 |
# Define a task with fallback search logic
|
36 |
task = (
|
37 |
f"Answer the following question accurately and concisely: {question}\n"
|
38 |
-
"First, try searching Wikipedia with 'Mercedes Sosa'. If that fails, "
|
39 |
-
"use DuckDuckGo to search 'Mercedes Sosa discography 2000-2009'."
|
40 |
)
|
41 |
response = await asyncio.to_thread(
|
42 |
self.agent.run,
|
43 |
task=task
|
44 |
)
|
|
|
|
|
|
|
|
|
45 |
if not response or "No Wikipedia page found" in response:
|
46 |
# Fallback response if search fails
|
47 |
response = "Unable to retrieve exact data. Please refine the question or check external sources."
|
|
|
1 |
+
from smolagents import CodeAgent, WikipediaSearchTool, LiteLLMModel, tool, load_tool # HfApiModel, OpenAIServerModel
|
|
|
2 |
import asyncio
|
3 |
import os
|
4 |
import yaml
|
5 |
|
6 |
+
# Simulated additional tools (implementation depends on external APIs or setup)
|
7 |
+
class GoogleSearchTool:
|
8 |
+
def __init__(self):
|
9 |
+
self.api_key = os.environ.get("GOOGLE_API_KEY")
|
10 |
+
self.cse_id = os.environ.get("GOOGLE_CSE_ID")
|
11 |
+
if not self.api_key or not self.cse_id:
|
12 |
+
raise ValueError("GOOGLE_API_KEY and GOOGLE_CSE_ID must be set in environment variables.")
|
13 |
+
|
14 |
+
def search(self, query: str) -> str:
|
15 |
+
# Placeholder: In practice, use googleapiclient or similar
|
16 |
+
return f"Google search results for '{query}' (simulated)."
|
17 |
+
|
18 |
+
class ImageAnalysisTool:
|
19 |
+
def analyze(self, image_path: str) -> str:
|
20 |
+
# Placeholder: Use Google Vision API or similar in real implementation
|
21 |
+
return f"Analyzed image at '{image_path}' (simulated description)."
|
22 |
+
|
23 |
+
class LocalFileAudioTool:
|
24 |
+
def transcribe(self, file_path: str) -> str:
|
25 |
+
# Placeholder: Use speech recognition library like SpeechRecognition in real setup
|
26 |
+
return f"Transcribed audio from '{file_path}' (simulated transcription)."
|
27 |
+
|
28 |
class MagAgent:
|
29 |
def __init__(self):
|
30 |
"""Initialize the MagAgent with search tools."""
|
|
|
42 |
self.agent = CodeAgent(
|
43 |
model= model,
|
44 |
tools=[
|
45 |
+
GoogleSearchTool(),
|
46 |
WikipediaSearchTool(),
|
47 |
+
ImageAnalysisTool(),
|
48 |
+
LocalFileAudioTool()
|
49 |
]
|
50 |
)
|
51 |
print("MagAgent initialized.")
|
|
|
57 |
# Define a task with fallback search logic
|
58 |
task = (
|
59 |
f"Answer the following question accurately and concisely: {question}\n"
|
|
|
|
|
60 |
)
|
61 |
response = await asyncio.to_thread(
|
62 |
self.agent.run,
|
63 |
task=task
|
64 |
)
|
65 |
+
|
66 |
+
# Ensure response is a string, fixing the integer error
|
67 |
+
response = str(response) if response is not None else "No answer found."
|
68 |
+
|
69 |
if not response or "No Wikipedia page found" in response:
|
70 |
# Fallback response if search fails
|
71 |
response = "Unable to retrieve exact data. Please refine the question or check external sources."
|