working app
Browse files- .gitignore +36 -0
- app.py +40 -0
- backend.py +47 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
*.egg-info/
|
| 8 |
+
*.egg
|
| 9 |
+
|
| 10 |
+
# Virtual Environment
|
| 11 |
+
chatbot-env/
|
| 12 |
+
venv/
|
| 13 |
+
ENV/
|
| 14 |
+
env/
|
| 15 |
+
env.bak/
|
| 16 |
+
|
| 17 |
+
# Environment variables
|
| 18 |
+
.env
|
| 19 |
+
|
| 20 |
+
# IDE specific files
|
| 21 |
+
.idea/
|
| 22 |
+
.vscode/
|
| 23 |
+
*.swp
|
| 24 |
+
*.swo
|
| 25 |
+
|
| 26 |
+
# Distribution / packaging
|
| 27 |
+
dist/
|
| 28 |
+
build/
|
| 29 |
+
*.egg-info/
|
| 30 |
+
|
| 31 |
+
# Jupyter Notebook
|
| 32 |
+
.ipynb_checkpoints
|
| 33 |
+
|
| 34 |
+
# Local development settings
|
| 35 |
+
*.log
|
| 36 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from backend import backend_chat
|
| 3 |
+
from langchain_core.messages import HumanMessage, BaseMessage, AIMessage
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def respond(message: str, history: list[dict]):
|
| 7 |
+
"""
|
| 8 |
+
message: the latest user input
|
| 9 |
+
history: openai-style list of dicts with keys 'role' and 'content'
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
backend_history = []
|
| 13 |
+
for msg in history:
|
| 14 |
+
if msg["role"] == "user":
|
| 15 |
+
backend_history.append(HumanMessage(content=msg["content"]))
|
| 16 |
+
elif msg["role"] == "assistant":
|
| 17 |
+
backend_history.append(AIMessage(content=msg["content"]))
|
| 18 |
+
|
| 19 |
+
updated_backend_history = backend_chat(backend_history, message)
|
| 20 |
+
latest_assistant = None
|
| 21 |
+
for msg in reversed(updated_backend_history):
|
| 22 |
+
if not isinstance(msg, HumanMessage):
|
| 23 |
+
latest_assistant = msg
|
| 24 |
+
break
|
| 25 |
+
|
| 26 |
+
if latest_assistant is None:
|
| 27 |
+
return "Sorry, I couldn't generate a response."
|
| 28 |
+
return latest_assistant.content
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
demo = gr.ChatInterface(
|
| 32 |
+
fn=respond,
|
| 33 |
+
type="messages",
|
| 34 |
+
title="ChatBot",
|
| 35 |
+
description="Chatbot using langgraph backend and Memory Checkpointing",
|
| 36 |
+
)
|
| 37 |
+
demo.launch()
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
main()
|
backend.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# backend.py
|
| 2 |
+
|
| 3 |
+
from langgraph.graph import StateGraph, START, END
|
| 4 |
+
from typing import TypedDict, Annotated, List
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from langgraph.checkpoint.memory import InMemorySaver
|
| 7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
+
from langchain_core.messages import BaseMessage, HumanMessage
|
| 9 |
+
from langgraph.graph.message import add_messages
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
THREAD_ID = "1"
|
| 13 |
+
|
| 14 |
+
gen_llm = ChatGoogleGenerativeAI(model='gemini-2.5-flash')
|
| 15 |
+
|
| 16 |
+
class ChatBotState(TypedDict):
|
| 17 |
+
messages: Annotated[List[BaseMessage], add_messages]
|
| 18 |
+
|
| 19 |
+
def chat_node(state: ChatBotState) -> ChatBotState:
|
| 20 |
+
messages = state['messages']
|
| 21 |
+
response = gen_llm.invoke(messages)
|
| 22 |
+
# depending on design, maybe response returns BaseMessage or list
|
| 23 |
+
return {'messages': [response]}
|
| 24 |
+
|
| 25 |
+
# build graph
|
| 26 |
+
graph = StateGraph(ChatBotState)
|
| 27 |
+
graph.add_node('chat_node', chat_node)
|
| 28 |
+
graph.add_edge(START, 'chat_node')
|
| 29 |
+
graph.add_edge('chat_node', END)
|
| 30 |
+
checkpointer = InMemorySaver()
|
| 31 |
+
chatbot = graph.compile(checkpointer=checkpointer)
|
| 32 |
+
|
| 33 |
+
def backend_chat(history: List[BaseMessage], user_text: str) -> List[BaseMessage]:
|
| 34 |
+
if history is None:
|
| 35 |
+
history = []
|
| 36 |
+
history.append(HumanMessage(content=user_text))
|
| 37 |
+
state = {'messages': history}
|
| 38 |
+
config = {
|
| 39 |
+
"configurable": {
|
| 40 |
+
"thread_id": THREAD_ID
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
result_state = chatbot.invoke(state, config=config)
|
| 44 |
+
new_messages = result_state.get('messages', [])
|
| 45 |
+
# append new messages (assistant responses) to history
|
| 46 |
+
history.extend(new_messages)
|
| 47 |
+
return history
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langgraph
|
| 2 |
+
langchain
|
| 3 |
+
langchain-google-genai
|
| 4 |
+
python-dotenv
|
| 5 |
+
gradio
|