Delete rag_chain.py
Browse files- rag_chain.py +0 -92
rag_chain.py
DELETED
|
@@ -1,92 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled
|
| 3 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
-
from langchain_community.vectorstores import FAISS
|
| 5 |
-
from langchain_core.prompts import PromptTemplate
|
| 6 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
-
from langchain_groq import ChatGroq
|
| 8 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
-
from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda
|
| 10 |
-
|
| 11 |
-
VECTORSTORE_DIR = "vectorstores"
|
| 12 |
-
EMBEDDING_MODEL_NAME = os.environ["EMBEDDING_MODEL"]
|
| 13 |
-
|
| 14 |
-
_embedding_model = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def load_transcript(video_id: str) -> str:
|
| 18 |
-
"""Fetch and flatten a YouTube video's transcript."""
|
| 19 |
-
try:
|
| 20 |
-
yt_api = YouTubeTranscriptApi()
|
| 21 |
-
transcript_list = yt_api.fetch(video_id, languages=["en"])
|
| 22 |
-
transcript = " ".join(chunk.text for chunk in transcript_list)
|
| 23 |
-
return transcript
|
| 24 |
-
except TranscriptsDisabled:
|
| 25 |
-
raise ValueError("No captions available for this video.")
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def split_transcript(transcript: str):
|
| 29 |
-
splitter = RecursiveCharacterTextSplitter(
|
| 30 |
-
chunk_size=1000,
|
| 31 |
-
chunk_overlap=200,
|
| 32 |
-
)
|
| 33 |
-
return splitter.create_documents([transcript])
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def get_or_build_vectorstore(video_id: str):
|
| 37 |
-
"""Load a cached FAISS index for this video, or build + save one if missing."""
|
| 38 |
-
path = os.path.join(VECTORSTORE_DIR, video_id)
|
| 39 |
-
|
| 40 |
-
if os.path.exists(path):
|
| 41 |
-
return FAISS.load_local(
|
| 42 |
-
path,
|
| 43 |
-
_embedding_model,
|
| 44 |
-
allow_dangerous_deserialization=True,
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
transcript = load_transcript(video_id)
|
| 48 |
-
chunks = split_transcript(transcript)
|
| 49 |
-
vectorstore = FAISS.from_documents(chunks, _embedding_model)
|
| 50 |
-
|
| 51 |
-
os.makedirs(VECTORSTORE_DIR, exist_ok=True)
|
| 52 |
-
vectorstore.save_local(path)
|
| 53 |
-
|
| 54 |
-
return vectorstore
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def format_docs(retrieved_docs):
|
| 58 |
-
return "\n\n".join(doc.page_content for doc in retrieved_docs)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def get_rag_chain(video_id: str):
|
| 62 |
-
"""Builds and returns the full RAG chain for a given YouTube video."""
|
| 63 |
-
vectorstore = get_or_build_vectorstore(video_id)
|
| 64 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
|
| 65 |
-
|
| 66 |
-
llm = ChatGroq(
|
| 67 |
-
model="llama-3.1-8b-instant",
|
| 68 |
-
temperature=0,
|
| 69 |
-
api_key=os.environ["GROQ_API_KEY"],
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
prompt = PromptTemplate(
|
| 73 |
-
template="""
|
| 74 |
-
You are a helpful assistant.
|
| 75 |
-
Answer ONLY from the provided transcript context.
|
| 76 |
-
If the context is insufficient, just say you don't know.
|
| 77 |
-
|
| 78 |
-
{context}
|
| 79 |
-
Question: {question}
|
| 80 |
-
""",
|
| 81 |
-
input_variables=["context", "question"],
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
parser = StrOutputParser()
|
| 85 |
-
|
| 86 |
-
parallel_chain = RunnableParallel({
|
| 87 |
-
"context": retriever | RunnableLambda(format_docs),
|
| 88 |
-
"question": RunnablePassthrough(),
|
| 89 |
-
})
|
| 90 |
-
|
| 91 |
-
main_chain = parallel_chain | prompt | llm | parser
|
| 92 |
-
return main_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|