Views
No views yet
1!pip install -U bitsandbytes
2!pip install -U transformers
3!pip install -U accelerate
4!pip install -U datasets
5!pip install ipywidgets --upgrade1from transformers import (
2 AutoModelForCausalLM,
3 AutoTokenizer,
4 BitsAndBytesConfig,
5)
6import torch
7from tqdm import tqdm
8import json
9import re1# Write here your Hugging Face token
2HF_TOKEN = "{your Hugging Face token}"
3model_name = "kazugiri/llm-jp-3-13b-kgit1.1"1# QLoRA config
2bnb_config = BitsAndBytesConfig(
3 load_in_8bit=True,
4 bnb_8bit_quant_type="fp8",
5 bnb_8bit_compute_dtype=torch.float32,
6 bnb_8bit_use_double_quant=True,
7 use_llm_int8=True,
8)1# Load model
2model = AutoModelForCausalLM.from_pretrained(
3 model_name,
4 quantization_config=bnb_config,
5 device_map="auto",
6 token = HF_TOKEN
7)
8
9# Load tokenizer
10tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)1datasets = []
2with open("{your task data jsonl file path}", "r") as f:
3 item = ""
4 for line in f:
5 line = line.strip()
6 item += line
7 if item.endswith("}"):
8 datasets.append(json.loads(item))
9 item = ""1results = []
2for data in tqdm(datasets):
3
4 input = data["input"]
5
6 prompt = f"""あなたは、読解力と共感力の高い、信頼できるロボットアシスタントです。ユーザーの指示を正確に理解し、具体的で的確な回答を提供します。必要に応じて追加の情報や説明を提供し、ユーザーの問題解決を助けます。
7
8以下は指示と回答の例です:
9
10---
11
12### 指示:
13日本の首都はどこですか?
14
15### 回答:
16日本の首都は東京です。
17
18---
19
20### 指示:
21猫と犬の違いを3つ教えてください。
22
23### 回答:
241. **社会性の違い**: 猫は単独行動を好む傾向がありますが、犬は群れで行動する社会的な動物です。
252. **運動能力の違い**: 猫は高い場所に登ったり、ジャンプしたりするのが得意ですが、犬は持久力があり、長距離を走るのが得意です。
263. **コミュニケーションの違い**: 猫はしっぽや鳴き声で感情を表現しますが、犬は表情や体全体で感情を伝えます。
27
28---
29
30### 指示:
31「break the ice」というイディオムを使った英語の例文を作成してください。
32
33### 回答:
34She told a funny story to break the ice at the meeting.
35
36---
37
38### 指示:
39次の対話を読み、その状況を説明してください。
40
41A: 「飛行機に乗り遅れるなんて信じられない!」
42B: 「本当だよ。あと10分早く家を出ていればよかったのに。」
43A: 「次の便まで待たなきゃいけなくなったね。」
44
45### 回答:
46このダイアログの状況は、AとBが飛行機に乗り遅れてしまったことです。二人は家を出るのがもう少し早ければ間に合ったのですが、今は次の飛行機を待つ必要がある状況にあります。
47
48---
49
50### 指示:
51{input}
52
53### 回答:
54 """
55
56 tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
57 with torch.no_grad():
58 outputs = model.generate(
59 tokenized_input,
60 max_new_tokens=500,
61 do_sample=True,
62 temperature=0.8,
63 top_p=0.95,
64 num_beams=3,
65 no_repeat_ngram_size=3,
66 length_penalty=1.2,
67 repetition_penalty=1.5
68 )[0]
69 output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
70
71 results.append({"task_id": data["task_id"], "input": input, "output": output})1# get the answers as a jsonl file
2model_name = re.sub(".*/", "", model_name)
3with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
4 for result in results:
5 json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
6 f.write('\n')