Spaces:
Sleeping
Sleeping
Kalyanbrata Maity commited on
Commit ·
637fd0b
1
Parent(s): 203b254
added graph builder, states, nodes
Browse files
src/langgraphagenticai/graph/graph_builder.py
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.graph import StateGraph, START, END, MessagesState
|
| 2 |
+
from langgraph.prebuilt import tools_condition, tool_node
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
from src.langgraphagenticai.state.state import State
|
| 5 |
+
from src.langgraphagenticai.nodes.basic_chatbot_node import BasicChatbotNode
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class GraphBuilder:
|
| 9 |
+
def __init__(self, model):
|
| 10 |
+
self.llm = model
|
| 11 |
+
self.graph_builder = StateGraph(State)
|
| 12 |
+
|
| 13 |
+
def basic_chatbot_build_graph(self):
|
| 14 |
+
"""
|
| 15 |
+
Builds a basic chatbot graph using LangGraph.
|
| 16 |
+
This method initializes a chatbot node using the `BasicChatbotNode` class
|
| 17 |
+
and integrates it into the graph. The chatbot node is set as both the
|
| 18 |
+
entry and exi point of the graph.
|
| 19 |
+
"""
|
| 20 |
+
self.basic_chatbot_node = BasicChatbotNode(self.llm)
|
| 21 |
+
self.graph_builder.add_node("chatbot", self.basic_chatbot_node.process)
|
| 22 |
+
self.graph_builder.add_edge(START, "chatbot")
|
| 23 |
+
self.graph_builder.add_edge("chatbot", END)
|
| 24 |
+
|
| 25 |
+
def setup_graph(self, usecase: str):
|
| 26 |
+
"""
|
| 27 |
+
Sets up the graph for the selected use case.
|
| 28 |
+
"""
|
| 29 |
+
if usecase == "Basic Chatbot":
|
| 30 |
+
self.basic_chatbot_build_graph()
|
| 31 |
+
return self.graph_builder.compile()
|
src/langgraphagenticai/main.py
CHANGED
|
@@ -3,6 +3,7 @@ import json
|
|
| 3 |
|
| 4 |
from src.langgraphagenticai.ui.streamlit.load_ui import LoadStreamlitUI
|
| 5 |
from src.langgraphagenticai.llms.groq_llm import GroqChatLLM
|
|
|
|
| 6 |
|
| 7 |
# MAIN function start
|
| 8 |
def load_langgraph_agenticai_app():
|
|
@@ -45,6 +46,11 @@ def load_langgraph_agenticai_app():
|
|
| 45 |
|
| 46 |
## Graph builder
|
| 47 |
graph_builder = GraphBuilder(model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
raise ValueError(f"Error Occurred with Exception: {e}")
|
|
|
|
| 3 |
|
| 4 |
from src.langgraphagenticai.ui.streamlit.load_ui import LoadStreamlitUI
|
| 5 |
from src.langgraphagenticai.llms.groq_llm import GroqChatLLM
|
| 6 |
+
from src.langgraphagenticai.graph.graph_builder import GraphBuilder
|
| 7 |
|
| 8 |
# MAIN function start
|
| 9 |
def load_langgraph_agenticai_app():
|
|
|
|
| 46 |
|
| 47 |
## Graph builder
|
| 48 |
graph_builder = GraphBuilder(model)
|
| 49 |
+
try:
|
| 50 |
+
graph = graph_builder.setup_graph(usecase)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
st.error(f"Error: Graph setup failed - {e}")
|
| 53 |
+
return
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
raise ValueError(f"Error Occurred with Exception: {e}")
|
src/langgraphagenticai/nodes/basic_chatbot_node.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.langgraphagenticai.state.state import State
|
| 2 |
+
|
| 3 |
+
class BasicChatbotNode:
|
| 4 |
+
"""
|
| 5 |
+
Basic chatbot logic implementation
|
| 6 |
+
"""
|
| 7 |
+
def __init__(self, model):
|
| 8 |
+
self.llm = model
|
| 9 |
+
|
| 10 |
+
def process(self, state: State) -> dict:
|
| 11 |
+
"""
|
| 12 |
+
Processes the input state and generates a chatbot response.
|
| 13 |
+
"""
|
| 14 |
+
return {"message": self.llm.invoke(state['messages'])}
|
src/langgraphagenticai/state/state.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Annotated, Literal, Optional
|
| 2 |
+
from typing_extensions import TypedDict
|
| 3 |
+
from langgraph.graph.message import add_messages
|
| 4 |
+
from typing import TypedDict, Annotated, List
|
| 5 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 6 |
+
|
| 7 |
+
class State(TypedDict):
|
| 8 |
+
"""
|
| 9 |
+
Represents the structure of the state used in the graph.
|
| 10 |
+
"""
|
| 11 |
+
messages: Annotated[list, add_messages]
|