File size: 2,309 Bytes
3ac10a0
f66e0c0
 
 
 
3ac10a0
f66e0c0
 
3ac10a0
062ea41
f66e0c0
 
3ac10a0
 
f66e0c0
 
 
 
062ea41
f66e0c0
062ea41
 
 
 
 
 
 
 
f66e0c0
3ac10a0
 
 
 
 
 
 
 
 
 
 
f66e0c0
3ac10a0
f66e0c0
 
 
 
 
 
 
 
 
3ac10a0
f66e0c0
 
 
3ac10a0
 
 
 
 
 
 
 
 
 
 
 
 
f66e0c0
3ac10a0
 
408d741
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI 

# 1. Initialize the search tool (for internal use by our custom tool only)
duckduckgo_tool = DuckDuckGoSearchTool()

# 2. Define Custom Tool
# --- Herramientas personalizadas (Solo queda la de compositores) ---
@tool
def get_composer_lifespan_from_web(composer_lastname: str) -> str:
    """
    Busca información sobre un compositor y devuelve los datos crudos para que el agente los procese.
    
    Args:
        composer_lastname: El apellido del compositor (p. ej., 'Beethoven', 'Mozart').
    """
    global duckduckgo_tool
    
    # CRITICAL CHANGE: Use a shorter, less restrictive query.
    # The original was: "classical composer {composer_lastname} full name birth and death year"
    # The new one is simply the composer's name.
    query = f"{composer_lastname} composer lifespan"
    
    # Use .forward() instead of the direct call if available, or just the direct call
    # depending on the smolagents version. Assuming .forward() from the previous correction.
    search_result = duckduckgo_tool.forward(query) 
    
    return f"""
    RESULTADOS DE BÚSQUEDA:
    {search_result}
    
    INSTRUCCIÓN OBLIGATORIA PARA EL AGENTE:
    Usa la información de arriba para responder ÚNICAMENTE con este formato exacto:
    
    Nombre: [Nombre Completo]
    Fecha de nacimiento: [Año]
    Fecha de fallecimiento: [Año]
    """

# 3. Setup Agent
final_answer = FinalAnswerTool()

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# Load prompts
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    # CRITICAL FIX: Removed 'duckduckgo_tool' from this list. 
    # Now the agent MUST use 'get_composer_lifespan_from_web'.
    tools=[final_answer, get_composer_lifespan_from_web], 
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# 4. Launch UI
# Removed 'title' argument to prevent the previous crash
GradioUI(agent).launch()