{ "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(\"\")\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 }