jjvelezo commited on
Commit
b8ac549
·
verified ·
1 Parent(s): 6232aa7

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +98 -49
agent.py CHANGED
@@ -1,52 +1,101 @@
 
 
 
1
  import os
2
- import requests
3
 
4
- class DuckDuckGoAgent:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def __init__(self):
6
- print("DuckDuckGoAgent initialized.")
7
- self.headers = {"User-Agent": "Mozilla/5.0"}
8
-
9
- self.model_config = {
10
- "api_key": os.getenv("HF_API_TOKEN"),
11
- "model_name": "mistralai/Mistral-7B-Instruct-v0.1"
12
- }
13
-
14
- def __call__(self, question: str) -> str:
15
- print(f"Agent received question: {question[:50]}...")
16
- answer = self.call_model_backend(question)
17
- print(f"Agent returning answer: {answer}")
18
- return answer.strip()
19
-
20
- def call_model_backend(self, prompt: str) -> str:
21
- api_key = self.model_config.get("api_key")
22
- model = self.model_config.get("model_name")
23
-
24
- if not api_key or not model:
25
- return "Error: Hugging Face API Token or model not configured."
26
-
27
- url = f"https://api-inference.huggingface.co/models/{model}"
28
- headers = {
29
- "Authorization": f"Bearer {api_key}",
30
- "Content-Type": "application/json"
31
- }
32
- payload = {
33
- "inputs": prompt,
34
- "parameters": {
35
- "max_new_tokens": 200,
36
- "temperature": 0.7
37
- }
38
- }
39
-
40
- try:
41
- response = requests.post(url, headers=headers, json=payload, timeout=30)
42
- response.raise_for_status()
43
- output = response.json()
44
- if isinstance(output, list) and "generated_text" in output[0]:
45
- return output[0]["generated_text"].strip()[:200]
46
- elif isinstance(output, dict) and "error" in output:
47
- return f"HF LLM error: {output['error']}"
48
- else:
49
- return "No response generated from Hugging Face LLM."
50
- except Exception as e:
51
- print(f"Error contacting Hugging Face LLM: {e}")
52
- return "Error contacting Hugging Face model."
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import Tool, DuckDuckGoSearchTool, GoogleSearchTool, VisitWebpageTool, WikipediaSearchTool, PythonInterpreterTool, FinalAnswerTool
2
+ from tavily import TavilyClient
3
+ from dotenv import load_dotenv
4
  import os
 
5
 
6
+ load_dotenv() # Cargar variables de entorno desde .env
7
+
8
+ # Inicializar herramientas directamente en agent.py
9
+ duck_search = DuckDuckGoSearchTool()
10
+ google_search = GoogleSearchTool()
11
+ visit_page = VisitWebpageTool()
12
+ wiki_search = WikipediaSearchTool()
13
+ do_python = PythonInterpreterTool()
14
+ final_answer = FinalAnswerTool()
15
+ tavily_search = TavilyClient()
16
+
17
+ # Herramienta de transcripción de audio a texto
18
+ speech_to_text_tool = Tool.from_space("hf-audio/whisper-large-v3-turbo",
19
+ name="speech_to_text_tool",
20
+ description="""Convierte un archivo de audio a texto. Usa este comando:
21
+ 'speech_to_text_tool(filename)'""",
22
+ api_name="/predict")
23
+
24
+ # Herramienta de QA visual
25
+ visual_qa_tool = Tool.from_space("sitammeur/PicQ",
26
+ name="visual_qa_tool",
27
+ description="""Responde preguntas sobre una imagen proporcionada.
28
+ Usa el comando: visual_qa_tool(question=<pregunta>, image=<nombre_de_imagen>)""",
29
+ api_name="/predict")
30
+
31
+ # Inicializar el modelo de Azure OpenAI
32
+ from smolagents import AzureOpenAIServerModel
33
+ import app_tokens
34
+
35
+ model = AzureOpenAIServerModel(
36
+ model_id=app_tokens.AZURE_OPENAI_MODEL,
37
+ azure_endpoint=app_tokens.AZURE_OPENAI_ENDPOINT,
38
+ api_key=app_tokens.AZURE_OPENAI_API_KEY,
39
+ api_version=app_tokens.OPENAI_API_VERSION
40
+ )
41
+
42
+ class BasicAgent:
43
  def __init__(self):
44
+ # Agente de búsqueda en la web
45
+ self.web_agent = CodeAgent(
46
+ model=model,
47
+ tools=[visit_page, wiki_search, google_search, final_answer],
48
+ max_steps=8,
49
+ name="web_agent",
50
+ description="Este agente realiza búsquedas en la web.",
51
+ add_base_tools=True
52
+ )
53
+
54
+ # Agente de conversión de audio a texto
55
+ self.audio_agent = CodeAgent(
56
+ model=model,
57
+ tools=[speech_to_text_tool, final_answer],
58
+ max_steps=4,
59
+ name="audio_agent",
60
+ description="Este agente convierte audio a texto.",
61
+ add_base_tools=True
62
+ )
63
+
64
+ # Agente para ejecución de código Python
65
+ self.py_agent = CodeAgent(
66
+ model=model,
67
+ tools=[do_python, final_answer],
68
+ additional_authorized_imports=["json", "pandas", "numpy", "regex"],
69
+ max_steps=8,
70
+ name="python_code_agent",
71
+ description="Este agente ejecuta y valida código Python.",
72
+ add_base_tools=True
73
+ )
74
+
75
+ # Agente para análisis de imágenes
76
+ self.visual_agent = CodeAgent(
77
+ model=model,
78
+ tools=[visual_qa_tool, final_answer],
79
+ max_steps=4,
80
+ name="visual_qa_agent",
81
+ description="Este agente responde preguntas sobre imágenes.",
82
+ add_base_tools=True
83
+ )
84
+
85
+ # Agente principal que coordina otros agentes
86
+ self.manager_agent = CodeAgent(
87
+ model=model,
88
+ tools=[],
89
+ managed_agents=[self.web_agent, self.audio_agent, self.py_agent, self.visual_agent],
90
+ planning_interval=8,
91
+ verbosity_level=2,
92
+ max_steps=12,
93
+ add_base_tools=True
94
+ )
95
+
96
+ def forward(self, question: str, attachment: str = None) -> str:
97
+ if attachment:
98
+ result = self.manager_agent.run(question, additional_args={"attachment": attachment})
99
+ else:
100
+ result = self.manager_agent.run(question)
101
+ return result