Spaces:
Sleeping
Sleeping
Update src/chains/qa_chain_gemini.py
Browse files
src/chains/qa_chain_gemini.py
CHANGED
|
@@ -1 +1,34 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_chroma import Chroma
|
| 2 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 3 |
+
from langchain.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
|
| 6 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 7 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 8 |
+
from src.agents.prompts import RAG_PROMPT
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def build_gemini_rag_chain(pdf_path: str):
|
| 12 |
+
# Load and split documents
|
| 13 |
+
loader = PyPDFLoader(pdf_path)
|
| 14 |
+
documents = loader.load()
|
| 15 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 16 |
+
texts = splitter.split_documents(documents)
|
| 17 |
+
|
| 18 |
+
# Create vectorstore
|
| 19 |
+
vectorstore = Chroma.from_documents(texts, embedding=GoogleGenerativeAIEmbeddings(model="models/embedding-001"))
|
| 20 |
+
retriever = vectorstore.as_retriever()
|
| 21 |
+
|
| 22 |
+
# Make retriever history-aware
|
| 23 |
+
contextualize_q_prompt = ChatPromptTemplate.from_messages([
|
| 24 |
+
("system", "Given a chat history and the latest user question, rewrite it as a standalone question."),
|
| 25 |
+
MessagesPlaceholder("chat_history"),
|
| 26 |
+
("human", "{input}"),
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
model = ChatGoogleGenerativeAI(model="gemini-2.0-flash", convert_system_message_to_human=True)
|
| 30 |
+
history_aware_retriever = create_history_aware_retriever(model, retriever, contextualize_q_prompt)
|
| 31 |
+
|
| 32 |
+
# Build RAG chain
|
| 33 |
+
qa_chain = create_stuff_documents_chain(model, RAG_PROMPT)
|
| 34 |
+
return create_retrieval_chain(history_aware_retriever, qa_chain)
|