Views
No views yet
| 模型名称 | 🤗HF模型标识 | 下载地址 |
|---|---|---|
| YaYi-7B | wenge-research/yayi-7b | 模型下载 |
| YaYi-7B-Llama2 | wenge-research/yayi-7b-llama2 | 模型下载 |
| YaYi-13B-Llama2 | wenge-research/yayi-13b-llama2 | 模型下载 |
| YaYi-70B-Llama2 | wenge-research/yayi-70b-llama2 | 模型下载 |
1import torch
2from transformers import LlamaForCausalLM, LlamaTokenizer, GenerationConfig
3from transformers import StoppingCriteria, StoppingCriteriaList
4
5pretrained_model_name_or_path = "wenge-research/yayi-7b-llama2"
6tokenizer = LlamaTokenizer.from_pretrained(pretrained_model_name_or_path)
7model = LlamaForCausalLM.from_pretrained(pretrained_model_name_or_path, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=False)
8
9# Define the stopping criteria
10class KeywordsStoppingCriteria(StoppingCriteria):
11 def __init__(self, keywords_ids:list):
12 self.keywords = keywords_ids
13
14 def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
15 if input_ids[0][-1] in self.keywords:
16 return True
17 return False
18
19stop_words = ["<|End|>", "<|YaYi|>", "<|Human|>", "</s>"]
20stop_ids = [tokenizer.encode(w)[-1] for w in stop_words]
21stop_criteria = KeywordsStoppingCriteria(stop_ids)
22
23# inference
24prompt = "你是谁?"
25formatted_prompt = f"""<|System|>:
26You are a helpful, respectful and honest assistant named YaYi developed by Beijing Wenge Technology Co.,Ltd. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
27
28<|Human|>:
29{prompt}
30
31<|YaYi|>:
32"""
33
34inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
35eos_token_id = tokenizer("<|End|>").input_ids[0]
36generation_config = GenerationConfig(
37 eos_token_id=eos_token_id,
38 pad_token_id=eos_token_id,
39 do_sample=True,
40 max_new_tokens=256,
41 temperature=0.3,
42 repetition_penalty=1.1,
43 no_repeat_ngram_size=0
44)
45response = model.generate(**inputs, generation_config=generation_config, stopping_criteria=StoppingCriteriaList([stop_criteria]))
46response = [response[0][len(inputs.input_ids[0]):]]
47response_str = tokenizer.batch_decode(response, skip_special_tokens=False, clean_up_tokenization_spaces=False)[0]
48print(response_str)| Model | 🤗HF Model Name | Download Links |
|---|---|---|
| YaYi-7B | wenge-research/yayi-7b | Download |
| YaYi-7B-Llama2 | wenge-research/yayi-7b-llama2 | Download |
| YaYi-13B-Llama2 | wenge-research/yayi-13b-llama2 | Download |
| YaYi-70B-Llama2 | wenge-research/yayi-70b-llama2 | Download |
1import torch
2from transformers import LlamaForCausalLM, LlamaTokenizer, GenerationConfig
3from transformers import StoppingCriteria, StoppingCriteriaList
4
5pretrained_model_name_or_path = "wenge-research/yayi-7b-llama2"
6tokenizer = LlamaTokenizer.from_pretrained(pretrained_model_name_or_path)
7model = LlamaForCausalLM.from_pretrained(pretrained_model_name_or_path, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=False)
8
9# Define the stopping criteria
10class KeywordsStoppingCriteria(StoppingCriteria):
11 def __init__(self, keywords_ids:list):
12 self.keywords = keywords_ids
13
14 def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
15 if input_ids[0][-1] in self.keywords:
16 return True
17 return False
18
19stop_words = ["<|End|>", "<|YaYi|>", "<|Human|>", "</s>"]
20stop_ids = [tokenizer.encode(w)[-1] for w in stop_words]
21stop_criteria = KeywordsStoppingCriteria(stop_ids)
22
23# inference
24prompt = "你是谁?"
25formatted_prompt = f"""<|System|>:
26You are a helpful, respectful and honest assistant named YaYi developed by Beijing Wenge Technology Co.,Ltd. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
27
28<|Human|>:
29{prompt}
30
31<|YaYi|>:
32"""
33
34inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
35eos_token_id = tokenizer("<|End|>").input_ids[0]
36generation_config = GenerationConfig(
37 eos_token_id=eos_token_id,
38 pad_token_id=eos_token_id,
39 do_sample=True,
40 max_new_tokens=256,
41 temperature=0.3,
42 repetition_penalty=1.1,
43 no_repeat_ngram_size=0
44)
45response = model.generate(**inputs, generation_config=generation_config, stopping_criteria=StoppingCriteriaList([stop_criteria]))
46response = [response[0][len(inputs.input_ids[0]):]]
47response_str = tokenizer.batch_decode(response, skip_special_tokens=False, clean_up_tokenization_spaces=False)[0]
48print(response_str)