File size: 2,684 Bytes
42b1b7d
 
 
 
 
e151f47
6846bc6
 
972142f
42b1b7d
 
 
 
 
 
 
 
e151f47
 
 
 
 
 
 
 
 
 
 
 
6846bc6
 
 
 
 
 
 
 
 
 
267e42e
6846bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
919bc51
7ef2cc7
6846bc6
 
e151f47
 
 
 
 
 
7864f36
6846bc6
e151f47
09b68c9
306f0be
 
e151f47
42b1b7d
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
from langgraph.graph import StateGraph,START,END,MessagesState
from langgraph.prebuilt import tools_condition,ToolNode
from langchain_core.prompts import ChatPromptTemplate
import datetime
from src.langgraphagenticai.state.state import State
from src.langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode
from src.langgraphagenticai.tools.search_tool import get_tools,create_tool_node
from src.langgraphagenticai.nodes.chatbot_with_tools_node import ChatbotWithToolNode
from IPython.display import display,Image

class GraphBuilder:

    def __init__(self,model):
        self.llm = model
        self.graph_builder = StateGraph(State)

    def basic_chatbot_build_graph(self):
        """ 
        Builds a basic chatbot Graph using LangGraph.
        This method initializes a chatbot node using the 'BasicChatbotNode' class
        and integrates into the graph.The chatbot node is set as both the entry 
        entry and exit point of graph

        """
        self.basic_chatbot_node = BasicChatbotNode(self.llm)
        self.graph_builder.add_node("chatbot",self.basic_chatbot_node.process)
        self.graph_builder.add_edge(START,"chatbot")
        self.graph_builder.add_edge("chatbot",END)


    def chatbot_with_tools_build_graph(self):
        """ 
        Builds an advanced chatbot graph with tool integration.
        This method creates a chatbot graph that includes both a chatbot node 
        and a tool node. It defines tools, initializes the chatbot with tool 
        capabilities, and sets up conditional and direct edges between nodes. 
        The chatbot node is set as the entry point.
        """
        tools = get_tools()
        tool_node = create_tool_node(tools)

        # Define LLM
        llm = self.llm

        # Define chatbot node
        obj_chatbot_with_node = ChatbotWithToolNode(model=llm)
        chatbot_node = obj_chatbot_with_node.create_chatbot(tools)

        # Add nodes
        self.graph_builder.add_node("chatbot",chatbot_node)
        self.graph_builder.add_node("tools",tool_node)

        # Define direct and conditional edges
        self.graph_builder.add_edge(START,"chatbot")
        self.graph_builder.add_conditional_edges("chatbot",tools_condition)                                   
        self.graph_builder.add_edge("tools","chatbot")


    def setup_graph(self,usecase:str):
        """ 
        Sets up the graph for the selected use cases
        """
        if usecase == "Basic Chatbot":
            self.basic_chatbot_build_graph()
        elif usecase == "Chatbot with Tool":
            self.chatbot_with_tools_build_graph()

        
       
        return self.graph_builder.compile()