Upload README.md
Browse files
README.md
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: other
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
pipeline_tag: text-generation
|
6 |
+
inference: false
|
7 |
+
tags:
|
8 |
+
- transformers
|
9 |
+
- gguf
|
10 |
+
- imatrix
|
11 |
+
- Mistral-7B-Instruct-v0.3
|
12 |
+
---
|
13 |
+
Quantizations of https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3
|
14 |
+
|
15 |
+
|
16 |
+
# From original readme
|
17 |
+
|
18 |
+
## Installation
|
19 |
+
|
20 |
+
It is recommended to use `mistralai/Mistral-7B-Instruct-v0.3` with [mistral-inference](https://github.com/mistralai/mistral-inference). For HF transformers code snippets, please keep scrolling.
|
21 |
+
|
22 |
+
```
|
23 |
+
pip install mistral_inference
|
24 |
+
```
|
25 |
+
|
26 |
+
## Download
|
27 |
+
|
28 |
+
```py
|
29 |
+
from huggingface_hub import snapshot_download
|
30 |
+
from pathlib import Path
|
31 |
+
|
32 |
+
mistral_models_path = Path.home().joinpath('mistral_models', '7B-Instruct-v0.3')
|
33 |
+
mistral_models_path.mkdir(parents=True, exist_ok=True)
|
34 |
+
|
35 |
+
snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.3", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
|
36 |
+
```
|
37 |
+
|
38 |
+
### Chat
|
39 |
+
|
40 |
+
After installing `mistral_inference`, a `mistral-chat` CLI command should be available in your environment. You can chat with the model using
|
41 |
+
|
42 |
+
```
|
43 |
+
mistral-chat $HOME/mistral_models/7B-Instruct-v0.3 --instruct --max_tokens 256
|
44 |
+
```
|
45 |
+
|
46 |
+
### Instruct following
|
47 |
+
|
48 |
+
```py
|
49 |
+
from mistral_inference.model import Transformer
|
50 |
+
from mistral_inference.generate import generate
|
51 |
+
|
52 |
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
53 |
+
from mistral_common.protocol.instruct.messages import UserMessage
|
54 |
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
55 |
+
|
56 |
+
|
57 |
+
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
|
58 |
+
model = Transformer.from_folder(mistral_models_path)
|
59 |
+
|
60 |
+
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
|
61 |
+
|
62 |
+
tokens = tokenizer.encode_chat_completion(completion_request).tokens
|
63 |
+
|
64 |
+
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
65 |
+
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
66 |
+
|
67 |
+
print(result)
|
68 |
+
```
|
69 |
+
|
70 |
+
### Function calling
|
71 |
+
|
72 |
+
```py
|
73 |
+
from mistral_common.protocol.instruct.tool_calls import Function, Tool
|
74 |
+
from mistral_inference.model import Transformer
|
75 |
+
from mistral_inference.generate import generate
|
76 |
+
|
77 |
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
78 |
+
from mistral_common.protocol.instruct.messages import UserMessage
|
79 |
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
80 |
+
|
81 |
+
|
82 |
+
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
|
83 |
+
model = Transformer.from_folder(mistral_models_path)
|
84 |
+
|
85 |
+
completion_request = ChatCompletionRequest(
|
86 |
+
tools=[
|
87 |
+
Tool(
|
88 |
+
function=Function(
|
89 |
+
name="get_current_weather",
|
90 |
+
description="Get the current weather",
|
91 |
+
parameters={
|
92 |
+
"type": "object",
|
93 |
+
"properties": {
|
94 |
+
"location": {
|
95 |
+
"type": "string",
|
96 |
+
"description": "The city and state, e.g. San Francisco, CA",
|
97 |
+
},
|
98 |
+
"format": {
|
99 |
+
"type": "string",
|
100 |
+
"enum": ["celsius", "fahrenheit"],
|
101 |
+
"description": "The temperature unit to use. Infer this from the users location.",
|
102 |
+
},
|
103 |
+
},
|
104 |
+
"required": ["location", "format"],
|
105 |
+
},
|
106 |
+
)
|
107 |
+
)
|
108 |
+
],
|
109 |
+
messages=[
|
110 |
+
UserMessage(content="What's the weather like today in Paris?"),
|
111 |
+
],
|
112 |
+
)
|
113 |
+
|
114 |
+
tokens = tokenizer.encode_chat_completion(completion_request).tokens
|
115 |
+
|
116 |
+
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
117 |
+
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
118 |
+
|
119 |
+
print(result)
|
120 |
+
```
|
121 |
+
|
122 |
+
## Generate with `transformers`
|
123 |
+
|
124 |
+
If you want to use Hugging Face `transformers` to generate text, you can do something like this.
|
125 |
+
|
126 |
+
```py
|
127 |
+
from transformers import pipeline
|
128 |
+
|
129 |
+
messages = [
|
130 |
+
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
|
131 |
+
{"role": "user", "content": "Who are you?"},
|
132 |
+
]
|
133 |
+
chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
|
134 |
+
chatbot(messages)
|
135 |
+
```
|