WilliamRabuel commited on
Commit
561d7fe
·
verified ·
1 Parent(s): 374cba8

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +10 -9
agent.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import json
3
  from smolagents import MultiStepAgent, PythonInterpreterTool
4
  from smolagents.agents import ActionOutput
 
5
  from models import ModelManager
6
  from tools import search_web, scrape_website, read_file
7
 
@@ -86,27 +87,26 @@ If you have the final answer, respond with an empty plan and the answer in the '
86
  memory_step.tool_calls = tool_calls
87
  yield tool_call
88
 
89
- # On exécute l'outil
90
- tool_output_value = self.execute_tool_call(tool_call.name, tool_call.arguments)
91
 
92
- # On vérifie si c'est la réponse finale
93
- if tool_call.name == "final_answer":
 
94
  final_answer = tool_output_value
95
  is_final = True
96
 
97
- # On génère une observation textuelle pour la prochaine étape de l'IA
98
  observation = self.render_tool_result(tool_output_value)
99
  memory_step.observations = observation
100
 
101
  yield {"tool_call_id": tool_call.id, "output": observation}
102
 
103
- # 5. Renvoyer le résultat final de l'étape
104
  yield ActionOutput(output=final_answer, is_final_answer=is_final)
105
 
106
 
107
  def execute_tool_call(self, tool_name: str, arguments: dict) -> any:
108
  """
109
- Exécute un outil avec les arguments fournis. C'est la fonction qui manquait.
110
  """
111
  if tool_name not in self.tools:
112
  raise AgentToolExecutionError(f"Outil inconnu '{tool_name}'. Les outils disponibles sont : {', '.join(self.tools.keys())}", self.logger)
@@ -114,11 +114,12 @@ If you have the final answer, respond with an empty plan and the answer in the '
114
  tool = self.tools[tool_name]
115
 
116
  try:
117
- # La plupart des outils attendent les arguments directement
 
118
  return tool(**arguments)
119
  except TypeError:
120
- # Certains outils plus simples attendent un seul argument positionnel
121
  try:
 
122
  return tool(list(arguments.values())[0])
123
  except Exception as e:
124
  raise AgentToolExecutionError(f"Erreur lors de l'exécution de l'outil '{tool_name}' avec les arguments {arguments}: {e}", self.logger)
 
2
  import json
3
  from smolagents import MultiStepAgent, PythonInterpreterTool
4
  from smolagents.agents import ActionOutput
5
+ from smolagents.utils import AgentToolExecutionError
6
  from models import ModelManager
7
  from tools import search_web, scrape_website, read_file
8
 
 
87
  memory_step.tool_calls = tool_calls
88
  yield tool_call
89
 
90
+ tool_name = tool_call.function.name
91
+ tool_arguments = tool_call.function.arguments
92
 
93
+ tool_output_value = self.execute_tool_call(tool_name, tool_arguments)
94
+
95
+ if tool_name == "final_answer":
96
  final_answer = tool_output_value
97
  is_final = True
98
 
 
99
  observation = self.render_tool_result(tool_output_value)
100
  memory_step.observations = observation
101
 
102
  yield {"tool_call_id": tool_call.id, "output": observation}
103
 
 
104
  yield ActionOutput(output=final_answer, is_final_answer=is_final)
105
 
106
 
107
  def execute_tool_call(self, tool_name: str, arguments: dict) -> any:
108
  """
109
+ Exécute un outil avec les arguments fournis.
110
  """
111
  if tool_name not in self.tools:
112
  raise AgentToolExecutionError(f"Outil inconnu '{tool_name}'. Les outils disponibles sont : {', '.join(self.tools.keys())}", self.logger)
 
114
  tool = self.tools[tool_name]
115
 
116
  try:
117
+ if arguments is None:
118
+ arguments = {}
119
  return tool(**arguments)
120
  except TypeError:
 
121
  try:
122
+ # Gère le cas où l'outil attend un seul argument
123
  return tool(list(arguments.values())[0])
124
  except Exception as e:
125
  raise AgentToolExecutionError(f"Erreur lors de l'exécution de l'outil '{tool_name}' avec les arguments {arguments}: {e}", self.logger)