File size: 1,345 Bytes
be995a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/langgraphagenticai/graph/graph_builder_tool.py
from langgraph.graph import StateGraph, START, END
from src.langgraphagenticai.nodes.chatbot_with_Tool_node import ChatbotWithToolNode
from src.langgraphagenticai.tools.search_tool import get_tools, create_tool_nodes
from langgraph.prebuilt import tools_condition
from src.langgraphagenticai.state.state import State
from langgraph.checkpoint.memory import MemorySaver

class ChatbotWithToolGraphBuilder:
    def __init__(self, llm, memory: MemorySaver):
        self.llm = llm
        self.memory = memory

    def build_graph(self):
        """
        Builds a graph for the Chatbot with Tool use case.
        """
        graph_builder = StateGraph(state_schema=State)

        # Define the tool and tool node
        tools = get_tools()
        tool_node = create_tool_nodes(tools)

        # Define chatbot node
        chatbot_with_tool_node = ChatbotWithToolNode(self.llm)
        chatbot_node = chatbot_with_tool_node.create_chatbot(tools)

        graph_builder.add_node("chatbot", chatbot_node)
        graph_builder.add_node("tools", tool_node)

        graph_builder.add_edge(START, "chatbot")
        graph_builder.add_conditional_edges("chatbot", tools_condition)
        graph_builder.add_edge("tools", "chatbot")
        return graph_builder.compile(checkpointer=self.memory)