-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.py
More file actions
65 lines (48 loc) · 1.43 KB
/
task3.py
File metadata and controls
65 lines (48 loc) · 1.43 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
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.chat_models import ChatOllama
# Load PDFs
files = ["sample1.pdf", "sample2.pdf"]
docs = []
for f in files:
loader = PyPDFLoader(f)
docs.extend(loader.load())
# Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)
# Exercise 1: Vector DB
embeddings = OllamaEmbeddings(model="nomic-embed-text")
db = Chroma.from_documents(chunks, embeddings)
# Exercise 2: Retriever
retriever = db.as_retriever(search_kwargs={"k": 3})
# Exercise 3: RAG
llm = ChatOllama(model="qwen2.5:1.5b")
def ask(question):
docs = retriever.invoke(question)
context = "\n\n".join([d.page_content for d in docs])
prompt = f"""
Answer using only the context below.
Context:
{context}
Question:
{question}
"""
answer = llm.invoke(prompt)
return docs, answer.content
# Exercise 4: Test
questions = [
"What is this document about?",
"Summarize it",
"What are the key points?"
]
for q in questions:
print("\n" + "="*40)
print("Question:", q)
docs, ans = ask(q)
print("\nChunks:")
for d in docs:
print("-", d.page_content[:150])
print("\nAnswer:")
print(ans)