Chapter 105
rag intro
NotebookPython 3 (ipykernel)12 cells
In [3]python · cell 1
python
from openai import OpenAI
client = OpenAI(
base_url='http://localhost:11434/v1/',
api_key='ollama',
)In [7]python · cell 2
python
from elasticsearch import ElasticsearchIn [8]python · cell 3
python
es_client = Elasticsearch('http://localhost:9200') In [9]python · cell 4
python
index_settings = {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"text": {"type": "text"},
"section": {"type": "text"},
"question": {"type": "text"},
"course": {"type": "keyword"}
}
}
}
index_name = "course-questions"
es_client.indices.create(index=index_name, body=index_settings)Output
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'course-questions'})In [10]python · cell 5
python
import requests
docs_url = 'https://github.com/DataTalksClub/llm-zoomcamp/blob/main/01-intro/documents.json?raw=1'
docs_response = requests.get(docs_url)
documents_raw = docs_response.json()
documents = []
for course in documents_raw:
course_name = course['course']
for doc in course['documents']:
doc['course'] = course_name
documents.append(doc)In [12]python · cell 6
python
from tqdm.auto import tqdmIn [13]python · cell 7
python
for doc in tqdm(documents):
es_client.index(index=index_name, document=doc)Output
0%| | 0/948 [00:00<?, ?it/s]
In [17]python · cell 8
python
def elastic_search(query):
search_query = {
"size": 5,
"query": {
"bool": {
"must": {
"multi_match": {
"query": query,
"fields": ["question^3", "text", "section"],
"type": "best_fields"
}
},
"filter": {
"term": {
"course": "data-engineering-zoomcamp"
}
}
}
}
}
response = es_client.search(index=index_name, body=search_query)
result_docs = []
for hit in response['hits']['hits']:
result_docs.append(hit['_source'])
return result_docsIn [18]python · cell 9
python
def build_prompt(query, search_results):
prompt_template = """
You're a course teaching assistant. Answer the QUESTION based on the CONTEXT from the FAQ database.
Use only the facts from the CONTEXT when answering the QUESTION.
QUESTION: {question}
CONTEXT:
{context}
""".strip()
context = ""
for doc in search_results:
context = context + f"section: {doc['section']}\nquestion: {doc['question']}\nanswer: {doc['text']}\n\n"
prompt = prompt_template.format(question=query, context=context).strip()
return prompt
def llm(prompt):
response = client.chat.completions.create(
model='phi3',
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.contentIn [19]python · cell 10
python
def rag(query):
search_results = elastic_search(query)
prompt = build_prompt(query, search_results)
answer = llm(prompt)
return answerIn [21]python · cell 11
python
query = 'I just disovered the course. Can I still join it?'
rag(query)Output
' Yes, you can still join the course even if you discover it after the start date. There will be deadlines for turning in final projects, but materials and support are available for those who start later. Additionally, we keep all the materials after the course finishes, so you can follow the course at your own pace.'
In [ ]python · cell 12
python
