-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathllama_qa.py
More file actions
44 lines (37 loc) · 1.06 KB
/
Copy pathllama_qa.py
File metadata and controls
44 lines (37 loc) · 1.06 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
import transformers
import torch
model_id = "unsloth/llama-3-8b-Instruct-bnb-4bit"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={
"torch_dtype": torch.float16,
"quantization_config": {"load_in_4bit": True},
"low_cpu_mem_usage": True,
},
)
def ask_question_llama(question, context):
messages = [
{"role": "system", "content": "You are a helpful assistant!"},
{"role": "user", "content": context},
{"role": "assistant", "content": ""},
{"role": "user", "content": question},
]
prompt = pipeline.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
terminators = [
pipeline.tokenizer.eos_token_id,
pipeline.tokenizer.convert_tokens_to_ids("")
]
outputs = pipeline(
prompt,
max_new_tokens=256,
eos_token_id=terminators,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
return outputs[0]["generated_text"][len(prompt):]