Spaces:
Sleeping
Sleeping
Suraj Yadav
commited on
Commit
·
3de25ad
1
Parent(s):
0a518ff
Chatbot with tools is completed
Browse files- src/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/__pycache__/main.cpython-311.pyc +0 -0
- src/basicchatbot/graph/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/graph/__pycache__/graph_builder.cpython-311.pyc +0 -0
- src/basicchatbot/graph/graph_builder.py +61 -16
- src/basicchatbot/llms/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/llms/__pycache__/base_llm.cpython-311.pyc +0 -0
- src/basicchatbot/llms/__pycache__/groq_llm.cpython-311.pyc +0 -0
- src/basicchatbot/llms/__pycache__/openai_llm.cpython-311.pyc +0 -0
- src/basicchatbot/main.py +3 -0
- src/basicchatbot/nodes/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/nodes/__pycache__/basic_chatbot_node.cpython-311.pyc +0 -0
- src/basicchatbot/nodes/__pycache__/chatbot_with_tools.cpython-311.pyc +0 -0
- src/basicchatbot/state/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/state/__pycache__/state.cpython-311.pyc +0 -0
- src/basicchatbot/tools/__init__.py +3 -1
- src/basicchatbot/tools/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/tools/__pycache__/tool_manager.cpython-311.pyc +0 -0
- src/basicchatbot/tools/__pycache__/tools_initializer.cpython-311.pyc +0 -0
- src/basicchatbot/tools/{tools.py → tool_manager.py} +0 -0
- src/basicchatbot/tools/tools_initializer.py +30 -0
- src/basicchatbot/ui/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/ui/__pycache__/uiconfigfile.cpython-311.pyc +0 -0
- src/basicchatbot/ui/streamlitui/__pycache__/__init__.cpython-311.pyc +0 -0
- src/basicchatbot/ui/streamlitui/__pycache__/display_result.cpython-311.pyc +0 -0
- src/basicchatbot/ui/streamlitui/__pycache__/loadui.cpython-311.pyc +0 -0
- src/basicchatbot/ui/streamlitui/display_result.py +1 -1
- src/basicchatbot/ui/streamlitui/loadui.py +1 -1
- src/basicchatbot/ui/uiconfigfile.ini +1 -1
src/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (178 Bytes). View file
|
|
src/basicchatbot/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (191 Bytes). View file
|
|
src/basicchatbot/__pycache__/main.cpython-311.pyc
ADDED
Binary file (2.44 kB). View file
|
|
src/basicchatbot/graph/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (197 Bytes). View file
|
|
src/basicchatbot/graph/__pycache__/graph_builder.cpython-311.pyc
ADDED
Binary file (5.52 kB). View file
|
|
src/basicchatbot/graph/graph_builder.py
CHANGED
@@ -1,37 +1,82 @@
|
|
1 |
import streamlit as st
|
2 |
from typing import Optional
|
|
|
3 |
from langgraph.graph import START, END, StateGraph
|
4 |
-
from
|
|
|
5 |
from src.basicchatbot.state.state import BasicChatBotState
|
|
|
6 |
|
7 |
|
8 |
class GraphBuilder:
|
|
|
|
|
9 |
def __init__(self, model):
|
10 |
self.llm = model
|
11 |
self.workflow = StateGraph(BasicChatBotState)
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def setup_graph(self, usecase: str) -> Optional[StateGraph]:
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
|
|
|
|
26 |
usecase_mapping = {
|
27 |
-
"Basic Chatbot": self.
|
28 |
-
"Chatbot with Tools": self.
|
29 |
}
|
30 |
|
31 |
build_graph = usecase_mapping.get(usecase)
|
32 |
|
33 |
if build_graph:
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
else:
|
37 |
st.error(f"Invalid use case: {usecase}. Choose from {list(usecase_mapping.keys())}.")
|
|
|
|
1 |
import streamlit as st
|
2 |
from typing import Optional
|
3 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
4 |
from langgraph.graph import START, END, StateGraph
|
5 |
+
from langgraph.prebuilt import tools_condition
|
6 |
+
from src.basicchatbot.nodes import BasicChatBotNode, ChatbotWithToolsNode
|
7 |
from src.basicchatbot.state.state import BasicChatBotState
|
8 |
+
from src.basicchatbot.tools import ToolInitializer
|
9 |
|
10 |
|
11 |
class GraphBuilder:
|
12 |
+
"""Builds different chatbot workflows based on the selected use case."""
|
13 |
+
|
14 |
def __init__(self, model):
|
15 |
self.llm = model
|
16 |
self.workflow = StateGraph(BasicChatBotState)
|
17 |
+
self.tool_initializer = ToolInitializer()
|
18 |
+
|
19 |
+
def _basic_chatbot_build_graph(self) -> None:
|
20 |
+
"""Builds a simple chatbot graph without external tools."""
|
21 |
+
try:
|
22 |
+
self.workflow.add_node("chatbot", BasicChatBotNode(model=self.llm).node)
|
23 |
+
self.workflow.add_edge(START, "chatbot")
|
24 |
+
self.workflow.add_edge("chatbot", END)
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Error in Basic Chatbot Graph: {str(e)}")
|
27 |
+
|
28 |
+
def _chatbot_with_tools_build_graph(self) -> None:
|
29 |
+
"""Builds a chatbot graph with external tool integrations."""
|
30 |
+
try:
|
31 |
+
self.tool_initializer.tool_manager.add_tools( # add comma seperated tools
|
32 |
+
TavilySearchResults(max_results=5)
|
33 |
+
)
|
34 |
+
tools = self.tool_initializer.get_all_tools()
|
35 |
+
if not tools:
|
36 |
+
st.warning("No tools available. The chatbot may not function as expected.")
|
37 |
+
|
38 |
+
tool_node = self.tool_initializer.create_tool_node()
|
39 |
+
|
40 |
+
self.workflow.add_node("chatbot", ChatbotWithToolsNode(model=self.llm, tools=tools).node)
|
41 |
+
self.workflow.add_node("tools", tool_node)
|
42 |
+
|
43 |
+
self.workflow.add_edge(START, "chatbot")
|
44 |
+
self.workflow.add_conditional_edges(
|
45 |
+
"chatbot",
|
46 |
+
tools_condition,
|
47 |
+
{
|
48 |
+
"tools": "tools",
|
49 |
+
"__end__": END
|
50 |
+
}
|
51 |
+
)
|
52 |
+
self.workflow.add_edge("tools", "chatbot")
|
53 |
+
except Exception as e:
|
54 |
+
st.error(f"Error in Chatbot with Tools Graph: {str(e)}")
|
55 |
|
56 |
def setup_graph(self, usecase: str) -> Optional[StateGraph]:
|
57 |
+
"""
|
58 |
+
Sets up the appropriate chatbot graph based on the selected use case.
|
59 |
+
|
60 |
+
Args:
|
61 |
+
usecase (str): The chatbot type ("Basic Chatbot" or "Chatbot with Tools").
|
62 |
|
63 |
+
Returns:
|
64 |
+
Optional[StateGraph]: The compiled chatbot workflow or None if an error occurs.
|
65 |
+
"""
|
66 |
usecase_mapping = {
|
67 |
+
"Basic Chatbot": self._basic_chatbot_build_graph,
|
68 |
+
"Chatbot with Tools": self._chatbot_with_tools_build_graph,
|
69 |
}
|
70 |
|
71 |
build_graph = usecase_mapping.get(usecase)
|
72 |
|
73 |
if build_graph:
|
74 |
+
try:
|
75 |
+
build_graph()
|
76 |
+
return self.workflow.compile()
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"Error compiling the graph: {str(e)}")
|
79 |
+
return None
|
80 |
else:
|
81 |
st.error(f"Invalid use case: {usecase}. Choose from {list(usecase_mapping.keys())}.")
|
82 |
+
return None
|
src/basicchatbot/llms/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (2.25 kB). View file
|
|
src/basicchatbot/llms/__pycache__/base_llm.cpython-311.pyc
ADDED
Binary file (2.42 kB). View file
|
|
src/basicchatbot/llms/__pycache__/groq_llm.cpython-311.pyc
ADDED
Binary file (1.97 kB). View file
|
|
src/basicchatbot/llms/__pycache__/openai_llm.cpython-311.pyc
ADDED
Binary file (2 kB). View file
|
|
src/basicchatbot/main.py
CHANGED
@@ -34,6 +34,9 @@ def load_basic_chatbot():
|
|
34 |
try:
|
35 |
graph_builder = GraphBuilder(model=llm)
|
36 |
graph = graph_builder.setup_graph(usecase)
|
|
|
|
|
|
|
37 |
|
38 |
DisplayResultStreamlit(
|
39 |
usecase=usecase,
|
|
|
34 |
try:
|
35 |
graph_builder = GraphBuilder(model=llm)
|
36 |
graph = graph_builder.setup_graph(usecase)
|
37 |
+
if graph is None:
|
38 |
+
st.error("Error: Graph setup failed.")
|
39 |
+
return
|
40 |
|
41 |
DisplayResultStreamlit(
|
42 |
usecase=usecase,
|
src/basicchatbot/nodes/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (395 Bytes). View file
|
|
src/basicchatbot/nodes/__pycache__/basic_chatbot_node.cpython-311.pyc
ADDED
Binary file (2.01 kB). View file
|
|
src/basicchatbot/nodes/__pycache__/chatbot_with_tools.cpython-311.pyc
ADDED
Binary file (2.67 kB). View file
|
|
src/basicchatbot/state/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (197 Bytes). View file
|
|
src/basicchatbot/state/__pycache__/state.cpython-311.pyc
ADDED
Binary file (786 Bytes). View file
|
|
src/basicchatbot/tools/__init__.py
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
from src.basicchatbot.tools.
|
|
|
|
|
|
1 |
+
from src.basicchatbot.tools.tool_manager import ToolManager
|
2 |
+
from src.basicchatbot.tools.tools_initializer import ToolInitializer
|
3 |
+
|
src/basicchatbot/tools/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (378 Bytes). View file
|
|
src/basicchatbot/tools/__pycache__/tool_manager.cpython-311.pyc
ADDED
Binary file (3.1 kB). View file
|
|
src/basicchatbot/tools/__pycache__/tools_initializer.cpython-311.pyc
ADDED
Binary file (1.82 kB). View file
|
|
src/basicchatbot/tools/{tools.py → tool_manager.py}
RENAMED
File without changes
|
src/basicchatbot/tools/tools_initializer.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Any
|
2 |
+
from langgraph.prebuilt import ToolNode
|
3 |
+
from src.basicchatbot.tools import ToolManager
|
4 |
+
|
5 |
+
|
6 |
+
class ToolInitializer:
|
7 |
+
"""Initializes and manages multiple tools using ToolManager."""
|
8 |
+
|
9 |
+
def __init__(self):
|
10 |
+
"""Initialize ToolInitializer with a ToolManager instance."""
|
11 |
+
self.tool_manager = ToolManager()
|
12 |
+
|
13 |
+
def get_all_tools(self) -> List[Any]:
|
14 |
+
"""
|
15 |
+
Retrieve all tools currently added to the manager.
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
List[Any]: A list of all registered tools.
|
19 |
+
"""
|
20 |
+
return self.tool_manager.get_tools()
|
21 |
+
|
22 |
+
def create_tool_node(self) -> ToolNode:
|
23 |
+
"""
|
24 |
+
Create a ToolNode with all registered tools.
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
ToolNode: A LangGraph ToolNode containing all active tools.
|
28 |
+
"""
|
29 |
+
return self.tool_manager.create_tool_node()
|
30 |
+
|
src/basicchatbot/ui/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (194 Bytes). View file
|
|
src/basicchatbot/ui/__pycache__/uiconfigfile.cpython-311.pyc
ADDED
Binary file (4.69 kB). View file
|
|
src/basicchatbot/ui/streamlitui/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (206 Bytes). View file
|
|
src/basicchatbot/ui/streamlitui/__pycache__/display_result.cpython-311.pyc
ADDED
Binary file (6.72 kB). View file
|
|
src/basicchatbot/ui/streamlitui/__pycache__/loadui.cpython-311.pyc
ADDED
Binary file (5.74 kB). View file
|
|
src/basicchatbot/ui/streamlitui/display_result.py
CHANGED
@@ -95,7 +95,7 @@ class DisplayResultStreamlit:
|
|
95 |
def display_result_on_ui(self) -> None:
|
96 |
usecase_handlers = {
|
97 |
"Basic Chatbot": self._handle_basic_chatbot,
|
98 |
-
"Chatbot with
|
99 |
}
|
100 |
|
101 |
handler = usecase_handlers.get(self.usecase)
|
|
|
95 |
def display_result_on_ui(self) -> None:
|
96 |
usecase_handlers = {
|
97 |
"Basic Chatbot": self._handle_basic_chatbot,
|
98 |
+
"Chatbot with Tools": self._handle_chatbot_with_tool,
|
99 |
}
|
100 |
|
101 |
handler = usecase_handlers.get(self.usecase)
|
src/basicchatbot/ui/streamlitui/loadui.py
CHANGED
@@ -88,7 +88,7 @@ class StreamlitUILoader:
|
|
88 |
usecase_options = self.config.get_usecase_options()
|
89 |
self.user_input['selected_usecase'] = st.selectbox(label="Select Usecase",options=usecase_options)
|
90 |
|
91 |
-
if self.user_input['selected_usecase'] == "Chatbot with
|
92 |
self._setup_tavily_configuration()
|
93 |
|
94 |
return self.user_input
|
|
|
88 |
usecase_options = self.config.get_usecase_options()
|
89 |
self.user_input['selected_usecase'] = st.selectbox(label="Select Usecase",options=usecase_options)
|
90 |
|
91 |
+
if self.user_input['selected_usecase'] == "Chatbot with Tools":
|
92 |
self._setup_tavily_configuration()
|
93 |
|
94 |
return self.user_input
|
src/basicchatbot/ui/uiconfigfile.ini
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[DEFAULT]
|
2 |
PAGE_TITLE = Baisc Chatbot with LangGraph
|
3 |
LLM_OPTIONS = Groq, OpenAI
|
4 |
-
USECASE_OPTIONS = Basic Chatbot, Chatbot with
|
5 |
GROQ_MODEL_OPTIONS = qwen-2.5-32b, qwen-2.5-coder-32, deepseek-r1-distill-qwen-32b, gemma2-9b-it, llama3-8b-8192, llama3-70b-8192
|
6 |
OPENAI_MODEL_OPTIONS = gpt-3.5-turbo-0125,gpt-4o-mini-2024-07-18,gpt-4o-2024-08-06,o3-mini-2025-01-31, o1-mini-2024-09-12
|
|
|
1 |
[DEFAULT]
|
2 |
PAGE_TITLE = Baisc Chatbot with LangGraph
|
3 |
LLM_OPTIONS = Groq, OpenAI
|
4 |
+
USECASE_OPTIONS = Basic Chatbot, Chatbot with Tools
|
5 |
GROQ_MODEL_OPTIONS = qwen-2.5-32b, qwen-2.5-coder-32, deepseek-r1-distill-qwen-32b, gemma2-9b-it, llama3-8b-8192, llama3-70b-8192
|
6 |
OPENAI_MODEL_OPTIONS = gpt-3.5-turbo-0125,gpt-4o-mini-2024-07-18,gpt-4o-2024-08-06,o3-mini-2025-01-31, o1-mini-2024-09-12
|