Spaces:
Runtime error
Runtime error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
from smolagents import HfApiModel, CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, Tool, LiteLLMModel
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
|
|
@@ -126,6 +128,77 @@ class modulo(Tool):
|
|
| 126 |
def forward(self, a: int, b: int):
|
| 127 |
return a % b
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
tools=[
|
| 130 |
add(),
|
| 131 |
subtract(),
|
|
|
|
| 1 |
from smolagents import HfApiModel, CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, Tool, LiteLLMModel
|
| 2 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 3 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 4 |
|
| 5 |
|
| 6 |
|
|
|
|
| 128 |
def forward(self, a: int, b: int):
|
| 129 |
return a % b
|
| 130 |
|
| 131 |
+
class WikipediaSearchTool(tool):
|
| 132 |
+
name = "wikipedia_search_tool"
|
| 133 |
+
|
| 134 |
+
description = """
|
| 135 |
+
Search Wikipedia for a query and return top 2 results.
|
| 136 |
+
|
| 137 |
+
Args:
|
| 138 |
+
query: the search query.
|
| 139 |
+
"""
|
| 140 |
+
|
| 141 |
+
inputs = {
|
| 142 |
+
"query":{
|
| 143 |
+
"type":"string",
|
| 144 |
+
"description":"the search query"
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
output_type = "string"
|
| 148 |
+
|
| 149 |
+
def forward(self, query: str) -> str:
|
| 150 |
+
documents = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 151 |
+
condensed_docs = "\n\n---\n\n".join(
|
| 152 |
+
[
|
| 153 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 154 |
+
for doc in documents
|
| 155 |
+
])
|
| 156 |
+
return {"wikipedia_results": condensed_docs}
|
| 157 |
+
|
| 158 |
+
class TavilySearchTool(tool):
|
| 159 |
+
name = "tavily_search_tool"
|
| 160 |
+
|
| 161 |
+
description = """
|
| 162 |
+
Search the internet using the browser Tavily and return the top 3 results.
|
| 163 |
+
|
| 164 |
+
Args:
|
| 165 |
+
query: the search query.
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
inputs = {
|
| 169 |
+
"query":{
|
| 170 |
+
"type":"string",
|
| 171 |
+
"description":"the search query"
|
| 172 |
+
}
|
| 173 |
+
}
|
| 174 |
+
output_type = "string"
|
| 175 |
+
|
| 176 |
+
def forward(self, query: str) -> str:
|
| 177 |
+
documents = TavilySearchResults(max_results=3).invoke(query=query)
|
| 178 |
+
condensed_docs = "\n\n---\n\n".join(
|
| 179 |
+
[
|
| 180 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 181 |
+
for doc in documents
|
| 182 |
+
])
|
| 183 |
+
return {"web_search_results": condensed_docs}
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
@tool
|
| 187 |
+
def arvix_search(query: str) -> str:
|
| 188 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 189 |
+
|
| 190 |
+
Args:
|
| 191 |
+
query: The search query."""
|
| 192 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 193 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 194 |
+
[
|
| 195 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 196 |
+
for doc in search_docs
|
| 197 |
+
])
|
| 198 |
+
return {"arvix_results": formatted_search_docs}
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
|
| 202 |
tools=[
|
| 203 |
add(),
|
| 204 |
subtract(),
|