Views
No views yet
1>>> python -m pip install numpy safetensors sentencepiece torch transformers accelerate
21import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3tokenizer = AutoTokenizer.from_pretrained(
4 "pfnet/plamo-13b-instruct-nc",
5 trust_remote_code=True,
6)
7model = AutoModelForCausalLM.from_pretrained(
8 "pfnet/plamo-13b-instruct-nc",
9 trust_remote_code=True,
10 torch_dtype=torch.bfloat16,
11 device_map="auto",
12)1def completion(prompt: str, max_new_tokens: int = 128) -> str:
2 inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
3 generated_ids = model.generate(
4 inputs.input_ids,
5 eos_token_id=2,
6 pad_token_id=3,
7 max_new_tokens=max_new_tokens,
8 temperature=1,
9 top_p=0.95,
10 top_k=50,
11 do_sample=True,
12 )
13 return tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
14
15def generate_prompt(messages: list) -> str:
16 sep = "\n\n### "
17 prompt = [
18 "以下はタスクを説明する指示で、文脈を説明した入力とペアになっています。",
19 "要求を適切に補完するよう応答を書いてください。",
20 ]
21 roles = {"instruction": "指示", "response": "応答", "input": "入力"}
22 for msg in messages:
23 prompt.append(sep + roles[msg["role"]] + ":\n" + msg['content'])
24 prompt.append(sep + roles["response"] + ":\n")
25 return "".join(prompt)1prompt = generate_prompt([
2 {"role": "instruction", "content": "日本の首都はどこですか?"},
3 # {"role": "input", "content": "..."} ## An extra input (optional)
4])
5print(completion(prompt, max_new_tokens=128))1@online{PLaMoInstructNC2023Introducing,
2 author = {Preferred Networks, Inc},
3 title = {PLaMo-13B-Instruct-NC},
4 year = {2023},
5 url = {https://huggingface.co/pfnet/plamo-13b-instruct-nc},
6 urldate = {2023-10-26}
7}1@misc{alpaca,
2 author = {Rohan Taori and Ishaan Gulrajani and Tianyi Zhang and Yann Dubois and Xuechen Li and Carlos Guestrin and Percy Liang and Tatsunori B. Hashimoto },
3 title = {Stanford Alpaca: An Instruction-following LLaMA model},
4 year = {2023},
5 publisher = {GitHub},
6 journal = {GitHub repository},
7 howpublished = {\url{https://github.com/tatsu-lab/stanford_alpaca}},
8}1@article{touvron2023llama,
2 title={LLaMA: Open and Efficient Foundation Language Models},
3 author={Touvron, Hugo and Lavril, Thibaut and Izacard, Gautier and Martinet, Xavier and Lachaux, Marie-Anne and Lacroix, Timoth{\'e}e and Rozi{\`e}re, Baptiste and Goyal, Naman and Hambro, Eric and Azhar, Faisal and Rodriguez, Aurelien and Joulin, Armand and Grave, Edouard and Lample, Guillaume},
4 journal={arXiv preprint arXiv:2302.13971},
5 year={2023}
6}