File size: 3,553 Bytes
a666d08
f818b63
 
 
58459b2
a173b58
58459b2
a173b58
58459b2
f818b63
 
 
 
 
58459b2
f818b63
58459b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f818b63
a173b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f818b63
58459b2
 
 
 
 
 
 
a173b58
 
 
 
 
58459b2
 
 
 
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
from langgraph.graph import StateGraph, END, START
from langgraph.prebuilt import tools_condition, ToolNode
from src.langgraphagenticai.state.state import State
from src.langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode
from src.langgraphagenticai.nodes.chatbot_with_tool_node import ChatbotWithToolNode
from src.langgraphagenticai.nodes.mcp_chatbot_node import MCPChatbotNode
from src.langgraphagenticai.tools.search_tool import get_tools, create_tool_node
from src.langgraphagenticai.tools.mcp_tools import create_mcp_tools_from_config

class GraphBuilder:
    def __init__(self, model):
        self.llm = model

    def basic_chatbot_build_graph(self):
        graph_builder = StateGraph(State)
        self.basic_chatbot_node = BasicChatbotNode(self.llm)
        graph_builder.add_node("chatbot", self.basic_chatbot_node.process)
        graph_builder.add_edge(START, "chatbot")
        graph_builder.add_edge("chatbot", END)
        return graph_builder

    def chatbot_with_tools_build_graph(self):
        graph_builder = StateGraph(State)

        # define tools and toolnode
        tools = get_tools()
        tool_node = create_tool_node(tools=tools)

        # define llm
        llm = self.llm

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

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

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

        return graph_builder

    def mcp_chatbot_build_graph(self, mcp_config: dict):
        """Build graph for MCP chatbot with MCP tools"""
        graph_builder = StateGraph(State)

        # Create MCP tools from configuration
        mcp_tools = create_mcp_tools_from_config(mcp_config)
        
        if not mcp_tools:
            raise ValueError("No MCP tools could be created from the configuration")

        # Create tool node
        tool_node = ToolNode(tools=mcp_tools)

        # Create MCP chatbot node
        mcp_chatbot_node_obj = MCPChatbotNode(self.llm)
        chatbot_node = mcp_chatbot_node_obj.create_chatbot(tools=mcp_tools)

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

        # Define edges
        graph_builder.add_edge(START, "chatbot")
        graph_builder.add_conditional_edges("chatbot", tools_condition)
        graph_builder.add_edge("tools", "chatbot")

        return graph_builder

    def setup_graph(self, usecase: str, **kwargs):
        if usecase == "Basic Chatbot":
            graph_builder = self.basic_chatbot_build_graph()
        elif usecase == "Chatbot with Tool":
            graph_builder = self.chatbot_with_tools_build_graph()
        elif usecase == "AI News":
            graph_builder = (
                self.chatbot_with_tools_build_graph()
            )  # AI News also uses tools
        elif usecase == "MCP Chatbot":
            mcp_config = kwargs.get("mcp_config")
            if not mcp_config:
                raise ValueError("MCP configuration is required for MCP Chatbot")
            graph_builder = self.mcp_chatbot_build_graph(mcp_config)
        else:
            raise ValueError(f"Unknown usecase: {usecase}")

        return graph_builder.compile()