-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrag.py
More file actions
75 lines (60 loc) · 2.5 KB
/
rag.py
File metadata and controls
75 lines (60 loc) · 2.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
import os
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains.retrieval import create_retrieval_chain
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import Annoy
from langchain_core.prompts import ChatPromptTemplate
from langchain_google_vertexai import VertexAIEmbeddings, ChatVertexAI
from langchain_text_splitters import RecursiveCharacterTextSplitter
# Reference: https://python.langchain.com/v0.2/docs/tutorials/pdf_qa/
if __name__ == "__main__":
print("Load and parse the PDF")
loader = PyPDFLoader(
"https://raw.githubusercontent.com/meteatamel/genai-beyond-basics/main/samples/grounding/vertexai-search/cymbal-starlight-2024.pdf")
documents = loader.load()
print("Split the document into chunks")
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
texts = text_splitter.split_documents(documents)
print("Initialize the embedding model")
embeddingsLlm = VertexAIEmbeddings(
project=os.environ["PROJECT_ID"],
location="us-central1",
model_name="text-embedding-005"
)
print("Create a vector store")
vector_store = Annoy.from_documents(texts, embeddingsLlm)
retriever = vector_store.as_retriever()
print("Initialize the chat model")
llm = ChatVertexAI(
project=os.environ["PROJECT_ID"],
location="us-central1",
model="gemini-2.0-flash"
)
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
print("Create RAG chain")
question_answer_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
print("Ready!")
questions = [
"What is the cargo capacity of Cymbal Starlight?",
"What's the emergency roadside assistance phone number?",
"Are there some special kits available on that car?"
]
for question in questions:
print(f"\n=== {question} ===")
response = rag_chain.invoke({"input": question})
print(response['answer'])