File size: 1,767 Bytes
553d849
ba9764b
766670a
553d849
f944c3f
553d849
 
 
 
 
766670a
 
553d849
7b404af
 
 
 
 
 
 
553d849
7b404af
553d849
 
7b404af
553d849
 
 
 
 
 
 
 
 
 
 
 
bfd13b1
 
 
 
 
e091a2c
bfd13b1
 
556ab31
4a22da8
bfd13b1
 
 
 
 
 
 
 
 
8ecb459
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
from typing import TypedDict, Annotated
from tool import add, substract, multiply, divide, DuckDuckGoSearchTool, WikipediaSearchTool, ArxivSearchTool, PubmedSearchTool
from os import getenv
from langgraph.graph.message import add_messages
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, AIMessage
from langgraph.graph import StateGraph, START, END, MessagesState
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace


HUGGINGFACEHUB_API_TOKEN = getenv("HUGGINGFACEHUB_API_TOKEN")

# Making the agent
#llm = HuggingFaceEndpoint(
#    repo_id="Qwen/Qwen2.5-Coder-32B-Instruct",
#    huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
#)



llm = HuggingFaceEndpoint(
    repo_id="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
)


chat = ChatHuggingFace(llm=llm, verbose=True)
tools = [add, 
         substract, 
         multiply, 
         divide, 
         DuckDuckGoSearchTool, 
         WikipediaSearchTool, 
         ArxivSearchTool, 
         PubmedSearchTool]

chat_with_tools = chat.bind_tools(tools)

def simple_graph():

    ## Defining our nodes
    def assistant(state: MessagesState):
        """Assistant node"""
        return {"messages": state["messages"] + [chat_with_tools.invoke(state["messages"])]}
    
    # Build graph / nodes
    builder = StateGraph(MessagesState)
    builder.add_node("assistant", assistant) # Assistant
    builder.add_node("tools", ToolNode(tools)) # Tools
    
    # Logic / edges
    builder.add_edge(START, "assistant")
    builder.add_conditional_edges("assistant", tools_condition)
    builder.add_edge("tools", "assistant")
    
    graph = builder.compile()

    return graph