File size: 2,988 Bytes
bb31303
b1d5ece
 
b90e21e
 
 
 
 
 
 
 
 
 
 
 
 
 
b1d5ece
bb31303
 
b90e21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad43e2e
b90e21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad43e2e
b90e21e
ad43e2e
 
b90e21e
 
 
 
 
 
 
 
 
ad43e2e
b90e21e
 
 
ad43e2e
b90e21e
bb31303
 
 
b90e21e
 
 
 
 
 
 
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
---
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:

```bash
pip install -U mistral-common transformers torch
```

## Usage Example

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

```python
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

- **Developed by:** Mistral AI
- **Model type:** Causal Language Model
- **Language(s):** English
- **License:**  [mrl](https://mistral.ai/licenses/MRL-0.1.md)
- **Resources for more information:**
  - [Model Repository](https://huggingface.co/prince-canuma/Ministral-8B-Instruct-2410-HF)
  - [Mistral AI GitHub](https://github.com/mistralai)