File size: 5,281 Bytes
fc6b400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61c17f1
 
fc6b400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from smolagents import (
    CodeAgent,
    ToolCallingAgent,
    DuckDuckGoSearchTool, 
    InferenceClientModel,
    GoogleSearchTool,
    VisitWebpageTool, 
    tool,
    LiteLLMModel,
)

from opentelemetry.sdk.trace import TracerProvider

from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

trace_provider = TracerProvider()
trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))

SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)


from  tools.mathTools import multiply, add, subtract, divide, modulus
from tools.searchTools import wiki_search, mini_web_search, arvix_search




def check_reasoning(final_answer,agent_memory):
        model = InferenceClientModel("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") #OpenAIServerModel("gpt-4o", max_tokens=8096)
        prompt = (
            f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}."
            "Please check that the reasoning process is correct: do it correctly answer the given task?"
            "Remember answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
            "First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
            
        )
        messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": prompt,
                }
            ],
            }
        ]
        output = model(messages).content
        print("Feedback: ", output)
        if "FAIL" in output:
            raise Exception(output)
        return True

def build_agents():
    
    model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
    
    web_agent = ToolCallingAgent(
            model=model,
            tools=[
                 multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search,
            GoogleSearchTool(provider="serper"),
            VisitWebpageTool(),
            ],
            #add_base_tools=True,
            #additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"],
            name="web_search_agent",
            description="Browses the web to find information",
            verbosity_level=10,
            max_steps=10,
        )
    
    coding_agent = CodeAgent(
        model = model,
        tools=[multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search,],
        additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"],
        name="coding_agent",
        description="A coding agent that can write and execute code to answer questions.",
        verbosity_level=10,
        max_steps=10,
        #prompt="You are a coding agent expert in python. I will ask you a question.",

    )


    agent = CodeAgent(
    model=InferenceClientModel("deepseek-ai/DeepSeek-R1",max_tokens=8096), #InferenceClientModel("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") ,# deepseek-ai/DeepSeek-R1",  max_tokens=8096),
    tools=[multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search,
    ],
    managed_agents=[web_agent,coding_agent],
    additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"],
    planning_interval=5,
    max_steps=10,
    final_answer_checks=[check_reasoning],
    add_base_tools=True,
    )
    prompt = f"""\nYou are a general AI assistant. I will ask you a question.
        Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
        YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
        If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. 
        If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. 
        If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
    
    #print("Before concatenation:")
    #print(agent.prompt_templates["system_prompt"])
    agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + prompt
    #print("After concatenation:")
    #print(agent.prompt_templates["system_prompt"])

    return agent