Repository navigation
Expand file tree
/
Copy pathlangraph_rag_backend.py
More file actions
169 lines (128 loc) · 4.64 KB
/
Copy pathlangraph_rag_backend.py
File metadata and controls
169 lines (128 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import annotations
import os
import sqlite3
import tempfile
from typing import Annotated, Any, Dict, Optional, TypedDict
from langchain_ollama import ChatOllama
from langchain_ollama import OllamaEmbeddings
from dotenv import load_dotenv
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.vectorstores import FAISS
from langchain_core.messages import BaseMessage, SystemMessage
from langchain_core.tools import tool
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.graph import START, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
import requests
load_dotenv()
# -------------------
# 1. LLM + embeddings
# -------------------
llm = ChatOllama(model="llama3")
embeddings = OllamaEmbeddings(model="nomic-embed-text")
# -------------------
# 2. PDF retriever store (per thread)
# -------------------
_THREAD_RETRIEVERS: Dict[str, Any] = {}
_THREAD_METADATA: Dict[str, dict] = {}
def _get_retriever(thread_id: Optional[str]):
"""Fetch the retriever for a thread if available."""
if thread_id and thread_id in _THREAD_RETRIEVERS:
return _THREAD_RETRIEVERS[thread_id]
return None
def ingest_pdf(file_bytes: bytes, thread_id: str, filename: Optional[str] = None) -> dict:
"""
Build a FAISS retriever for the uploaded PDF and store it for the thread.
Returns a summary dict that can be surfaced in the UI.
"""
if not file_bytes:
raise ValueError("No bytes received for ingestion.")
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
temp_file.write(file_bytes)
temp_path = temp_file.name
try:
loader = PyPDFLoader(temp_path)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=200, separators=["\n\n", "\n", " ", ""]
)
chunks = splitter.split_documents(docs)
vector_store = FAISS.from_documents(chunks, embeddings)
retriever = vector_store.as_retriever(
search_type="similarity", search_kwargs={"k": 4}
)
_THREAD_RETRIEVERS[str(thread_id)] = retriever
_THREAD_METADATA[str(thread_id)] = {
"filename": filename or os.path.basename(temp_path),
"documents": len(docs),
"chunks": len(chunks),
}
return {
"filename": filename or os.path.basename(temp_path),
"documents": len(docs),
"chunks": len(chunks),
}
finally:
# The FAISS store keeps copies of the text, so the temp file is safe to remove.
try:
os.remove(temp_path)
except OSError:
pass
# -------------------
# 4. State
# -------------------
class ChatState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
# -------------------
# 5. Nodes
# -------------------
def chat_node(state: ChatState, config=None):
thread_id = None
if config and isinstance(config, dict):
thread_id = config.get("configurable", {}).get("thread_id")
user_message = state["messages"][-1].content
retriever = _get_retriever(thread_id)
context_text = ""
if retriever:
docs = retriever.invoke(user_message)
context_text = "\n\n".join([doc.page_content for doc in docs])
system_prompt = f"""
You are a helpful assistant.
If context is provided below, answer ONLY from that context.
If no context is available, answer normally.
Context:
{context_text}
"""
messages = [
SystemMessage(content=system_prompt),
*state["messages"],
]
response = llm.invoke(messages)
return {"messages": [response]}
# -------------------
# 6. Checkpointer
# -------------------
conn = sqlite3.connect(database="chatbot.db", check_same_thread=False)
checkpointer = SqliteSaver(conn=conn)
# -------------------
# 7. Graph
# -------------------
graph = StateGraph(ChatState)
graph.add_node("chat_node", chat_node)
graph.add_edge(START, "chat_node")
chatbot = graph.compile(checkpointer=checkpointer)
# -------------------
# 8. Helpers
# -------------------
def retrieve_all_threads():
all_threads = set()
for checkpoint in checkpointer.list(None):
all_threads.add(checkpoint.config["configurable"]["thread_id"])
return list(all_threads)
def thread_has_document(thread_id: str) -> bool:
return str(thread_id) in _THREAD_RETRIEVERS
def thread_document_metadata(thread_id: str) -> dict:
return _THREAD_METADATA.get(str(thread_id), {})