Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# agent.py
|
| 2 |
import os
|
| 3 |
import datetime
|
| 4 |
import requests
|
|
@@ -8,18 +7,26 @@ from typing import List
|
|
| 8 |
|
| 9 |
from langchain.text_splitter import CharacterTextSplitter
|
| 10 |
from tools.final_answer import FinalAnswerTool
|
| 11 |
-
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool,
|
| 12 |
|
| 13 |
# === TOOLS ===
|
| 14 |
|
| 15 |
@tool
|
| 16 |
def web_search(query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
search_tool = DuckDuckGoSearchTool()
|
| 18 |
results = search_tool(query)
|
| 19 |
return "\n".join(results)
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
tz = pytz.timezone(timezone)
|
| 25 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -29,14 +36,22 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 29 |
|
| 30 |
@tool
|
| 31 |
def visit_webpage(url: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
response = requests.get(url, timeout=5)
|
| 34 |
-
return response.text[:5000]
|
| 35 |
except Exception as e:
|
| 36 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
def text_splitter(text: str) -> List[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
|
| 41 |
return splitter.split_text(text)
|
| 42 |
|
|
@@ -84,10 +99,6 @@ agent = CodeAgent(
|
|
| 84 |
prompt_templates=prompt_templates
|
| 85 |
)
|
| 86 |
|
| 87 |
-
# ===
|
| 88 |
-
def
|
| 89 |
-
|
| 90 |
-
result = agent(question)
|
| 91 |
-
return [str(result)]
|
| 92 |
-
except Exception as e:
|
| 93 |
-
return [f"Error: {e}"]
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import datetime
|
| 3 |
import requests
|
|
|
|
| 7 |
|
| 8 |
from langchain.text_splitter import CharacterTextSplitter
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
| 10 |
+
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, load_tool, tool
|
| 11 |
|
| 12 |
# === TOOLS ===
|
| 13 |
|
| 14 |
@tool
|
| 15 |
def web_search(query: str) -> str:
|
| 16 |
+
"""Allows search through DuckDuckGo.
|
| 17 |
+
Args:
|
| 18 |
+
query: what you want to search
|
| 19 |
+
"""
|
| 20 |
search_tool = DuckDuckGoSearchTool()
|
| 21 |
results = search_tool(query)
|
| 22 |
return "\n".join(results)
|
| 23 |
|
| 24 |
@tool
|
| 25 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
+
"""Fetches the current local time in a specified timezone.
|
| 27 |
+
Args:
|
| 28 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 29 |
+
"""
|
| 30 |
try:
|
| 31 |
tz = pytz.timezone(timezone)
|
| 32 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 36 |
|
| 37 |
@tool
|
| 38 |
def visit_webpage(url: str) -> str:
|
| 39 |
+
"""Fetches raw HTML content of a web page.
|
| 40 |
+
Args:
|
| 41 |
+
url: The url of the webpage.
|
| 42 |
+
"""
|
| 43 |
try:
|
| 44 |
response = requests.get(url, timeout=5)
|
| 45 |
+
return response.text[:5000] # Limit length
|
| 46 |
except Exception as e:
|
| 47 |
return f"[ERROR fetching {url}]: {str(e)}"
|
| 48 |
|
| 49 |
@tool
|
| 50 |
def text_splitter(text: str) -> List[str]:
|
| 51 |
+
"""Splits text into chunks using LangChain's CharacterTextSplitter.
|
| 52 |
+
Args:
|
| 53 |
+
text: A string of text to split.
|
| 54 |
+
"""
|
| 55 |
splitter = CharacterTextSplitter(chunk_size=450, chunk_overlap=10)
|
| 56 |
return splitter.split_text(text)
|
| 57 |
|
|
|
|
| 99 |
prompt_templates=prompt_templates
|
| 100 |
)
|
| 101 |
|
| 102 |
+
# === EXPORT AGENT ===
|
| 103 |
+
def get_agent():
|
| 104 |
+
return agent
|
|
|
|
|
|
|
|
|
|
|
|