works
Browse files- src/__pycache__/main.cpython-312.pyc +0 -0
- src/graph/graph_builder.py +38 -0
- src/main.py +66 -15
- src/nodes/basic_chatbot.py +25 -0
- src/state/__init__.py +0 -0
- src/state/state.py +9 -0
- src/ui/__pycache__/config.cpython-312.pyc +0 -0
- src/ui/display_results.py +40 -0
src/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/src/__pycache__/main.cpython-312.pyc and b/src/__pycache__/main.cpython-312.pyc differ
|
|
|
src/graph/graph_builder.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph import START, END, StateGraph
|
| 2 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
import datetime
|
| 5 |
+
from src.state.state import State
|
| 6 |
+
from src.nodes.basic_chatbot import BasicChatbot
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class GraphBuilder:
|
| 10 |
+
|
| 11 |
+
"""Class to build the state graph for the application."""
|
| 12 |
+
|
| 13 |
+
def __init__(self, model):
|
| 14 |
+
self.llm = model
|
| 15 |
+
self.graph_builder = StateGraph(State)
|
| 16 |
+
|
| 17 |
+
def basic_chatbot(self):
|
| 18 |
+
"""Initialize the basic chatbot node in the graph."""
|
| 19 |
+
self.basic_chatbot_node = BasicChatbot(self.llm)
|
| 20 |
+
self.graph_builder.add_node('basic_chatbot', self.basic_chatbot_node.process)
|
| 21 |
+
self.graph_builder.add_edge(START, 'basic_chatbot')
|
| 22 |
+
self.graph_builder.add_edge('basic_chatbot', END)
|
| 23 |
+
|
| 24 |
+
def setup_graph(self, use_case: str):
|
| 25 |
+
"""
|
| 26 |
+
Setup the graph with the appropriate nodes based on use case.
|
| 27 |
+
|
| 28 |
+
:param use_case: The use case for which the graph is being built.
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
if use_case == 'Basic Chatbot':
|
| 32 |
+
self.basic_chatbot()
|
| 33 |
+
else:
|
| 34 |
+
# Default to basic chatbot if use case is not recognized
|
| 35 |
+
self.basic_chatbot()
|
| 36 |
+
|
| 37 |
+
# Compile and return the graph
|
| 38 |
+
return self.graph_builder.compile()
|
src/main.py
CHANGED
|
@@ -3,7 +3,10 @@ import json
|
|
| 3 |
|
| 4 |
from src.ui.load import LoadStreamlitUI
|
| 5 |
from src.llms.groq import GroqLLM
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def load_app():
|
| 9 |
"""
|
|
@@ -17,26 +20,74 @@ def load_app():
|
|
| 17 |
st.error("Failed to load the UI. Please check your configuration.")
|
| 18 |
return
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
return
|
| 34 |
|
| 35 |
-
use_case = user_input.get('
|
| 36 |
|
| 37 |
-
if use_case:
|
| 38 |
-
st.error("Error: No
|
| 39 |
return
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
|
|
|
|
|
|
| 3 |
|
| 4 |
from src.ui.load import LoadStreamlitUI
|
| 5 |
from src.llms.groq import GroqLLM
|
| 6 |
+
from src.llms.openai import OpenAILLM
|
| 7 |
+
from src.graph.graph_builder import GraphBuilder
|
| 8 |
+
from src.ui.display_results import DisplayResults
|
| 9 |
+
from langchain_core.messages import HumanMessage
|
| 10 |
|
| 11 |
def load_app():
|
| 12 |
"""
|
|
|
|
| 20 |
st.error("Failed to load the UI. Please check your configuration.")
|
| 21 |
return
|
| 22 |
|
| 23 |
+
# Initialize chat history in session state
|
| 24 |
+
if "messages" not in st.session_state:
|
| 25 |
+
st.session_state.messages = []
|
| 26 |
+
|
| 27 |
+
# Display chat history
|
| 28 |
+
for message in st.session_state.messages:
|
| 29 |
+
with st.chat_message(message["role"]):
|
| 30 |
+
st.write(message["content"])
|
| 31 |
|
| 32 |
+
# Chat input at the bottom
|
| 33 |
+
if prompt := st.chat_input("Enter your message..."):
|
| 34 |
+
# Add user message to chat history
|
| 35 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 36 |
+
|
| 37 |
+
# Display user message
|
| 38 |
+
with st.chat_message("user"):
|
| 39 |
+
st.write(prompt)
|
| 40 |
|
| 41 |
+
# Process the message
|
| 42 |
+
user_message = prompt
|
| 43 |
try:
|
| 44 |
+
# Get the selected LLM provider from user input
|
| 45 |
+
selected_llm = user_input.get('Selected LLM')
|
| 46 |
+
|
| 47 |
+
# Initialize the appropriate LLM class based on user selection
|
| 48 |
+
if selected_llm == 'Groq':
|
| 49 |
+
llm = GroqLLM(user_controls_input=user_input)
|
| 50 |
+
elif selected_llm == 'OpenAI':
|
| 51 |
+
llm = OpenAILLM(user_controls_input=user_input)
|
| 52 |
+
else:
|
| 53 |
+
st.error("Please select a valid LLM provider (Groq or OpenAI).")
|
| 54 |
+
return
|
| 55 |
|
| 56 |
+
# Get the model instance
|
| 57 |
+
model = llm.get_llm_model()
|
| 58 |
+
|
| 59 |
+
if not model:
|
| 60 |
+
st.error(f"Failed to initialize the {selected_llm} model. Please check your API key and model selection.")
|
| 61 |
return
|
| 62 |
|
| 63 |
+
use_case = user_input.get('Selected Use Case')
|
| 64 |
|
| 65 |
+
if not use_case:
|
| 66 |
+
st.error("Error: No use case selected.")
|
| 67 |
return
|
| 68 |
+
|
| 69 |
+
graph_builder = GraphBuilder(model=model)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
graph = graph_builder.setup_graph(use_case=use_case)
|
| 73 |
+
|
| 74 |
+
# Process the message through the graph
|
| 75 |
+
ai_response = ""
|
| 76 |
+
for event in graph.stream({'messages': [HumanMessage(content=user_message)]}):
|
| 77 |
+
for value in event.values():
|
| 78 |
+
ai_response = value['messages'].content
|
| 79 |
+
|
| 80 |
+
# Add AI response to chat history
|
| 81 |
+
st.session_state.messages.append({"role": "assistant", "content": ai_response})
|
| 82 |
+
|
| 83 |
+
# Display AI response
|
| 84 |
+
with st.chat_message("assistant"):
|
| 85 |
+
st.write(ai_response)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"An error occurred while setting up the graph: {e}")
|
| 89 |
+
return
|
| 90 |
+
|
| 91 |
except Exception as e:
|
| 92 |
+
st.error(f"An error occurred while processing the input: {e}")
|
| 93 |
+
return
|
src/nodes/basic_chatbot.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.state.state import State
|
| 2 |
+
|
| 3 |
+
class BasicChatbot:
|
| 4 |
+
"""
|
| 5 |
+
Class to handle the basic chatbot functionality.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
def __init__(self, model):
|
| 9 |
+
"""
|
| 10 |
+
Initialize the BasicChatbot with the given model.
|
| 11 |
+
|
| 12 |
+
:param model: The LLM to be used for the chatbot.
|
| 13 |
+
"""
|
| 14 |
+
self.model = model
|
| 15 |
+
|
| 16 |
+
def process(self, state):
|
| 17 |
+
"""
|
| 18 |
+
Process the state to generate a response from the model.
|
| 19 |
+
|
| 20 |
+
:param state: The current state of the chatbot.
|
| 21 |
+
:return: The response generated by the model.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
return {'messages': self.model.invoke(state['messages'])}
|
| 25 |
+
|
src/state/__init__.py
ADDED
|
File without changes
|
src/state/state.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Annotated
|
| 2 |
+
from typing_extensions import TypedDict
|
| 3 |
+
from langgraph.graph.message import add_messages
|
| 4 |
+
|
| 5 |
+
class State(TypedDict):
|
| 6 |
+
"""
|
| 7 |
+
State for the graph.
|
| 8 |
+
"""
|
| 9 |
+
messages: Annotated[list, add_messages] # List of messages in the graph
|
src/ui/__pycache__/config.cpython-312.pyc
CHANGED
|
Binary files a/src/ui/__pycache__/config.cpython-312.pyc and b/src/ui/__pycache__/config.cpython-312.pyc differ
|
|
|
src/ui/display_results.py
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class DisplayResults:
|
| 7 |
+
"""Class to display results in the Streamlit UI."""
|
| 8 |
+
def __init__(self, user_message, use_case, graph):
|
| 9 |
+
"""
|
| 10 |
+
Initialize the DisplayResults with user message, use case, and graph.
|
| 11 |
+
|
| 12 |
+
:param user_message: The message input by the user.
|
| 13 |
+
:param use_case: The selected use case for the chatbot.
|
| 14 |
+
:param graph: The state graph to be processed.
|
| 15 |
+
"""
|
| 16 |
+
self.user_message = user_message
|
| 17 |
+
self.use_case = use_case
|
| 18 |
+
self.graph = graph
|
| 19 |
+
|
| 20 |
+
def display_results(self):
|
| 21 |
+
"""
|
| 22 |
+
Display the results in the Streamlit UI.
|
| 23 |
+
|
| 24 |
+
This method processes the user message through the graph and displays the response.
|
| 25 |
+
"""
|
| 26 |
+
use_case = self.use_case
|
| 27 |
+
user_message = self.user_message
|
| 28 |
+
graph = self.graph
|
| 29 |
+
|
| 30 |
+
if use_case == 'Basic Chatbot':
|
| 31 |
+
for event in graph.stream({'messages':("user",user_message)}):
|
| 32 |
+
print(event.values())
|
| 33 |
+
for value in event.values():
|
| 34 |
+
print(value['messages'])
|
| 35 |
+
with st.chat_message("user"):
|
| 36 |
+
st.write(user_message)
|
| 37 |
+
with st.chat_message("assistant"):
|
| 38 |
+
st.write(value["messages"].content)
|
| 39 |
+
|
| 40 |
+
|