Chapter 24
vector search
Notebookllm-zoomcamp-2026-vector45 cells
In [3]python · cell 1
python
print(123)Output
123
In [7]python · cell 2
python
q1 = "I just discovered the course, can I still join?"
q2 = "I just found out about the program, can I still enroll?"In [4]python · cell 3
python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')Output
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
modules.json: 0%| | 0.00/349 [00:00<?, ?B/s]
config_sentence_transformers.json: 0%| | 0.00/116 [00:00<?, ?B/s]
README.md: 0.00B [00:00, ?B/s]
sentence_bert_config.json: 0%| | 0.00/53.0 [00:00<?, ?B/s]
config.json: 0%| | 0.00/612 [00:00<?, ?B/s]
model.safetensors: 0%| | 0.00/90.9M [00:00<?, ?B/s]
Loading weights: 0%| | 0/103 [00:00<?, ?it/s]
tokenizer_config.json: 0%| | 0.00/350 [00:00<?, ?B/s]
vocab.txt: 0.00B [00:00, ?B/s]
tokenizer.json: 0.00B [00:00, ?B/s]
special_tokens_map.json: 0%| | 0.00/112 [00:00<?, ?B/s]
config.json: 0%| | 0.00/190 [00:00<?, ?B/s]
In [9]python · cell 4
python
v1 = model.encode(q1)In [10]python · cell 5
python
v1.shapeOutput
(384,)
In [12]python · cell 6
python
v2 = model.encode(q2)In [13]python · cell 7
python
q1 = 'Can I still join the course after the start date?'
v1 = model.encode(q1)In [14]python · cell 8
python
d = "You don't need to register. You're accepted. You can also just start learning and submitting homework without registering."
dv = model.encode(d)In [17]python · cell 9
python
v1.dot(dv)Output
np.float32(0.32332397)
In [18]python · cell 10
python
q2 = 'How to install Docker on Windows?'
v2 = model.encode(q2)In [19]python · cell 11
python
v2.dot(dv)Output
np.float32(0.019730574)
In [20]python · cell 12
python
!wget https://raw.githubusercontent.com/DataTalksClub/llm-zoomcamp/main/01-agentic-rag/code/ingest.pyOutput
--2026-05-18 10:31:25-- https://raw.githubusercontent.com/DataTalksClub/llm-zoomcamp/main/01-agentic-rag/code/ingest.py Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 738 [text/plain] Saving to: ‘ingest.py’ ingest.py 100%[===================>] 738 --.-KB/s in 0s 2026-05-18 10:31:26 (41.5 MB/s) - ‘ingest.py’ saved [738/738]
In [21]python · cell 13
python
from ingest import load_faq_data
documents = load_faq_data()In [23]python · cell 14
python
documents[10]Output
{'id': '2b5ff70c77',
'course': 'machine-learning-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'Do I need to enroll in the course before submitting homework?',
'answer': 'No enrollment is required to submit homework. Just log into the homework form when it opens. The Airtable registration you may see is only for announcements; actual submissions are made on the course platform forms and via your GitHub as specified in the homework guidelines.'}In [ ]python · cell 15
python
texts = []
for doc in documents:
text = doc['question'] + ' ' + doc['answer']
texts.append(text)In [26]python · cell 16
python
len(texts)Output
1208
In [ ]python · cell 17
python
documents[10]Output
{'id': '2b5ff70c77',
'course': 'machine-learning-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'Do I need to enroll in the course before submitting homework?',
'answer': 'No enrollment is required to submit homework. Just log into the homework form when it opens. The Airtable registration you may see is only for announcements; actual submissions are made on the course platform forms and via your GitHub as specified in the homework guidelines.'}In [29]python · cell 18
python
from tqdm.auto import tqdm
batch_size = 50
vectors = []
for i in tqdm(range(0, len(texts), batch_size)):
batch = texts[i:i + batch_size]
batch_vectors = model.encode(batch)
vectors.extend(batch_vectors)
len(vectors)Output
0%| | 0/25 [00:00<?, ?it/s]
1208
In [35]python · cell 19
python
scores = []
for i in range(len(vectors)):
score = v1.dot(vectors[i])
scores.append(score)In [37]python · cell 20
python
import numpy as np
X = np.array(vectors)In [40]python · cell 21
python
scores = X.dot(v1)In [42]python · cell 22
python
idx = np.argmax(scores)
idx, scores[idx]Output
(np.int64(553), np.float32(0.762941))
In [43]python · cell 23
python
documents[553]Output
{'id': '3f1424af17',
'course': 'data-engineering-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'Course: Can I still join the course after the start date?',
'answer': "Yes, even if you don't register, you're still eligible to submit the homework.\n\nBe aware, however, that there will be deadlines for turning in homeworks and the final projects. So don't leave everything for the last minute."}In [51]python · cell 24
python
top5 = np.argsort(scores)[-5:]
top5 = top5[::-1]In [52]python · cell 25
python
scores[top5]Output
array([0.762941 , 0.7579372 , 0.7192131 , 0.6536311 , 0.56009984],
dtype=float32)In [53]python · cell 26
python
for idx in top5:
print(scores[idx])
print(documents[idx])
print()Output
0.762941
{'id': '3f1424af17', 'course': 'data-engineering-zoomcamp', 'section': 'General Course-Related Questions', 'question': 'Course: Can I still join the course after the start date?', 'answer': "Yes, even if you don't register, you're still eligible to submit the homework.\n\nBe aware, however, that there will be deadlines for turning in homeworks and the final projects. So don't leave everything for the last minute."}
0.7579372
{'id': '2d8b16c2a0', 'course': 'mlops-zoomcamp', 'section': 'General Course-Related Questions', 'question': 'Course - Can I still join the course after the start date?', 'answer': "Yes, even if you don't register, you're still eligible to submit the homeworks as long as the form is still open and accepting submissions.\n\nBe aware, however, that there will be deadlines for turning in the final projects. So don't leave everything to the last minute."}
0.7192131
{'id': '41aabbd7c5', 'course': 'machine-learning-zoomcamp', 'section': 'General Course-Related Questions', 'question': 'The course has already started. Can I still join it?', 'answer': 'Yes, you can. Even though you missed the start date, you can register for the course. You won’t be able to submit some of the homeworks, but you can still take part in the course.\n\nIn order to get a certificate, you need to submit 2 out of 3 course projects and review 3 peers by the deadline. It means that if you join the course at the end of November and manage to work on two projects, you will still be eligible for a certificate.'}
0.6536311
{'id': '74eb249bbf', 'course': 'llm-zoomcamp', 'section': 'General Course-Related Questions', 'question': 'I just discovered the course. Can I still join?', 'answer': 'Yes, but if you want to receive a certificate, you need to submit your project while we’re still accepting submissions.'}
0.56009984
{'id': '068529125b', 'course': 'data-engineering-zoomcamp', 'section': 'General Course-Related Questions', 'question': 'Course - Can I follow the course after it finishes?', 'answer': 'Yes, we will keep all the materials available, so you can follow the course at your own pace after it finishes.\n\nYou can also continue reviewing the homeworks and prepare for the next cohort. You can also start working on your final capstone project.'}
In [59]python · cell 27
python
top5Output
array([553, 955, 29, 472, 558])
In [60]python · cell 28
python
top5 = np.argsort(-scores)[:5]In [61]python · cell 29
python
top5Output
array([553, 955, 29, 472, 558])
In [62]python · cell 30
python
from minsearch import VectorSearch
vindex = VectorSearch(keyword_fields=['course'])
vindex.fit(X, documents)Output
<minsearch.vector.VectorSearch at 0x70dabdde4590>
In [65]python · cell 31
python
vindex.search(v1, num_results=5, filter_dict={'course': 'llm-zoomcamp'})Output
[{'id': '74eb249bbf',
'course': 'llm-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'I just discovered the course. Can I still join?',
'answer': 'Yes, but if you want to receive a certificate, you need to submit your project while we’re still accepting submissions.'},
{'id': 'bd31146b0e',
'course': 'llm-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'When will the course be offered next?',
'answer': 'Summer 2025.'},
{'id': '69d122f12e',
'course': 'llm-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'Certificate: Can I follow the course in a self-paced mode and get a certificate?',
'answer': 'No, you can only get a certificate if you finish the course with a "live" cohort.\n\nWe don\'t award certificates for the self-paced mode. The reason is you need to peer-review 3 capstone(s) after submitting your project.\n\nYou can only peer-review projects at the time the course is running; after the form is closed and the peer-review list is compiled.'},
{'id': '977bf7786c',
'course': 'llm-zoomcamp',
'section': 'General Course-Related Questions',
'question': 'Course: I have registered for the LLM Zoomcamp. When can I expect to receive the confirmation email?',
'answer': "You don't need it. You're accepted. You can also just start learning and submitting homework (while the form is open) without registering. It is not checked against any registered list. Registration is just to gauge interest before the start date."},
{'id': '85384a18e5',
'course': 'llm-zoomcamp',
'section': 'Module 1: Introduction to LLMs and RAG',
'question': 'OpenAI: Do I have to subscribe and pay for Open AI API for this course?',
'answer': "No, you don't have to pay for this service in order to complete the course homeworks. You could use some of the free alternatives listed in the course GitHub.\n\n[llm-zoomcamp/01-intro/open-ai-alternatives.md at main · DataTalksClub/llm-zoomcamp (github.com)](https://github.com/DataTalksClub/llm-zoomcamp/blob/main/01-intro/open-ai-alternatives.md)"}]In [66]python · cell 32
python
!wget https://raw.githubusercontent.com/DataTalksClub/llm-zoomcamp/main/01-agentic-rag/code/rag_helper.pyOutput
--2026-05-18 10:56:54-- https://raw.githubusercontent.com/DataTalksClub/llm-zoomcamp/main/01-agentic-rag/code/rag_helper.py Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2134 (2.1K) [text/plain] Saving to: ‘rag_helper.py’ rag_helper.py 100%[===================>] 2.08K --.-KB/s in 0s 2026-05-18 10:56:54 (25.3 MB/s) - ‘rag_helper.py’ saved [2134/2134]
In [67]python · cell 33
python
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
openai_client = OpenAI()In [68]python · cell 34
python
from ingest import load_faq_data, build_index
documents = load_faq_data()
index = build_index(documents)In [ ]python · cell 35
python
from rag_helper import RAGBase
assistant = RAGBase(
index=index,
llm_client=openai_client,
)In [71]python · cell 36
python
query = 'I just found out about the program, can I still sign up?'
assistant.rag(query)Output
'Yes — you can still sign up. If you want a certificate, make sure to submit your project while submissions are still open.'
In [73]python · cell 37
python
class RAGVector(RAGBase):
def __init__(self, embedder, **kwargs):
super().__init__(**kwargs)
self.embedder = embedder
def search(self, query, num_results=5):
query_vector = self.embedder.encode(query)
filter_dict = {'course': self.course}
return self.index.search(
query_vector,
num_results=num_results,
filter_dict=filter_dict
)In [ ]python · cell 38
python
vector_assistant = RAGVector(
embedder=model,
index=vindex,
llm_client=openai_client,
)In [75]python · cell 39
python
query = 'I just found out about the program, can I still sign up?'
vector_assistant.rag(query)Output
'Yes, but if you want to receive a certificate, you need to submit your project while submissions are still open.'
In [76]python · cell 40
python
from sqlitesearch import VectorSearchIndex
vs_index = VectorSearchIndex(
keyword_fields=['course'],
mode='ivf',
db_path='faq_vectors2.db'
)In [77]python · cell 41
python
vs_index.fit(vectors, documents)Output
<sqlitesearch.vector.index.VectorSearchIndex at 0x70dbbd1949e0>
In [80]python · cell 42
python
query = 'I just discovered the course. Can I still join it?'
query_vector = model.encode(query)
results = vs_index.search(query_vector, num_results=5)In [81]python · cell 43
python
results = vs_index.search(
query_vector,
filter_dict={'course': 'llm-zoomcamp'},
num_results=5
)In [86]python · cell 44
python
vs_index.close()In [ ]python · cell 45
python
