ankitpatil3003/Basic-AI-Chatbot-using-Langgraph
1
1from langgraph.graph import StateGraph, START,END, MessagesState2from langgraph.prebuilt import tools_condition,ToolNode3from langchain_core.prompts import ChatPromptTemplate4from src.langgraphagenticai.state.state import State5from src.langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode6from src.langgraphagenticai.nodes.chatbot_with_Tool_node import ChatbotWithToolNode7from src.langgraphagenticai.tools.serach_tool import get_tools,create_tool_node8 9 10 11 12class GraphBuilder:13 14 def __init__(self,model):15 self.llm=model16 self.graph_builder=StateGraph(State)17 18 def basic_chatbot_build_graph(self):19 """20 Builds a basic chatbot graph using LangGraph.21 This method initializes a chatbot node using the `BasicChatbotNode` class 22 and integrates it into the graph. The chatbot node is set as both the 23 entry and exit point of the graph.24 """25 self.basic_chatbot_node=BasicChatbotNode(self.llm)26 self.graph_builder.add_node("chatbot",self.basic_chatbot_node.process)27 self.graph_builder.add_edge(START,"chatbot")28 self.graph_builder.add_edge("chatbot",END)29 30 31 def chatbot_with_tools_build_graph(self):32 """33 Builds an advanced chatbot graph with tool integration.34 This method creates a chatbot graph that includes both a chatbot node 35 and a tool node. It defines tools, initializes the chatbot with tool 36 capabilities, and sets up conditional and direct edges between nodes. 37 The chatbot node is set as the entry point.38 """39 ## Define the tool and tool node40 41 tools=get_tools()42 tool_node=create_tool_node(tools)43 44 ##Define LLM45 llm = self.llm46 47 # Define chatbot node48 obj_chatbot_with_node = ChatbotWithToolNode(llm)49 chatbot_node = obj_chatbot_with_node.create_chatbot(tools)50 51 # Add nodes52 self.graph_builder.add_node("chatbot", chatbot_node)53 self.graph_builder.add_node("tools", tool_node)54 55 # Define conditional and direct edges56 self.graph_builder.add_edge(START,"chatbot")57 self.graph_builder.add_conditional_edges("chatbot", tools_condition)58 self.graph_builder.add_edge("tools","chatbot")59 60 61 62 63 def setup_graph(self, usecase: str):64 """65 Sets up the graph for the selected use case.66 """67 if usecase == "Basic Chatbot":68 self.basic_chatbot_build_graph()69 70 if usecase == "Chatbot with Tool":71 self.chatbot_with_tools_build_graph()72 return self.graph_builder.compile()