File size: 2,692 Bytes
a46eb40
 
 
 
 
 
 
5afc603
a46eb40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import CodeAgent, ToolCallingAgent, TransformersModel
from tools import available_tools
import torch
import json
import os


def instantiate_agent(executor_type : str="local", agent_type: str ="tool_calling", tools = available_tools) -> CodeAgent | ToolCallingAgent:
    
    # Локальный агент с моделью кодером
    if executor_type == "local" and agent_type == "code":

        code_system_prompt = os.getenv("CODE_AGENT_SYSTEM_PROMPT")

        print(code_system_prompt)

        hf_model = "Qwen/Qwen2.5-Coder-32B-Instruct"
        # hf_model = "Qwen/Qwen2.5-Coder-7B-Instruct"
        # hf_model = "Qwen/Qwen2.5-Coder-3B-Instruct"   # For debug purpose

        llm = TransformersModel(model_id=hf_model,
                                device_map="cuda",
                                torch_dtype=torch.bfloat16,
                                )
        
        agent = CodeAgent(tools=available_tools,
                          model=llm,
                          additional_authorized_imports=['pandas','numpy', 'numpy.*', 'csv', 'markdownify', 'requests'],
                          prompt_templates=({'system_prompt': code_system_prompt}),
                          max_steps=20,
                          )
        return agent
    
    elif executor_type == "local" and agent_type == "tool_calling":

        tool_call_system_prompt = os.getenv("TOOL_CALLING_SYSTEM_PROMPT")

        hf_model = "Qwen/Qwen2.5-7B-Instruct"
        # hf_model = "Qwen/Qwen2.5-3B-Instruct"         # For debug purpose
        # hf_model = "google/gemma-2-2b-it"
        # hf_model = "Qwen/Qwen2.5-7B-Instruct-1M"

        llm = TransformersModel(model_id=hf_model, device_map="cuda", torch_dtype=torch.bfloat16)

        agent = ToolCallingAgent(tools=available_tools, model=llm, max_steps=3, planning_interval=1)
        agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + tool_call_system_prompt



        return agent 
    
    else:
        raise ValueError(f"Unsupported executor type: {executor_type} or agent type: {agent_type}")


if __name__ == "__main__":

    agent = instantiate_agent()

    question = """
Question: Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.

Tools required:
1. search engine

Approximately, the problem can be solved as follows::
1. Search "Kuznetzov Nedoshivina 2010"
2. Find the 2010 paper "A catalogue of type specimens of the Tortricidae described by V. I. Kuznetzov from Vietnam and deposited in the Zoological Institute, St. Petersburg"
"""
    agent.run(question)