Chapter 103
huggingface phi3
Notebooksaturn (Python 3)12 cells
If you're not running in Saturn Cloud, you need to install these libraries:
Make sure you use the latest versions
code
pip install -U transformers accelerate bitsandbytesIn [1]python · cell 2
python
import os
os.environ['HF_HOME'] = '/run/cache/'In [1]python · cell 3
python
!rm -f minsearch.py
!wget https://raw.githubusercontent.com/alexeygrigorev/minsearch/main/minsearch.pyOutput
--2024-06-13 19:36:42-- https://raw.githubusercontent.com/alexeygrigorev/minsearch/main/minsearch.py Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.109.133, 185.199.111.133, 185.199.108.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.109.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 3832 (3.7K) [text/plain] Saving to: ‘minsearch.py’ minsearch.py 100%[===================>] 3.74K --.-KB/s in 0s 2024-06-13 19:36:42 (50.3 MB/s) - ‘minsearch.py’ saved [3832/3832]
In [2]python · cell 4
python
import requests
import minsearch
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)
index = minsearch.Index(
text_fields=["question", "text", "section"],
keyword_fields=["course"]
)
index.fit(documents)Output
<minsearch.Index at 0x7f58c573f970>
In [3]python · cell 5
python
def search(query):
boost = {'question': 3.0, 'section': 0.5}
results = index.search(
query=query,
filter_dict={'course': 'data-engineering-zoomcamp'},
boost_dict=boost,
num_results=5
)
return resultsIn [4]python · cell 6
python
def rag(query):
search_results = search(query)
prompt = build_prompt(query, search_results)
answer = llm(prompt)
return answerIn [6]python · cell 7
python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
torch.random.manual_seed(0)Output
<torch._C.Generator at 0x7f5891365750>
In [7]python · cell 8
python
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3-mini-128k-instruct",
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct")Output
config.json: 0%| | 0.00/3.55k [00:00<?, ?B/s]
configuration_phi3.py: 0%| | 0.00/10.4k [00:00<?, ?B/s]
A new version of the following files was downloaded from https://huggingface.co/microsoft/Phi-3-mini-128k-instruct: - configuration_phi3.py . Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.
modeling_phi3.py: 0%| | 0.00/73.8k [00:00<?, ?B/s]
A new version of the following files was downloaded from https://huggingface.co/microsoft/Phi-3-mini-128k-instruct: - modeling_phi3.py . Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision. `flash-attention` package not found, consider installing for better performance: No module named 'flash_attn'. Current `flash-attenton` does not support `window_size`. Either upgrade or use `attn_implementation='eager'`.
model.safetensors.index.json: 0%| | 0.00/16.3k [00:00<?, ?B/s]
Downloading shards: 0%| | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors: 0%| | 0.00/4.97G [00:00<?, ?B/s]
model-00002-of-00002.safetensors: 0%| | 0.00/2.67G [00:00<?, ?B/s]
Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]
generation_config.json: 0%| | 0.00/172 [00:00<?, ?B/s]
tokenizer_config.json: 0%| | 0.00/3.17k [00:00<?, ?B/s]
tokenizer.model: 0%| | 0.00/500k [00:00<?, ?B/s]
tokenizer.json: 0%| | 0.00/1.84M [00:00<?, ?B/s]
added_tokens.json: 0%| | 0.00/293 [00:00<?, ?B/s]
special_tokens_map.json: 0%| | 0.00/568 [00:00<?, ?B/s]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
In [8]python · cell 9
python
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)In [14]python · cell 10
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):
messages = [
{"role": "user", "content": prompt},
]
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": 0.0,
"do_sample": False,
}
output = pipe(messages, **generation_args)
return output[0]['generated_text'].strip()In [15]python · cell 11
python
rag("I just discovered the course. Can I still join it?")Output
'Yes, you can still join the course even if you discover it after the start date.'
In [ ]python · cell 12
python
