Final_Assignment / agent.py
znacer's picture
attemp1(dexter)
e4a5799 unverified
raw
history blame
1.69 kB
import math
import numexpr
from langchain_community.document_loaders import ArxivLoader
from smolagents import (
CodeAgent,
DuckDuckGoSearchTool,
InferenceClientModel,
WikipediaSearchTool,
tool,
)
@tool
def calculator(expression: str) -> str:
"""Calculate expression using Python's numexpr library.
Expression should be a single line mathematical expression
that solves the problem.
Examples:
"37593 * 67" for "37593 times 67"
"37593**(1/5)" for "37593^(1/5)"
"""
local_dict = {"pi": math.pi, "e": math.e}
return str(
numexpr.evaluate(
expression.strip(),
global_dict={}, # restrict access to globals
local_dict=local_dict, # add common mathematical functions
)
)
@tool
def arxiv_search_tool(query: str) -> str:
"""
Searches Arxiv and returns a summary or full text of the given topic, along with the page URL.
"""
doc = ArxivLoader(query=query, load_max_docs=1).load()
if len(doc) > 0:
doc = doc[0]
return f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
return "No content found"
tools = [calculator, arxiv_search_tool, DuckDuckGoSearchTool, WikipediaSearchTool]
model_id = "Qwen/Qwen3-30B-A3B"
model = InferenceClientModel(
model_id=model_id, token="MY_HUGGINGFACEHUB_API_TOKEN"
) # You can choose to not pass any model_id to InferenceClientModel to use a default model
agent = CodeAgent(
model=model,
tools=tools,
add_base_tools=True,
additional_authorized_imports=["pandas", "numpy", "csv", "subprocess"],
)