File size: 4,742 Bytes
6fe4093 ebdbe23 6fe4093 ebdbe23 6fe4093 ebdbe23 6fe4093 939829a 6fe4093 939829a 6fe4093 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
from langgraph.graph import StateGraph
from langgraphagenticai.state.state import State
from langgraph.graph import START,END
from langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode
from langgraphagenticai.tools.search_tool import get_tools,create_tool_node,get_tools_by_usecase
from langgraph.prebuilt import tools_condition,ToolNode
from langgraphagenticai.nodes.chatbot_with_Tool_node import ChatbotWithToolNode
from langgraphagenticai.nodes.ai_news_node import AINewsNode
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 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.
"""
## Define the tool and tool node
tools=get_tools()
tool_node=create_tool_node(tools)
## Define the LLM
llm=self.llm
## Define the chatbot node
obj_chatbot_with_node=ChatbotWithToolNode(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 conditional and direct edges
self.graph_builder.add_edge(START,"chatbot")
self.graph_builder.add_conditional_edges("chatbot",tools_condition)
self.graph_builder.add_edge("tools","chatbot")
# self.graph_builder.add_edge("chatbot",END)
def research_assistant_build_graph(self):
"""
Builds a research assistant graph with ArXiv and web search tools.
This method creates a chatbot graph specifically designed for academic
research, integrating ArXiv search capabilities alongside web search
to provide comprehensive research assistance.
"""
## Define the research tools (ArXiv + Web search)
tools = get_tools_by_usecase("Research Assistant")
tool_node = create_tool_node(tools)
## Define the LLM
llm = self.llm
## Define the chatbot node with research capabilities
obj_chatbot_with_node = ChatbotWithToolNode(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 conditional and direct 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 ai_news_builder_graph(self):
ai_news_node=AINewsNode(self.llm)
## added the nodes
self.graph_builder.add_node("fetch_news",ai_news_node.fetch_news)
self.graph_builder.add_node("summarize_news",ai_news_node.summarize_news)
# self.graph_builder.add_node("save_result",ai_news_node.save_result)
#added the edges
self.graph_builder.set_entry_point("fetch_news")
self.graph_builder.add_edge("fetch_news","summarize_news")
self.graph_builder.add_edge("summarize_news",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()
elif usecase == "Chatbot with Web Search":
self.chatbot_with_tools_build_graph()
elif usecase == "Research Assistant":
self.research_assistant_build_graph()
elif usecase == "AI News":
self.ai_news_builder_graph()
else:
# Default to basic chatbot if usecase is not recognized
self.basic_chatbot_build_graph()
return self.graph_builder.compile() |