Spaces:
Sleeping
Sleeping
Update src/agent.py
Browse files- src/agent.py +54 -10
src/agent.py
CHANGED
|
@@ -1,40 +1,84 @@
|
|
| 1 |
from src.settings import Settings
|
| 2 |
-
from smolagents import LiteLLMModel, ToolCallingAgent
|
| 3 |
from tools.FinalAnswerTool import FinalAnswerTool
|
| 4 |
from tools.ReadAudioTool import ReadAudioTool
|
| 5 |
from tools.ReadImageTool import ReadImageTool
|
| 6 |
from tools.ReadTextTool import ReadTextTool
|
| 7 |
from tools.ReadVideoTool import ReadVideoTool
|
| 8 |
-
from tools.WebSearchTool import
|
| 9 |
from tools.YouTubeTool import YouTubeTool
|
| 10 |
from tools.PythonRunnerTool import PythonRunnerTool
|
| 11 |
-
from tools.
|
|
|
|
| 12 |
from src.utils import InputTokenRateLimiter
|
|
|
|
|
|
|
| 13 |
import time
|
| 14 |
import random
|
| 15 |
-
from litellm import completion
|
| 16 |
|
| 17 |
settings = Settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
class BasicAgent():
|
| 19 |
def __init__(self):
|
| 20 |
self.model = LiteLLMModel(
|
| 21 |
model_id=settings.llm_model_id,
|
| 22 |
api_key=settings.llm_api_key
|
| 23 |
)
|
| 24 |
-
self.agent =
|
|
|
|
| 25 |
tools=[
|
| 26 |
FinalAnswerTool(),
|
| 27 |
ReadAudioTool(),
|
| 28 |
ReadImageTool(),
|
| 29 |
ReadTextTool(),
|
| 30 |
ReadVideoTool(),
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
YouTubeTool(),
|
| 34 |
-
PythonRunnerTool()
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
],
|
| 37 |
-
max_steps=
|
| 38 |
planning_interval=3,
|
| 39 |
model=self.model
|
| 40 |
)
|
|
|
|
| 1 |
from src.settings import Settings
|
| 2 |
+
from smolagents import LiteLLMModel, ToolCallingAgent, CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, Tool
|
| 3 |
from tools.FinalAnswerTool import FinalAnswerTool
|
| 4 |
from tools.ReadAudioTool import ReadAudioTool
|
| 5 |
from tools.ReadImageTool import ReadImageTool
|
| 6 |
from tools.ReadTextTool import ReadTextTool
|
| 7 |
from tools.ReadVideoTool import ReadVideoTool
|
| 8 |
+
from tools.WebSearchTool import DuckDuckGoSearchTool
|
| 9 |
from tools.YouTubeTool import YouTubeTool
|
| 10 |
from tools.PythonRunnerTool import PythonRunnerTool
|
| 11 |
+
from tools.PythonCalcTool import PythonCalcTool
|
| 12 |
+
from tools.SemanticScholar import AcademicPaperSearchTool
|
| 13 |
from src.utils import InputTokenRateLimiter
|
| 14 |
+
import wikipedia as wiki
|
| 15 |
+
from markdownify import markdownify as to_markdown
|
| 16 |
import time
|
| 17 |
import random
|
|
|
|
| 18 |
|
| 19 |
settings = Settings()
|
| 20 |
+
import litellm
|
| 21 |
+
from litellm import completion
|
| 22 |
+
|
| 23 |
+
#litellm._turn_on_debug()
|
| 24 |
+
class WikiTitleFinder(Tool):
|
| 25 |
+
name = "wiki_titles"
|
| 26 |
+
description = "Search for related Wikipedia page titles."
|
| 27 |
+
inputs = {"query": {"type": "string", "description": "Search query."}}
|
| 28 |
+
output_type = "string"
|
| 29 |
+
|
| 30 |
+
def forward(self, query: str) -> str:
|
| 31 |
+
results = wiki.search(query)
|
| 32 |
+
return ", ".join(results) if results else "No results."
|
| 33 |
+
|
| 34 |
+
class WikiContentFetcher(Tool):
|
| 35 |
+
name = "wiki_page"
|
| 36 |
+
description = "Fetch Wikipedia page content."
|
| 37 |
+
inputs = {"page_title": {"type": "string", "description": "Wikipedia page title."}}
|
| 38 |
+
output_type = "string"
|
| 39 |
+
|
| 40 |
+
def forward(self, page_title: str) -> str:
|
| 41 |
+
try:
|
| 42 |
+
return to_markdown(wiki.page(page_title).html())
|
| 43 |
+
except wiki.exceptions.PageError:
|
| 44 |
+
return f"'{page_title}' not found."
|
| 45 |
+
|
| 46 |
+
class MathSolver(Tool):
|
| 47 |
+
name = "math_solver"
|
| 48 |
+
description = "Safely evaluate basic math expressions."
|
| 49 |
+
inputs = {"input": {"type": "string", "description": "Math expression to evaluate."}}
|
| 50 |
+
output_type = "string"
|
| 51 |
+
|
| 52 |
+
def forward(self, input: str) -> str:
|
| 53 |
+
try:
|
| 54 |
+
return str(eval(input, {"__builtins__": {}}))
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Math error: {e}"
|
| 57 |
+
|
| 58 |
class BasicAgent():
|
| 59 |
def __init__(self):
|
| 60 |
self.model = LiteLLMModel(
|
| 61 |
model_id=settings.llm_model_id,
|
| 62 |
api_key=settings.llm_api_key
|
| 63 |
)
|
| 64 |
+
self.agent = CodeAgent(
|
| 65 |
+
model=self.model,
|
| 66 |
tools=[
|
| 67 |
FinalAnswerTool(),
|
| 68 |
ReadAudioTool(),
|
| 69 |
ReadImageTool(),
|
| 70 |
ReadTextTool(),
|
| 71 |
ReadVideoTool(),
|
| 72 |
+
DuckDuckGoSearchTool(),
|
| 73 |
+
WikiTitleFinder(),
|
| 74 |
+
WikiContentFetcher(),
|
| 75 |
YouTubeTool(),
|
| 76 |
+
PythonRunnerTool(),
|
| 77 |
+
PythonCalcTool(),
|
| 78 |
+
AcademicPaperSearchTool(),
|
| 79 |
+
MathSolver()
|
| 80 |
],
|
| 81 |
+
max_steps=5,
|
| 82 |
planning_interval=3,
|
| 83 |
model=self.model
|
| 84 |
)
|