prince-canuma's picture
Upload tokenizer
b1d5ece verified
|
raw
history blame
2.99 kB
metadata
base_model:
  - mistralai/Ministral-8B-Instruct-2410
language:
  - en
  - fr
  - de
  - es
  - it
  - pt
  - zh
  - ja
  - ru
  - ko
license: other
license_name: mrl
license_link: https://mistral.ai/licenses/MRL-0.1.md
inference: false

Installation

To use this model, install the required packages:

pip install -U mistral-common transformers torch

Usage Example

Here's a Python script demonstrating how to use the model for chat completion:

import torch
from pathlib import Path
from transformers import AutoModelForCausalLM
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.tokens.tokenizers.tekken import SpecialTokenPolicy
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
from huggingface_hub import snapshot_download

def get_model_path(path_or_hf_repo: str, revision: str = None) -> Path:
    """Ensures the model is available locally, downloading if necessary."""
    model_path = Path(path_or_hf_repo)
    if not model_path.exists():
        model_path = Path(
            snapshot_download(
                repo_id=path_or_hf_repo,
                revision=revision,
                allow_patterns=[
                    "*.json",
                    "*.safetensors",
                    "*.py",
                    "tokenizer.model",
                    "*.tiktoken",
                    "*.txt",
                ],
                resume_download=True,
            )
        )
    return model_path

def load_chat_request(message: str) -> ChatCompletionRequest:
    """Creates a chat completion request with a user message."""
    return ChatCompletionRequest(messages=[UserMessage(content=message)])

# Model setup
model_name = "prince-canuma/Ministral-8B-Instruct-2410-HF"
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
model_path = get_model_path(model_name)

tokenizer = MistralTokenizer.from_file(f"{model_path}/tokenizer.json")
tekken = tokenizer.instruct_tokenizer.tokenizer
tekken.special_token_policy = SpecialTokenPolicy.IGNORE

# Chat interaction
user_message = "Tell me a short story about a robot learning to paint."
completion_request = load_chat_request(user_message)
tokens = tokenizer.encode_chat_completion(completion_request).tokens
input_ids = torch.tensor(tokens).unsqueeze(0)

# Generate response
output = model.generate(input_ids, max_new_tokens=500, temperature=0.7, do_sample=True)
response = tokenizer.decode(output[0][input_ids.shape[1]:].tolist())

print("User:", user_message)
print("Model:", response)

Model Details