-
Notifications
You must be signed in to change notification settings - Fork 21
/
inference.py
41 lines (33 loc) · 1.04 KB
/
inference.py
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
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "zhichen/Llama3-Chinese" # 替换成你自己的模型路径
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device="cuda",
)
messages = [
{"role": "system", "content": "A chat between a curious user and an artificial intelligence assistant.The assistant gives helpful, detailed, and polite answers to the user's questions."},
{"role": "user", "content": "你是谁"},
]
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("<|eot_id|>")
]
outputs = pipeline(
prompt,
max_new_tokens=2048,
eos_token_id=terminators,
do_sample=False,
temperature=0.6,
top_p=1,
repetition_penalty=1.05
)
print(outputs[0]["generated_text"][len(prompt):])