| | from langgraph.graph import StateGraph, START,END, MessagesState |
| | from langgraph.prebuilt import tools_condition,ToolNode |
| | from langchain_core.prompts import ChatPromptTemplate |
| | from src.langgraphagenticai.state.state import State |
| | from src.langgraphagenticai.state.bloggenerator_state import BlogGeneratorState |
| | from src.langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode |
| | from src.langgraphagenticai.nodes.chatbot_with_Tool_node import ChatbotWithToolNode |
| | from src.langgraphagenticai.nodes.bloggenerator_chatbot_node import BlogGeneratorNode |
| | from src.langgraphagenticai.tools.search_tool import get_tools,create_tool_node |
| |
|
| |
|
| |
|
| |
|
| | class GraphBuilder: |
| |
|
| | def __init__(self,model): |
| | self.llm=model |
| | self.graph_builder=StateGraph(State) |
| | self.blog_graph_builder=StateGraph(BlogGeneratorState) |
| |
|
| | 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 it into the graph. The chatbot node is set as both the |
| | entry and exit point of the 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) |
| |
|
| | |
| | llm = self.llm |
| |
|
| | |
| | obj_chatbot_with_node = ChatbotWithToolNode(llm) |
| | chatbot_node = obj_chatbot_with_node.create_chatbot(tools) |
| |
|
| | |
| | self.graph_builder.add_node("chatbot", chatbot_node) |
| | self.graph_builder.add_node("tools", tool_node) |
| |
|
| | |
| | self.graph_builder.add_edge(START,"chatbot") |
| | self.graph_builder.add_conditional_edges("chatbot", tools_condition) |
| | self.graph_builder.add_edge("tools","chatbot") |
| |
|
| | def blog_generator_build_graph(self): |
| |
|
| | |
| | llm = self.llm |
| |
|
| | |
| | self.obj_blog_generator_node = BlogGeneratorNode(llm) |
| |
|
| | |
| | self.blog_graph_builder.add_node("chatbot", self.obj_blog_generator_node.process) |
| | self.blog_graph_builder.add_node("titleagent", self.obj_blog_generator_node.titleagent) |
| | self.blog_graph_builder.add_node("blogagent", self.obj_blog_generator_node.blogagent) |
| | self.blog_graph_builder.add_node("finalblogagent", self.obj_blog_generator_node.finalblogagent) |
| |
|
| | |
| | self.blog_graph_builder.add_edge(START, "chatbot") |
| | self.blog_graph_builder.add_edge("chatbot", "titleagent") |
| | self.blog_graph_builder.add_edge("chatbot", "blogagent") |
| | self.blog_graph_builder.add_edge("titleagent", "finalblogagent") |
| | self.blog_graph_builder.add_edge("blogagent", "finalblogagent") |
| | self.blog_graph_builder.add_edge("finalblogagent", END) |
| | |
| | |
| | def setup_graph(self, usecase: str): |
| | """ |
| | Sets up the graph for the selected use case. |
| | """ |
| | if usecase == "Basic Chatbot": |
| | self.basic_chatbot_build_graph() |
| | return self.graph_builder.compile() |
| |
|
| | if usecase == "Chatbot with Tool": |
| | self.chatbot_with_tools_build_graph() |
| | return self.graph_builder.compile() |
| | |
| | if usecase == "Blog Generator Chatbot": |
| | self.blog_generator_build_graph() |
| | return self.blog_graph_builder.compile() |
| | |