MAmmarr commited on
Commit
2adc70d
·
1 Parent(s): c419a9b

chatbot wiht tools

Browse files
Agentic_chatbot.ipynb DELETED
@@ -1,33 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "id": "dc1df7f3",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": []
10
- }
11
- ],
12
- "metadata": {
13
- "kernelspec": {
14
- "display_name": "Python 3",
15
- "language": "python",
16
- "name": "python3"
17
- },
18
- "language_info": {
19
- "codemirror_mode": {
20
- "name": "ipython",
21
- "version": 3
22
- },
23
- "file_extension": ".py",
24
- "mimetype": "text/x-python",
25
- "name": "python",
26
- "nbconvert_exporter": "python",
27
- "pygments_lexer": "ipython3",
28
- "version": "3.13.5"
29
- }
30
- },
31
- "nbformat": 4,
32
- "nbformat_minor": 5
33
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/langgraph_agentic_ai/UI/streamlitui/__pycache__/display_result.cpython-313.pyc CHANGED
Binary files a/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/display_result.cpython-313.pyc and b/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/display_result.cpython-313.pyc differ
 
src/langgraph_agentic_ai/UI/streamlitui/__pycache__/loadui.cpython-313.pyc CHANGED
Binary files a/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/loadui.cpython-313.pyc and b/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/loadui.cpython-313.pyc differ
 
src/langgraph_agentic_ai/UI/streamlitui/__pycache__/uiconfigfile.cpython-313.pyc CHANGED
Binary files a/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/uiconfigfile.cpython-313.pyc and b/src/langgraph_agentic_ai/UI/streamlitui/__pycache__/uiconfigfile.cpython-313.pyc differ
 
src/langgraph_agentic_ai/UI/streamlitui/display_result.py CHANGED
@@ -22,4 +22,22 @@ class DisplayResultStreamlit:
22
  with st.chat_message("user"):
23
  st.write(user_message)
24
  with st.chat_message("assistant"):
25
- st.write(value["messages"].content)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  with st.chat_message("user"):
23
  st.write(user_message)
24
  with st.chat_message("assistant"):
25
+ st.write(value["messages"].content)
26
+
27
+
28
+ elif usecase=="Chatbot with WebSearch":
29
+ # Prepare state and invoke the graph
30
+ initial_state = {"messages": [user_message]}
31
+ res = graph.invoke(initial_state)
32
+ for message in res['messages']:
33
+ if type(message) == HumanMessage:
34
+ with st.chat_message("user"):
35
+ st.write(message.content)
36
+ elif type(message)==ToolMessage:
37
+ with st.chat_message("ai"):
38
+ st.write("Tool Call Start")
39
+ st.write(message.content)
40
+ st.write("Tool Call End")
41
+ elif type(message)==AIMessage and message.content:
42
+ with st.chat_message("assistant"):
43
+ st.write(message.content)
src/langgraph_agentic_ai/UI/streamlitui/loadui.py CHANGED
@@ -34,4 +34,11 @@ class LoadStreamlitUI:
34
  ## USecase selection
35
  self.user_controls["selected_usecase"]=st.selectbox("Select Usecases",usecase_options)
36
 
 
 
 
 
 
 
 
37
  return self.user_controls
 
34
  ## USecase selection
35
  self.user_controls["selected_usecase"]=st.selectbox("Select Usecases",usecase_options)
36
 
37
+ if self.user_controls["selected_usecase"] == "Chatbot with WebSearch":
38
+ os.environ["TAVILY_API_KEY"]=self.user_controls["TAVILY_API_KEY"]= st.session_state['TAVILY_API_KEY']=st.text_input("TAVILY API KEY",type="password")
39
+
40
+ if not self.user_controls["TAVILY_API_KEY"]:
41
+ st.warning("⚠️ Please enter your TAVILY_API_KEY key to proceed. Don't have? refer : https://app.tavily.com/home")
42
+
43
+
44
  return self.user_controls
src/langgraph_agentic_ai/UI/streamlitui/uiconfigfile.ini CHANGED
@@ -1,5 +1,5 @@
1
  [DEFAULT]
2
  PAGE_TITLE= LangGraph: Build stateful Agentic Ai graph
3
  LLM_OPTIONS= Groq
4
- USECASE_OPTIONS= Basic Chatbot, Chatbot with Tool, AI News, Blog Generator
5
  GROQ_MODEL_OPTIONS= deepseek-r1-distill-llama-70b, gemma2-9b-it, llama-3.1-8b-instant
 
1
  [DEFAULT]
2
  PAGE_TITLE= LangGraph: Build stateful Agentic Ai graph
3
  LLM_OPTIONS= Groq
4
+ USECASE_OPTIONS= Basic Chatbot, Chatbot with WebSearch, AI News, Blog Generator
5
  GROQ_MODEL_OPTIONS= deepseek-r1-distill-llama-70b, gemma2-9b-it, llama-3.1-8b-instant
src/langgraph_agentic_ai/graph/__pycache__/graph_builder.cpython-313.pyc CHANGED
Binary files a/src/langgraph_agentic_ai/graph/__pycache__/graph_builder.cpython-313.pyc and b/src/langgraph_agentic_ai/graph/__pycache__/graph_builder.cpython-313.pyc differ
 
src/langgraph_agentic_ai/graph/graph_builder.py CHANGED
@@ -1,7 +1,13 @@
1
  from langgraph.graph import StateGraph
 
2
  from src.langgraph_agentic_ai.state.state import State
3
  from langgraph.graph import START,END
4
  from src.langgraph_agentic_ai.nodes.basic_chatbot_node import BasicChatbotNode
 
 
 
 
 
5
 
6
  class GraphBuilder:
7
  def __init__(self,model):
@@ -26,6 +32,33 @@ class GraphBuilder:
26
  self .graph_builder.add_edge(START,"chatbot")
27
  self .graph_builder.add_edge("chatbot",END)
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  def setup_graph(self,usecase:str):
31
  """
@@ -34,6 +67,8 @@ class GraphBuilder:
34
  """
35
  if usecase == "Basic Chatbot":
36
  self.basic_chatbot_build_graph()
 
 
37
 
38
  return self.graph_builder.compile()
39
 
 
1
  from langgraph.graph import StateGraph
2
+ from langgraph.prebuilt import tools_condition
3
  from src.langgraph_agentic_ai.state.state import State
4
  from langgraph.graph import START,END
5
  from src.langgraph_agentic_ai.nodes.basic_chatbot_node import BasicChatbotNode
6
+ from src.langgraph_agentic_ai.tools.search_tool import get_tools,create_tool_node
7
+ from langgraph.prebuilt import tools_condition,ToolNode
8
+ from src.langgraph_agentic_ai.nodes.chatbot_with_tool_node import chatbotwithToolNode
9
+
10
+
11
 
12
  class GraphBuilder:
13
  def __init__(self,model):
 
32
  self .graph_builder.add_edge(START,"chatbot")
33
  self .graph_builder.add_edge("chatbot",END)
34
 
35
+ def chatbot_with_tools_build_graph(self):
36
+ """
37
+ Builds an advanced chatbot graph with tool integration.
38
+ This method creates a chatbot graph that includes both a chatbot node
39
+ and a tool node. It defines tools, initializes the chatbot with tool
40
+ capabilities, and sets up conditional and direct edges between nodes.
41
+ The chatbot node is set as the entry point.
42
+ """
43
+ ## Define the tool and tool node
44
+ tools=get_tools()
45
+ tool_node=create_tool_node(tools)
46
+
47
+ ## Define the LLM
48
+ llm=self.llm
49
+
50
+ ## Define the chatbot node
51
+ obj_chatbot_with_node=chatbotwithToolNode(llm)
52
+ chatbot_node=obj_chatbot_with_node.create_chatbot(tools)
53
+
54
+ ## Add nodes
55
+ self.graph_builder.add_node("chatbot",chatbot_node)
56
+ self.graph_builder.add_node("tools",tool_node)
57
+
58
+ self.graph_builder.add_edge(START,"chatbot")
59
+ self.graph_builder.add_conditional_edges("chatbot",tools_condition)
60
+ self.graph_builder.add_edge("tools","chatbot")
61
+ self.graph_builder.add_edge("chatbot",END)
62
 
63
  def setup_graph(self,usecase:str):
64
  """
 
67
  """
68
  if usecase == "Basic Chatbot":
69
  self.basic_chatbot_build_graph()
70
+ if usecase == "Chatbot with WebSearch":
71
+ self.chatbot_with_tools_build_graph()
72
 
73
  return self.graph_builder.compile()
74
 
src/langgraph_agentic_ai/nodes/__pycache__/chatbot_with_tool_node.cpython-313.pyc ADDED
Binary file (1.99 kB). View file
 
src/langgraph_agentic_ai/nodes/chatbot_with_tool_node.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from src.langgraph_agentic_ai.state.state import State
2
+
3
+ class chatbotwithToolNode:
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
+ Process the input state and generate a response with tool integration
14
+ """
15
+ user_input= state["messages"][-1] if state ["messages"] else ""
16
+ llm_response = self.llm.invoke([{'role':"user","content":user_input}])
17
+
18
+ tools_response = f"Tool integration for '{user_input}"
19
+
20
+ return {"messages":[llm_response,tools_response]}
21
+
22
+
23
+ def create_chatbot(self,tools):
24
+ """
25
+ Returns a chatbot node function
26
+ """
27
+
28
+ llm_with_tools = self.llm.bind_tools(tools)
29
+
30
+ def chatbot_node(state:State):
31
+ """
32
+ Chatbot logic for processing the input state and returning a response
33
+ """
34
+
35
+ return {"messages":[llm_with_tools.invoke(state["messages"])]}
36
+
37
+ return chatbot_node
src/langgraph_agentic_ai/tools/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (201 Bytes). View file
 
src/langgraph_agentic_ai/tools/__pycache__/search_tool.cpython-313.pyc ADDED
Binary file (753 Bytes). View file
 
src/langgraph_agentic_ai/tools/search_tool.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.tools.tavily_search import TavilySearchResults
2
+ from langgraph.prebuilt import ToolNode
3
+
4
+ def get_tools():
5
+ """
6
+ Return the list of tools to be used in the chatbot
7
+ """
8
+ tools=[TavilySearchResults(max_results=2)]
9
+ return tools
10
+
11
+
12
+ def create_tool_node(tools):
13
+ """
14
+ create and returns a tool node for the graph
15
+ """
16
+ return ToolNode(tools=tools)