Definisci i prompt dell'agente
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import inspect
|
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
from smolagents import CodeAgent, TransformersModel, VisitWebpageTool, PythonInterpreterTool, WebSearchTool, WikipediaSearchTool, FinalAnswerTool, Tool, tool # InferenceClientModel, GoogleSearchTool (usa SERPAPI_API_KEY), DuckDuckGoSearchTool
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
@@ -27,12 +28,25 @@ def invert_sentence(sentence: str) -> str:
|
|
| 27 |
class FirstAgent:
|
| 28 |
### First Agent is the first attempt to develop an agent for the course. ###
|
| 29 |
def __init__(self):
|
|
|
|
| 30 |
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 31 |
model = TransformersModel(model_id=model_id)
|
| 32 |
|
| 33 |
# Inizializza l'agente
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
self.agent = CodeAgent(
|
| 35 |
model=model,
|
|
|
|
| 36 |
tools=[
|
| 37 |
WebSearchTool(),
|
| 38 |
PythonInterpreterTool(),
|
|
@@ -43,11 +57,12 @@ class FirstAgent:
|
|
| 43 |
print("FirstAgent inizializzato.")
|
| 44 |
|
| 45 |
def __call__(self, question: str) -> str:
|
|
|
|
| 46 |
print(f"Agent ricevuto domanda (primi 50 char): {question[:50]}...")
|
| 47 |
try:
|
| 48 |
answer = self.agent.run(question)
|
| 49 |
print(f"Agent restituisce risposta: {str(answer)[:100]}...")
|
| 50 |
-
return
|
| 51 |
except Exception as e:
|
| 52 |
print(f"Errore nell'agente: {e}")
|
| 53 |
return f"Errore nell'agente: {str(e)}"
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
|
| 8 |
from smolagents import CodeAgent, TransformersModel, VisitWebpageTool, PythonInterpreterTool, WebSearchTool, WikipediaSearchTool, FinalAnswerTool, Tool, tool # InferenceClientModel, GoogleSearchTool (usa SERPAPI_API_KEY), DuckDuckGoSearchTool
|
| 9 |
+
from smolagents.agents import PromptTemplates, EMPTY_PROMPT_TEMPLATES
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 28 |
class FirstAgent:
|
| 29 |
### First Agent is the first attempt to develop an agent for the course. ###
|
| 30 |
def __init__(self):
|
| 31 |
+
""" Initializes the FirstAgent with a TransformersModel and a CodeAgent. """
|
| 32 |
model_id = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 33 |
model = TransformersModel(model_id=model_id)
|
| 34 |
|
| 35 |
# Inizializza l'agente
|
| 36 |
+
prompt_templates = EMPTY_PROMPT_TEMPLATES
|
| 37 |
+
prompt_templates["system_prompt"] = """You are an intelligent agent that answers questions and uses tools to help users.
|
| 38 |
+
Think step by step and use tools when needed.
|
| 39 |
+
If you don't know the answer, say 'I don't know'.
|
| 40 |
+
If you need to search the web, use the WebSearchTool.
|
| 41 |
+
If you need to visit a webpage, use the VisitWebpageTool.
|
| 42 |
+
If you need to run Python code, use the PythonInterpreterTool.
|
| 43 |
+
If you need to generate code to extract information, generate the code and use the PythonInterpreterTool to execute the code and extract the informations.
|
| 44 |
+
If you find information in Wikipedia, use the WikipediaSearchTool.
|
| 45 |
+
Verify your final answer and the format of the final answer before returning it."""
|
| 46 |
+
|
| 47 |
self.agent = CodeAgent(
|
| 48 |
model=model,
|
| 49 |
+
prompt_templates=prompt_templates,
|
| 50 |
tools=[
|
| 51 |
WebSearchTool(),
|
| 52 |
PythonInterpreterTool(),
|
|
|
|
| 57 |
print("FirstAgent inizializzato.")
|
| 58 |
|
| 59 |
def __call__(self, question: str) -> str:
|
| 60 |
+
""" Runs the agent with the provided question and returns the answer. """
|
| 61 |
print(f"Agent ricevuto domanda (primi 50 char): {question[:50]}...")
|
| 62 |
try:
|
| 63 |
answer = self.agent.run(question)
|
| 64 |
print(f"Agent restituisce risposta: {str(answer)[:100]}...")
|
| 65 |
+
return answer
|
| 66 |
except Exception as e:
|
| 67 |
print(f"Errore nell'agente: {e}")
|
| 68 |
return f"Errore nell'agente: {str(e)}"
|