Spaces:
Sleeping
Sleeping
Chat bot with tools
Browse files
src/langgraph_agenticai/graph/graph_builder.py
CHANGED
|
@@ -3,6 +3,8 @@ from langgraph.prebuilt import tools_condition, ToolNode
|
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from src.langgraph_agenticai.state.state import State
|
| 5 |
from src.langgraph_agenticai.nodes.basic_chatbot_node import BasicChatbotNode
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class GraphBuilder:
|
| 8 |
|
|
@@ -24,7 +26,36 @@ class GraphBuilder:
|
|
| 24 |
self.graph_builder.add_node("chatbot", self.basic_chatbot_node.process)
|
| 25 |
self.graph_builder.add_edge(START, "chatbot")
|
| 26 |
self.graph_builder.add_edge("chatbot", END)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def setup_graph(self, usecase: str):
|
| 30 |
"""
|
|
@@ -34,6 +65,6 @@ class GraphBuilder:
|
|
| 34 |
self.basic_chatbot_build_graph()
|
| 35 |
|
| 36 |
if usecase == "Chatbot with Tool":
|
| 37 |
-
|
| 38 |
-
|
| 39 |
return self.graph_builder.compile()
|
|
|
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from src.langgraph_agenticai.state.state import State
|
| 5 |
from src.langgraph_agenticai.nodes.basic_chatbot_node import BasicChatbotNode
|
| 6 |
+
from src.langgraph_agenticai.nodes.chatbot_with_tools_node import ChatbotWithToolsNode
|
| 7 |
+
from src.langgraph_agenticai.tools.search_tool import get_tools, create_tool_node
|
| 8 |
|
| 9 |
class GraphBuilder:
|
| 10 |
|
|
|
|
| 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 |
+
def chatbot_with_tools_build_graph(self):
|
| 31 |
+
"""
|
| 32 |
+
Builds an advanced chatbot graph with tool integration.
|
| 33 |
+
This method creates a chatbot graph that includes both a chatbot node
|
| 34 |
+
and a tool node. It defines tools, initializes the chatbot with tool
|
| 35 |
+
capabilities, and sets up conditional and direct edges between nodes.
|
| 36 |
+
The chatbot node is set as the entry point.
|
| 37 |
+
"""
|
| 38 |
|
| 39 |
+
## Define the tool and tool node
|
| 40 |
+
tools = get_tools()
|
| 41 |
+
tool_node = create_tool_node(tools)
|
| 42 |
+
|
| 43 |
+
## Define the LLM
|
| 44 |
+
llm = self.llm
|
| 45 |
+
|
| 46 |
+
## Define Chatbot node
|
| 47 |
+
obj_chatbot_with_tools_node = ChatbotWithToolsNode(llm)
|
| 48 |
+
chatbot_node = obj_chatbot_with_tools_node.create_chatbot(tools)
|
| 49 |
+
|
| 50 |
+
## Add nodes
|
| 51 |
+
self.graph_builder.add_node("chatbot", chatbot_node)
|
| 52 |
+
self.graph_builder.add_node("tools", tool_node)
|
| 53 |
+
|
| 54 |
+
## define conditional and direct edges
|
| 55 |
+
self.graph_builder.add_edge(START,"chatbot")
|
| 56 |
+
self.graph_builder.add_conditional_edges("chatbot", tools_condition)
|
| 57 |
+
self.graph_builder.add_edge("tools", "chatbot")
|
| 58 |
+
|
| 59 |
|
| 60 |
def setup_graph(self, usecase: str):
|
| 61 |
"""
|
|
|
|
| 65 |
self.basic_chatbot_build_graph()
|
| 66 |
|
| 67 |
if usecase == "Chatbot with Tool":
|
| 68 |
+
self.chatbot_with_tools_build_graph()
|
| 69 |
+
|
| 70 |
return self.graph_builder.compile()
|
src/langgraph_agenticai/nodes/chatbot_with_tools_node.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.langgraph_agenticai.state.state import State
|
| 2 |
+
|
| 3 |
+
class ChatbotWithToolsNode:
|
| 4 |
+
"""
|
| 5 |
+
Chatbot logic enhanced with tool integration.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
def __init__(self, model):
|
| 9 |
+
self.llm = model
|
| 10 |
+
|
| 11 |
+
# def process(self, state: State) -> dict:
|
| 12 |
+
# """
|
| 13 |
+
# Processes the input state and generates a response with tool integration.
|
| 14 |
+
# """
|
| 15 |
+
|
| 16 |
+
# user_input = state["messages"][-1] if state["messages"] else ""
|
| 17 |
+
# llm_response = self.llm.invoke([{"role": "user", "content": user_input}])
|
| 18 |
+
|
| 19 |
+
# # Simulate tool-specific logic
|
| 20 |
+
# tools_response = f"Tool integration for: '{user_input}'"
|
| 21 |
+
|
| 22 |
+
# return {"messages": [llm_response, tools_response]}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def create_chatbot(self, tools):
|
| 26 |
+
"""
|
| 27 |
+
Returns a chatbot node function.
|
| 28 |
+
"""
|
| 29 |
+
llm_with_tools = self.llm.bind_tools(tools)
|
| 30 |
+
|
| 31 |
+
def chatbot_node(state: State):
|
| 32 |
+
"""
|
| 33 |
+
Chatbot logic for processing the input state and returning a response.
|
| 34 |
+
"""
|
| 35 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
| 36 |
+
|
| 37 |
+
return chatbot_node
|
src/langgraph_agenticai/tools/search_tool.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 2 |
+
from langgraph.prebuilt import ToolNode
|
| 3 |
+
|
| 4 |
+
def get_tools():
|
| 5 |
+
|
| 6 |
+
"""
|
| 7 |
+
Returls list of tolls to be used in the Chatbot
|
| 8 |
+
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
tools = [TavilySearchResults(max_results=2)]
|
| 12 |
+
return tools
|
| 13 |
+
|
| 14 |
+
def create_tool_node(tools):
|
| 15 |
+
"""
|
| 16 |
+
creates and returns a tool node for the graph
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
return ToolNode(tools=tools)
|
src/langgraph_agenticai/ui/streamlit_ui/loadui.py
CHANGED
|
@@ -23,7 +23,7 @@ class LoadStreamlitUI:
|
|
| 23 |
}
|
| 24 |
|
| 25 |
def load_streamlit_ui(self):
|
| 26 |
-
st.set_page_config(page_title=self.config.get_page_title()
|
| 27 |
st.header(self.config.get_page_title())
|
| 28 |
st.session_state.timeframe = ''
|
| 29 |
st.session_state.IsFetchButtonClicked = False
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
def load_streamlit_ui(self):
|
| 26 |
+
st.set_page_config(page_title=self.config.get_page_title(), layout="wide")
|
| 27 |
st.header(self.config.get_page_title())
|
| 28 |
st.session_state.timeframe = ''
|
| 29 |
st.session_state.IsFetchButtonClicked = False
|
src/langgraph_agenticai/ui/uiconfigfile.ini
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
[DEFAULT]
|
| 2 |
-
PAGE_TITLE = LangGraph AgenticAI
|
| 3 |
LLM_OPTIONS = Groq, OpenAI
|
| 4 |
-
USECASE_OPTIONS = Basic Chatbot, Chatbot with
|
| 5 |
GROQ_MODEL_OPTIONS = gemma2-9b-it, llama3-8b-8192, llama3-70b-8192
|
|
|
|
| 1 |
[DEFAULT]
|
| 2 |
+
PAGE_TITLE = LangGraph: AgenticAI Application Usecases
|
| 3 |
LLM_OPTIONS = Groq, OpenAI
|
| 4 |
+
USECASE_OPTIONS = Basic Chatbot, Chatbot with Tool, Travel Planner, AI News, SDLC Workflow, Blog Generator, Appointment Receptionist
|
| 5 |
GROQ_MODEL_OPTIONS = gemma2-9b-it, llama3-8b-8192, llama3-70b-8192
|