Frazer2810 commited on
Commit
f732e70
·
verified ·
1 Parent(s): aade277

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +92 -1
agent.py CHANGED
@@ -32,6 +32,97 @@ class GaiaAgent:
32
  api_key=api_key
33
  )
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Istruzioni specifiche per il formato delle risposte
36
  system_prompt = """You are a helpful assistant tasked with answering questions using a set of tools.
37
  Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
@@ -42,11 +133,11 @@ Return ONLY the final answer line."""
42
  # Creazione dell'agente
43
  self.agent = CodeAgent(
44
  model=self.model,
 
45
  system_prompt=system_prompt,
46
  verbose=True # Per logging essenziale
47
  )
48
 
49
- # Configurazione degli strumenti avviene automaticamente in CodeAgent
50
  logger.info("GaiaAgent inizializzato con successo")
51
 
52
  def __call__(self, question: str) -> str:
 
32
  api_key=api_key
33
  )
34
 
35
+ # Definizione dei tools necessari
36
+ @tool("search_wikipedia", "Cerca informazioni su Wikipedia")
37
+ def search_wikipedia(query: str) -> str:
38
+ """
39
+ Cerca informazioni su Wikipedia.
40
+
41
+ Args:
42
+ query: La query di ricerca
43
+
44
+ Returns:
45
+ I risultati della ricerca
46
+ """
47
+ import wikipedia
48
+ try:
49
+ return wikipedia.summary(query, sentences=5)
50
+ except Exception as e:
51
+ return f"Errore nella ricerca su Wikipedia: {str(e)}"
52
+
53
+ @tool("search_web", "Cerca informazioni sul web usando DuckDuckGo")
54
+ def search_web(query: str) -> str:
55
+ """
56
+ Cerca informazioni sul web usando DuckDuckGo.
57
+
58
+ Args:
59
+ query: La query di ricerca
60
+
61
+ Returns:
62
+ I risultati della ricerca
63
+ """
64
+ from duckduckgo_search import DDGS
65
+ try:
66
+ with DDGS() as ddgs:
67
+ results = list(ddgs.text(query, max_results=5))
68
+ return str(results)
69
+ except Exception as e:
70
+ return f"Errore nella ricerca web: {str(e)}"
71
+
72
+ @tool("search_arxiv", "Cerca articoli scientifici su arXiv")
73
+ def search_arxiv(query: str) -> str:
74
+ """
75
+ Cerca articoli scientifici su arXiv.
76
+
77
+ Args:
78
+ query: La query di ricerca
79
+
80
+ Returns:
81
+ I risultati della ricerca
82
+ """
83
+ import arxiv
84
+ try:
85
+ search = arxiv.Search(query=query, max_results=5)
86
+ results = []
87
+ for result in search.results():
88
+ results.append({
89
+ "title": result.title,
90
+ "authors": ", ".join(author.name for author in result.authors),
91
+ "summary": result.summary[:200] + "..." if len(result.summary) > 200 else result.summary
92
+ })
93
+ return str(results)
94
+ except Exception as e:
95
+ return f"Errore nella ricerca su arXiv: {str(e)}"
96
+
97
+ @tool("execute_code", "Esegui codice Python")
98
+ def execute_code(code: str) -> str:
99
+ """
100
+ Esegue codice Python.
101
+
102
+ Args:
103
+ code: Il codice da eseguire
104
+
105
+ Returns:
106
+ Il risultato dell'esecuzione
107
+ """
108
+ import sys
109
+ from io import StringIO
110
+ import traceback
111
+
112
+ old_stdout = sys.stdout
113
+ redirected_output = sys.stdout = StringIO()
114
+
115
+ try:
116
+ exec(code)
117
+ sys.stdout = old_stdout
118
+ return redirected_output.getvalue()
119
+ except Exception as e:
120
+ sys.stdout = old_stdout
121
+ return f"Errore nell'esecuzione del codice: {str(e)}\n{traceback.format_exc()}"
122
+
123
+ # Lista dei tools
124
+ tools = [search_wikipedia, search_web, search_arxiv, execute_code]
125
+
126
  # Istruzioni specifiche per il formato delle risposte
127
  system_prompt = """You are a helpful assistant tasked with answering questions using a set of tools.
128
  Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
 
133
  # Creazione dell'agente
134
  self.agent = CodeAgent(
135
  model=self.model,
136
+ tools=tools,
137
  system_prompt=system_prompt,
138
  verbose=True # Per logging essenziale
139
  )
140
 
 
141
  logger.info("GaiaAgent inizializzato con successo")
142
 
143
  def __call__(self, question: str) -> str: