File size: 7,739 Bytes
50e06eb | 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 | import os
from typing import Dict, List, Optional
from dotenv import load_dotenv
from langchain_community.document_loaders import (
PyPDFLoader,
TextLoader,
WebBaseLoader,
UnstructuredPowerPointLoader,
)
from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_groq import ChatGroq
load_dotenv()
_embeddings = FastEmbedEmbeddings()
_text_splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=200,
)
_vectorstores: Dict[str, Chroma] = {}
_llm: Optional[ChatGroq] = None
def _get_vectorstore(persist_directory: str) -> Chroma:
"""
Return a cached Chroma instance for a given persist_directory.
This enables per-notebook vector stores on disk.
"""
key = os.path.abspath(persist_directory)
vs = _vectorstores.get(key)
if vs is None:
vs = Chroma(
collection_name="notebooklm_sources",
embedding_function=_embeddings,
persist_directory=persist_directory,
)
_vectorstores[key] = vs
return vs
def _get_llm() -> ChatGroq:
"""
Return a ChatGroq client configured for the openai/gpt-oss-120b model.
Requires GROQ_API_KEY in the environment.
"""
global _llm
if _llm is None:
_llm = ChatGroq(
model="openai/gpt-oss-120b",
temperature=0.3,
)
return _llm
def _load_file(path: str):
ext = os.path.splitext(path)[1].lower()
if ext == ".pdf":
loader = PyPDFLoader(path)
elif ext == ".pptx":
loader = UnstructuredPowerPointLoader(path)
elif ext == ".txt":
loader = TextLoader(path, encoding="utf-8")
else:
raise ValueError(f"Unsupported file type for RAG ingestion: {ext}")
return loader.load()
def extract_plain_text_from_file(path: str) -> str:
docs = _load_file(path)
parts: List[str] = []
for d in docs:
content = (getattr(d, "page_content", "") or "").strip()
if content:
parts.append(content)
return "\n\n".join(parts)
def extract_plain_text_from_url(url: str) -> str:
if not url:
return ""
loader = WebBaseLoader([url])
docs = loader.load()
parts: List[str] = []
for d in docs:
content = (getattr(d, "page_content", "") or "").strip()
if content:
parts.append(content)
return "\n\n".join(parts)
def ingest_file_for_rag(
path: str, *, persist_directory: str, source_name: Optional[str] = None
) -> int:
"""
Ingest a local file (PDF/PPTX/TXT) into the vector store.
Returns the number of chunks added.
"""
docs = _load_file(path)
for d in docs:
d.metadata = d.metadata or {}
if source_name:
d.metadata.setdefault("source", source_name)
splits = _text_splitter.split_documents(docs)
vs = _get_vectorstore(persist_directory)
vs.add_documents(splits)
# Ensure data is flushed to disk for persistence.
vs.persist()
return len(splits)
def ingest_url_for_rag(
url: str, *, persist_directory: str, source_name: Optional[str] = None
) -> int:
"""
Ingest the contents of a URL into the vector store.
"""
if not url:
return 0
loader = WebBaseLoader([url])
docs = loader.load()
for d in docs:
d.metadata = d.metadata or {}
d.metadata.setdefault("url", url)
if source_name:
d.metadata.setdefault("source", source_name)
splits = _text_splitter.split_documents(docs)
vs = _get_vectorstore(persist_directory)
vs.add_documents(splits)
# Ensure data is flushed to disk for persistence.
vs.persist()
return len(splits)
def _has_any_documents(*, persist_directory: str) -> bool:
vs = _get_vectorstore(persist_directory)
try:
return vs._collection.count() > 0 # type: ignore[attr-defined]
except Exception:
# Fallback if underlying API changes
docs = vs.similarity_search("test", k=1)
return len(docs) > 0
def generate_quiz(*, persist_directory: str, num_questions: int = 5) -> str:
"""
Generate a multiple-choice quiz in Markdown based on the ingested sources.
"""
if num_questions <= 0:
return "Please request at least one question."
if not _has_any_documents(persist_directory=persist_directory):
return "No sources have been ingested into the quiz engine yet. Upload a PDF/PPTX/TXT or ingest a URL first."
vs = _get_vectorstore(persist_directory)
retriever = vs.as_retriever(search_kwargs={"k": 12})
# Use a broad query to pull representative chunks across the corpus.
docs = retriever.invoke(
"key concepts, main ideas, and important facts from all documents"
)
if not docs:
return "Unable to retrieve content from the ingested sources to build a quiz."
context = "\n\n".join(d.page_content for d in docs)
prompt = f"""
You are a tutor generating a quiz for a student.
Use ONLY the information in the context below to create {num_questions} high‑quality multiple‑choice questions.
Each question should:
- Focus on an important concept or fact.
- Have 4 options labeled A, B, C, D.
- Clearly indicate the correct answer.
- Include a short explanation of why the correct option is right.
Return the quiz in Markdown with this format:
Q1. Question text
A. Option text
B. Option text
C. Option text
D. Option text
**Answer:** X
**Explanation:** ...
(then continue with Q2, Q3, etc.)
Context:
\"\"\"{context}\"\"\"
"""
llm = _get_llm()
response = llm.invoke(prompt)
content = getattr(response, "content", None)
if not content:
content = str(response)
return content
def chat_with_sources(
question: str,
*,
persist_directory: str,
history: List[List[str]] | List[tuple[str, str]] | None = None,
) -> str:
"""
Answer a user question using RAG over the ingested sources.
- `question`: current user message.
- `history`: chat history as [[user, assistant], ...] or list of tuples.
"""
question = (question or "").strip()
if not question:
return "Please enter a question."
if not _has_any_documents(persist_directory=persist_directory):
return "No sources have been ingested yet. Upload a PDF/PPTX/TXT or ingest a URL first."
vs = _get_vectorstore(persist_directory)
retriever = vs.as_retriever(search_kwargs={"k": 6})
docs = retriever.invoke(question)
if not docs:
return "I could not find relevant information in your ingested sources for that question."
context = "\n\n".join(d.page_content for d in docs)
# Keep only the last few turns to keep the prompt compact.
history = history or []
turns: List[str] = []
for h in history[-4:]:
try:
user_msg, assistant_msg = h
except ValueError:
continue
turns.append(f"User: {user_msg}")
turns.append(f"Assistant: {assistant_msg}")
history_str = "\n".join(turns) if turns else "No previous conversation."
prompt = f"""
You are a helpful assistant answering questions based ONLY on the provided context.
If the answer is not in the context, say you don't know and suggest the user add more sources.
Conversation so far:
{history_str}
User question: "{question}"
Context from the user's documents:
\"\"\"
{context}
\"\"\"
Provide a clear, concise answer grounded in the context.
"""
llm = _get_llm()
response = llm.invoke(prompt)
content = getattr(response, "content", None)
if not content:
content = str(response)
return content
|