File size: 1,840 Bytes
bef7a57
 
 
 
 
 
9e33353
fe0904c
 
29877b5
52874fc
bef7a57
9d5a984
bef7a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe0904c
 
 
bef7a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Agent built with LangGraph.
"""

from typing import TypedDict, Annotated
from langgraph.graph import START, StateGraph, END
from langgraph.graph.message import add_messages
from langchain_huggingface import ChatHuggingFace
from langchain_groq import ChatGroq
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_core.tools import tool
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.messages import AnyMessage
import os

with open("system_prompt.txt") as f:
    SYSTEM_PROMPT = f.read()

@tool 
def add_tool(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

@tool 
def subtract_tool(a: int, b: int) -> int:
    """Subtract two integers."""
    return a - b

@tool 
def multiply_tool(a: int, b: int) -> int:
    """Multiply two integers."""
    return a * b

@tool 
def divide_tool(a: int, b: int) -> int:
    """Divide two integers."""
    return a / b

@tool 
def modulus_tool(a: int, b: int) -> int:
    """Calculate the modulus of two integers."""
    return a % b

search_tool = DuckDuckGoSearchRun()

llm = ChatGroq(
    model="llama3-8b-8192",
    api_key=os.environ.get("GROQ_API_KEY")
)

chat = ChatHuggingFace(llm=llm, verbose=True)
tools = [add_tool, subtract_tool, multiply_tool, divide_tool, modulus_tool, search_tool]
chat_with_tools = chat.bind_tools(tools)

class AgentState(TypedDict):
    messages: Annotated[list[AnyMessage], add_messages]

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

builder = StateGraph(AgentState)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
    "assistant",
    tools_condition,
)
builder.add_edge("tools", "assistant")
graph = builder.compile()