File size: 2,671 Bytes
758f7c7
 
01ba5f6
758f7c7
 
 
 
 
42778a4
758f7c7
 
 
 
e683d4e
1394900
758f7c7
ded9d01
cdcbc72
af04a70
cdcbc72
 
9a4d131
c1581ff
9a4d131
 
 
 
 
 
 
 
 
 
 
 
 
758f7c7
ded9d01
758f7c7
 
9a4d131
758f7c7
 
 
 
9a4d131
 
758f7c7
 
 
 
 
 
 
 
 
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
import os
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_groq import ChatGroq
from langgraph.graph import START, StateGraph, MessagesState
from langgraph.prebuilt import tools_condition
from langgraph.prebuilt import ToolNode
from langchain_core.messages import SystemMessage, HumanMessage
from tools import *
from typing import TypedDict

tools = [add, substract, multiply, divide, web_search]

def build_agent():
    llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
    chat_with_tools = llm.bind_tools(tools)

    def assistant(state: MessagesState):
        return {
            "messages": [chat_with_tools.invoke(state["messages"])],
        }

    def enhancer(state: MessagesState):
        sys_msg = """
                            You are a helpful assistant tasked with answering questions using a set of tools. 
                            Now, 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.
                            Your answer should only start with "FINAL ANSWER: ", then follows with the answer. 
                        """
        return {
            "messages": [sys_msg] + state["messages"]
        }

    ## The graph
    builder = StateGraph(MessagesState)
    
    # Define nodes: these do the work
    builder.add_node("enhancer", enhancer)
    builder.add_node("assistant", assistant)
    builder.add_node("tools", ToolNode(tools))
    
    # Define edges: these determine how the control flow moves
    builder.add_edge(START, "enhancer")
    builder.add_edge("enhancer", "assistant")
    builder.add_conditional_edges(
        "assistant",
        # If the latest message requires a tool, route to tools
        # Otherwise, provide a direct response
        tools_condition,
    )
    builder.add_edge("tools", "assistant")

    return builder.compile()