File size: 5,211 Bytes
face319 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a3a51bb2-faa9-47ef-8cc2-c33761bc16b6",
"metadata": {},
"outputs": [],
"source": [
"# Complete model usage guidelines available for reference, applicable to the MineMA-8B series models\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM\n",
"import torch\n",
"\n",
"model_path = \"\"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_path)\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" model_path,\n",
" torch_dtype=torch.bfloat16,\n",
" device_map = \"auto\",\n",
" # Or you can specify the device number to use like below\n",
" # device_map = {\"\": 0},\n",
")\n",
"\n",
"sys_mes = \"You are a Large Language Model, and your task is to answer questions posed by users about Minecraft. Utilize your knowledge and understanding of the game to provide detailed, accurate, and helpful responses. Use your capabilities to assist users in solving problems, understanding game mechanics, and enhancing their Minecraft experience.\"\n",
"user_mes = \"\"\"\n",
"How to get diamond in Minecraft?\n",
"\"\"\"\n",
"\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": sys_mes},\n",
" {\"role\": \"user\", \"content\": user_mes},\n",
"]\n",
"\n",
"input_ids = tokenizer.apply_chat_template(\n",
" messages,\n",
" add_generation_prompt=True,\n",
" return_tensors=\"pt\"\n",
").to(model.device)\n",
"\n",
"terminators = [\n",
" tokenizer.eos_token_id,\n",
" tokenizer.convert_tokens_to_ids(\"<|eot_id|>\")\n",
"]\n",
"# If using LLaMA-2-based model, use the following code\n",
"\"\"\"\n",
"terminators = [\n",
" tokenizer.eos_token_id,\n",
" tokenizer.convert_tokens_to_ids(\"</s>\")\n",
"]\n",
"\"\"\"\n",
"\n",
"outputs = model.generate(\n",
" input_ids,\n",
" max_new_tokens=256,\n",
" eos_token_id=terminators,\n",
" do_sample=True,\n",
" temperature=0.6,\n",
" top_p=0.9,\n",
" #repetition_penalty=1.3, # If there are duplicate problems with model responses, you can use this line of code \n",
")\n",
"response = outputs[0][input_ids.shape[-1]:]\n",
"print(tokenizer.decode(response, skip_special_tokens=True))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "634637b4-e04f-4d3a-a77e-712e53617249",
"metadata": {},
"outputs": [],
"source": [
"# LoRA model usage guidelines available for reference, applicable to the MineMA-70B series models\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"import torch\n",
"import json\n",
"from peft import PeftModel, LoraConfig, TaskType\n",
"\n",
"mode_path = '' # base model path\n",
"lora_path = '' # lora model path\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(mode_path)\n",
"\n",
"model = AutoModelForCausalLM.from_pretrained(mode_path, device_map=\"auto\",torch_dtype=torch.bfloat16)\n",
"\n",
"with open('config.json', 'r') as f:\n",
" lora_config_data = json.load(f)\n",
"\n",
"config = LoraConfig(**lora_config_data)\n",
"\n",
"model = PeftModel.from_pretrained(model, model_id=lora_path, config=config)\n",
"\n",
"prompt = \"How to get diamond in Minecraft?\"\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a Large Language Model, and your task is to answer questions posed by users about Minecraft. Utilize your knowledge and understanding of the game to provide detailed, accurate, and helpful responses. Use your capabilities to assist users in solving problems, understanding game mechanics, and enhancing their Minecraft experience.\"},\n",
" {\"role\": \"user\", \"content\":prompt}\n",
"]\n",
"\n",
"input_ids = tokenizer.apply_chat_template(\n",
" messages,\n",
" add_generation_prompt=True,\n",
" return_tensors=\"pt\"\n",
").to(model.device)\n",
"\n",
"terminators = [\n",
" tokenizer.eos_token_id,\n",
" tokenizer.convert_tokens_to_ids(\"<|eot_id|>\")\n",
"]\n",
"\n",
"outputs = model.generate(\n",
" input_ids,\n",
" max_new_tokens=256,\n",
" eos_token_id=terminators,\n",
" do_sample=True,\n",
" temperature=0.6,\n",
" top_p=0.9,\n",
" #repetition_penalty=1.3, # If there are duplicate problems with model responses, you can use this line of code \n",
")\n",
"response = outputs[0][input_ids.shape[-1]:]\n",
"print(tokenizer.decode(response, skip_special_tokens=True))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|