testAgent / BasicAgent.py
optionEdge's picture
Update BasicAgent.py
a1d1bf7 verified
raw
history blame
2.48 kB
import smolagents, numpy, math, xlrd, os
import pandas as pd
from typing import Union
from smolagents import (
tool,
CodeAgent,
HfApiModel,
InferenceClientModel,
WebSearchTool,
PythonInterpreterTool,
FinalAnswerTool,
DuckDuckGoSearchTool,
GoogleSearchTool
)
#*
@tool
def read_excel(file_path: str, sheet_name: str = "0") -> str:
"""
Read an Excel file and return it as JSON-like string.
"""
try:
# Try to convert sheet_name to int if it's a number
sheet = int(sheet_name) if sheet_name.isdigit() else sheet_name
df = pd.read_excel(file_path, sheet_name=sheet)
return df.to_json(orient="records")
except Exception as e:
return f"Error reading Excel file: {e}"
#
class newAgent:
"""Adapts smolagents.CodeAgent to the HF course template API."""
def __init__(self):
model_id = "meta-llama/Meta-Llama-3-70B-Instruct" # correct repo name
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN") # read real secret
if not hf_token:
raise RuntimeError("HUGGINGFACEHUB_API_TOKEN not set in Space secrets")
#*
system_prompt=(
"You are an agent that answers exam questions."
"Your answers should contain only what is asked for in the question, without any other content."
"Be exact and concise in your answers to get a good score on the exam questions. Do not add explanation to the answers."
"If you are ask for a list of items, reply with ONLY those items separated by commas, no other text."
"You have a tool to read Excel files. The Excel file tool is named read_excel"
)
#*
model = HfApiModel(model_id=model_id, token=hf_token)
# include FinalAnswerTool in tools so agent knows when to stop
tools = [FinalAnswerTool(), read_excel]
self.agent = CodeAgent(
tools=tools,
model=model,
add_base_tools=True,
max_steps=5 # limit reasoning time
)
def __call__(self, question: str) -> str:
"""ONE question in β†’ ONE pure-text answer out."""
#↓ Replace .run with whatever method actually returns the answer string.
result = self.agent.run(question)
return result
#answer = self.run
#agent.run(
# "At what temperature and for how long should I bake French baguettes made with type 65 flour?",
#)