jjvelezo commited on
Commit
809141b
·
verified ·
1 Parent(s): 64fcc70

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +14 -60
agent.py CHANGED
@@ -1,63 +1,17 @@
1
- import agent_tools
2
- from smolagents import CodeAgent, AzureOpenAIServerModel
3
- import my_tokens
4
 
5
- # Instancia del modelo
6
- model = AzureOpenAIServerModel(
7
- model_id=my_tokens.AZURE_OPENAI_MODEL,
8
- azure_endpoint=my_tokens.AZURE_OPENAI_ENDPOINT,
9
- api_key=my_tokens.AZURE_OPENAI_API_KEY,
10
- api_version=my_tokens.OPENAI_API_VERSION
11
- )
12
-
13
- class CustomAgent:
14
- def __init__(self):
15
- # Definir agentes de trabajo con sus herramientas
16
- self.web_agent = CodeAgent(
17
- model=model,
18
- tools=[agent_tools.google_search, agent_tools.wiki_search, agent_tools.visit_page, agent_tools.final_answer],
19
- max_steps=8,
20
- name="web_agent",
21
- description="Este agente maneja búsquedas web."
22
- )
23
-
24
- self.audio_agent = CodeAgent(
25
- model=model,
26
- tools=[agent_tools.speech_to_text_tool, agent_tools.final_answer],
27
- max_steps=4,
28
- name="audio_agent",
29
- description="Este agente convierte audio a texto."
30
- )
31
-
32
- self.py_agent = CodeAgent(
33
- model=model,
34
- tools=[agent_tools.do_python, agent_tools.final_answer],
35
- additional_authorized_imports=["json", "pandas", "numpy", "regex"],
36
- max_steps=8,
37
- name="python_code_agent",
38
- description="Este agente ejecuta y valida código Python."
39
- )
40
-
41
- self.visual_agent = CodeAgent(
42
- model=model,
43
- tools=[agent_tools.visual_qa_tool, agent_tools.final_answer],
44
- max_steps=4,
45
- name="visual_qa_agent",
46
- description="Este agente responde preguntas sobre imágenes."
47
- )
48
-
49
- self.manager_agent = CodeAgent(
50
- model=model,
51
- tools=[],
52
- managed_agents=[self.web_agent, self.audio_agent, self.py_agent, self.visual_agent],
53
- planning_interval=8,
54
- verbosity_level=2,
55
- max_steps=12,
56
  )
 
57
 
58
- def forward(self, question: str, attachment: str = None) -> str:
59
- if attachment:
60
- result = self.manager_agent.run(question, additional_args={"attachment": attachment})
61
- else:
62
- result = self.manager_agent.run(question)
63
- return result
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer, pipeline
2
+ import torch
 
3
 
4
+ class HuggingFaceAgent:
5
+ def __init__(self, model_id, access_token):
6
+ self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=access_token)
7
+ self.model = AutoModelForCausalLM.from_pretrained(
8
+ model_id,
9
+ token=access_token,
10
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
11
+ device_map="auto"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
+ self.pipeline = pipeline("text-generation", model=self.model, tokenizer=self.tokenizer)
14
 
15
+ def responder(self, prompt):
16
+ respuesta = self.pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)[0]["generated_text"]
17
+ return respuesta[len(prompt):].strip()