-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-llama31-qlora-fine-tuned-inference-llamacpp.py
More file actions
49 lines (40 loc) 路 1.4 KB
/
Copy path03-llama31-qlora-fine-tuned-inference-llamacpp.py
File metadata and controls
49 lines (40 loc) 路 1.4 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
from llama_cpp import Llama
# ----------------------------------------------------------------------------------------
# max sequence length that the model will receive as input
max_seq_length = 2048
# seed for reproducibility
seed = 1993
# output path for lora adapters
path_gguf = 'models/llama31-8b-qlora-gguf'
# example question
question = 'The product PRODUCT_CODE is it orderable?'
# example information/context
information = {'availability': {'product code': 'PRODUCT_CODE', 'product desc': 'PRODUCT_DESC', 'available': False, 'orderable': False, 'product status': 'PRODUCT_STATUS'}}
# ----------------------------------------------------------------------------------------
# load quantized model
llm = Llama(
model_path=f'{path_gguf}/unsloth.Q5_K_M.gguf',
n_gpu_layers=-1,
seed=seed,
n_ctx=max_seq_length,
chat_format='llama-3'
)
# create a list of messages
messages = [
{'role': 'system', 'content': 'you are a helpful assistant'},
{'role': 'user', 'content': question},
{'role': 'ipython', 'content': information}
]
# format messages and prepare output from llm
output = llm.create_chat_completion(
messages=messages,
max_tokens=512,
stream=True
)
# stream output from llm
for chunk in output:
delta = chunk['choices'][0]['delta']
if 'role' in delta:
print(delta['role'], end=': ')
elif 'content' in delta:
print(delta['content'], end='')