-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrarian_graph.py
More file actions
299 lines (263 loc) · 12.5 KB
/
librarian_graph.py
File metadata and controls
299 lines (263 loc) · 12.5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from typing import TypedDict, List, Dict, Optional
import logging
from langgraph.graph import StateGraph, END
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from config import Config
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Define State
class LibrarianState(TypedDict):
query: str
focus_video_id: str
chat_history: List[Dict]
attached_highlight: Optional[Dict]
context_docs: List[Dict]
saved_videos: List[Dict]
inventory_highlights: List[Dict]
answer: str
sources: List[Dict]
class LibrarianGraph:
def __init__(self, librarian_agent):
self.agent = librarian_agent
self.model = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
google_api_key=Config.GOOGLE_API_KEY,
temperature=0
)
self.workflow = self._build_graph()
def _retrieve(self, state: LibrarianState):
"""Retrieve documents from Firestore via LibrarianAgent using cascading multi-tier search."""
query = state['query']
focus_video_id = state.get('focus_video_id') or ""
logger.info(f"LangGraph Retrieve: {query} (focus: {focus_video_id or 'none'})")
# Multi-tier retrieval: passes focus_video_id for optimized search
search_res = self.agent.search_history(
query, n_results=8, focus_video_id=focus_video_id or None
)
docs = search_res.get('results', [])
source_cards = self.agent.build_source_cards_from_results(
docs,
focus_video_id=focus_video_id,
limit=4
)
# If a video is focused, ALWAYS ensure its source card is included
if focus_video_id:
focus_norm = self.agent._normalize_original_video_id(focus_video_id)
has_focus_card = any(
self.agent._normalize_original_video_id(c.get("video_id")) == focus_norm
for c in source_cards
)
if not has_focus_card:
focus_card = self.agent.get_video_context_card(focus_norm)
source_cards.insert(0, focus_card)
saved_videos = self.agent.get_saved_videos(limit=80)
inventory_highlights = self.agent.get_all_highlights(limit=120)
if focus_video_id:
focus_norm = self.agent._normalize_original_video_id(focus_video_id)
inventory_highlights = [
h for h in inventory_highlights
if self.agent._normalize_original_video_id(h.get("video_id")) == focus_norm
]
return {
"context_docs": docs,
"sources": source_cards,
"saved_videos": saved_videos[:30],
"inventory_highlights": inventory_highlights[:30],
}
def _generate(self, state: LibrarianState):
"""Generate answer using RAG."""
query = state['query']
docs = state['context_docs']
sources = state.get('sources', [])
focus_video_id = state.get("focus_video_id") or ""
saved_videos = state.get("saved_videos", [])
inventory_highlights = state.get("inventory_highlights", [])
chat_history = state.get("chat_history") or []
attached_highlight = state.get("attached_highlight")
# Format Context with tier and timestamp info
if docs:
context_lines = []
for d in docs:
tier = d.get('tier', 2)
tier_label = {1: 'Summary', 2: 'Segment', 3: 'Clip'}.get(tier, 'Chunk')
ts_info = ''
if d.get('start_time') is not None:
ts_info = f" [{d['start_time']:.0f}s-{d.get('end_time', 0):.0f}s]"
context_lines.append(
f"[{tier_label}{ts_info}] Video: {d.get('title', 'Untitled')}: {d.get('snippet', '')}"
)
context_str = "\n\n".join(context_lines)
else:
context_str = "No semantic match found from saved embeddings."
# Format source cards with rich highlight content AND transcript snippets
if sources:
enriched_context_lines = []
for source in sources:
# Include transcript snippets from the video's chunks
snippets = source.get("snippets", [])
snippets_text = ""
if snippets:
snippets_text = "\n ".join([f"- {s}" for s in snippets[:10]])
snippets_text = f"\n Transcript Excerpts:\n {snippets_text}"
highlights = source.get("highlights", [])
highlight_parts = []
for h in highlights[:6]:
rl = h.get('range_label', '')
note = (h.get('note') or '').strip()
transcript = (h.get('transcript') or '').strip()
parts = []
if rl:
parts.append(f"[{rl}]")
if note:
parts.append(f'Note: "{note}"')
if transcript:
parts.append(f"Content: {transcript[:300]}")
elif note:
pass # note already included
highlight_parts.append(" ".join(parts))
highlights_text = "\n ".join(highlight_parts) if highlight_parts else "None"
enriched_context_lines.append(
f"Video Card: {source.get('title', 'Untitled')}\n"
f" Description: {source.get('description', '')}\n"
f" Summary: {source.get('summary', '')}"
f"{snippets_text}\n"
f" Highlights:\n {highlights_text}"
)
enriched_context = "\n\n".join(enriched_context_lines)
else:
enriched_context = "No video cards were built."
# Format saved videos with content preview
if saved_videos:
inventory_lines = []
for video in saved_videos[:30]:
line = (
f"- {video.get('title', 'Untitled')} | id: {video.get('video_id', '')} "
f"| description: {video.get('description', '')}"
)
# Include summary/content preview if available from source card
summary = video.get('summary', '')
if summary:
line += f" | summary: {summary[:200]}"
inventory_lines.append(line)
inventory_context = "\n".join(inventory_lines)
else:
inventory_context = "No saved videos were retrieved."
# Format highlights with BOTH note AND transcript content
if inventory_highlights:
highlight_lines = []
for h in inventory_highlights[:30]:
video_title = h.get("video_title") or h.get("title") or "Untitled"
label = h.get("range_label") or ""
note = (h.get("note") or "").strip()
transcript = (h.get("transcript") or "").strip()
created_at = h.get("created_at") or ""
line = f"- {video_title} [{label}]"
if created_at:
line += f" (created: {created_at[:16]})"
if note:
line += f'\n Note: "{note}"'
if transcript:
line += f"\n Transcript content: {transcript[:300]}"
elif not note:
line += "\n (no note or transcript)"
highlight_lines.append(line)
highlights_context = "\n".join(highlight_lines)
else:
highlights_context = "No highlights were retrieved."
# Format attached highlight (when user clicks/drags a highlight into chat)
attached_context = ""
if attached_highlight:
ah = attached_highlight
attached_context = (
f"\n\nAttached Highlight (the user is asking specifically about this highlight):\n"
f" Video: {ah.get('video_title', 'Unknown')}\n"
f" Time Range: {ah.get('range_label', 'Unknown')}\n"
f" User Note: {ah.get('note', '(none)')}\n"
f" Transcript Content: {ah.get('transcript', '(no transcript available)')}\n"
)
# System Prompt
system_msg = """You are the TubeFocus Librarian — an AI assistant that helps users recall and understand their saved YouTube video content and highlights.
You have access to the user's saved library context below. Use it to answer their questions.
## Core Rules
- Answer the user's question directly first (1-3 sentences), then add short evidence bullets if helpful.
- If a focused video is present, prioritize that video unless the context clearly points elsewhere.
- For inventory/list/count questions, use the Saved Videos Inventory and Inventory Highlights sections.
- Never claim "no saved videos" if Saved Videos Inventory has items.
- Include highlight time ranges when referencing specific moments.
- Keep responses concise and practical.
- **Format your response using Markdown**:
- Use **bold** for key concepts and terms.
- Use bullet points for lists.
- Use `### Headers` to organize sections if the answer is long.
## Highlight Queries
- The Inventory Highlights section contains BOTH user notes AND actual transcript content for each highlight.
- When asked "what are my highlights" or "summarize my highlights", synthesize ALL highlights into a coherent summary grouped by video.
- When asked "what was my recent/last highlight about", look at the most recent entries (sorted by creation date) and describe their transcript content.
- When an Attached Highlight is present, the user is asking about THAT specific highlight — analyze its transcript content in depth.
## Grounding
- Make a best-effort grounded answer from partial context. Do NOT default to "I don't have enough context" or "I need more information" when ANY relevant data exists in the provided sections.
- Use available titles, summaries, snippets, transcript content, and highlight notes.
- If truly no relevant context exists, say so clearly and suggest what to save next.
## Conversation History
- Previous messages in this conversation are provided. Use them for continuity (e.g., "tell me more", "what else", references to prior answers)."""
# Build messages array
messages = [SystemMessage(content=system_msg)]
# Add conversation history
for msg in chat_history[-6:]: # Last 6 turns (3 user + 3 assistant)
role = msg.get("role", "")
content = msg.get("content", "")
if role == "user":
messages.append(HumanMessage(content=content))
elif role == "assistant":
messages.append(AIMessage(content=content))
# Current user message with full context
user_msg = f"""Question: {query}
Focused Video ID: {focus_video_id or "none"}
{attached_context}
Context Snippets:
{context_str}
Video Cards:
{enriched_context}
Saved Videos Inventory:
{inventory_context}
Inventory Highlights:
{highlights_context}
"""
messages.append(HumanMessage(content=user_msg))
try:
response = self.model.invoke(messages)
return {"answer": response.content, "sources": sources}
except Exception as e:
logger.error(f"Generation failed: {e}")
return {"answer": "Sorry, I encountered an error generating the response.", "sources": []}
def _build_graph(self):
"""Build the LangGraph workflow."""
workflow = StateGraph(LibrarianState)
# Add Nodes
workflow.add_node("retrieve", self._retrieve)
workflow.add_node("generate", self._generate)
# Add Edges
workflow.set_entry_point("retrieve")
workflow.add_edge("retrieve", "generate")
workflow.add_edge("generate", END)
return workflow.compile()
def invoke(self, query: str, focus_video_id: str = "", chat_history: List[Dict] = None, attached_highlight: Dict = None):
"""Entry point for the graph."""
inputs = {
"query": query,
"focus_video_id": focus_video_id or "",
"chat_history": chat_history or [],
"attached_highlight": attached_highlight,
"context_docs": [],
"saved_videos": [],
"inventory_highlights": [],
"answer": "",
"sources": []
}
result = self.workflow.invoke(inputs)
return {
"answer": result.get("answer"),
"sources": result.get("sources")
}