Update README.md
Browse files
README.md
CHANGED
|
@@ -368,6 +368,42 @@ After installing `mistral_inference`, a `mistral-chat` CLI command should be ava
|
|
| 368 |
mistral-chat $HOME/mistral_models/8B-Instruct --instruct --max_tokens 256
|
| 369 |
```
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
### Instruct following
|
| 372 |
|
| 373 |
```py
|
|
@@ -402,9 +438,13 @@ from mistral_inference.generate import generate
|
|
| 402 |
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 403 |
from mistral_common.protocol.instruct.messages import UserMessage
|
| 404 |
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
|
|
|
| 405 |
|
| 406 |
|
| 407 |
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
|
|
|
|
|
|
|
|
|
|
| 408 |
model = Transformer.from_folder(mistral_models_path)
|
| 409 |
|
| 410 |
completion_request = ChatCompletionRequest(
|
|
|
|
| 368 |
mistral-chat $HOME/mistral_models/8B-Instruct --instruct --max_tokens 256
|
| 369 |
```
|
| 370 |
|
| 371 |
+
### Passkey detection
|
| 372 |
+
|
| 373 |
+
```py
|
| 374 |
+
from mistral_inference.transformer import Transformer
|
| 375 |
+
from pathlib import Path
|
| 376 |
+
import json
|
| 377 |
+
from mistral_inference.generate import generate
|
| 378 |
+
from huggingface_hub import hf_hub_download
|
| 379 |
+
|
| 380 |
+
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 381 |
+
from mistral_common.protocol.instruct.messages import UserMessage
|
| 382 |
+
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
| 383 |
+
|
| 384 |
+
def load_passkey_request() -> ChatCompletionRequest:
|
| 385 |
+
passkey_file = hf_hub_download(repo_id="mistralai/Ministral-8B-Instruct-2410", filename="passkey_example.json")
|
| 386 |
+
|
| 387 |
+
with open(passkey_file, "r") as f:
|
| 388 |
+
data = json.load(f)
|
| 389 |
+
|
| 390 |
+
message_content = data["messages"][0]["content"]
|
| 391 |
+
return ChatCompletionRequest(messages=[UserMessage(content=message_content)])
|
| 392 |
+
|
| 393 |
+
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
|
| 394 |
+
model = Transformer.from_folder(mistral_models_path, softmax_fp32=False)
|
| 395 |
+
|
| 396 |
+
completion_request = load_passkey_request()
|
| 397 |
+
|
| 398 |
+
tokens = tokenizer.encode_chat_completion(completion_request).tokens
|
| 399 |
+
|
| 400 |
+
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
|
| 401 |
+
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
|
| 402 |
+
|
| 403 |
+
print(result)
|
| 404 |
+
```
|
| 405 |
+
|
| 406 |
+
|
| 407 |
### Instruct following
|
| 408 |
|
| 409 |
```py
|
|
|
|
| 438 |
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
| 439 |
from mistral_common.protocol.instruct.messages import UserMessage
|
| 440 |
from mistral_common.protocol.instruct.request import ChatCompletionRequest
|
| 441 |
+
from mistral_common.tokens.tokenizers.tekken import SpecialTokenPolicy
|
| 442 |
|
| 443 |
|
| 444 |
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
|
| 445 |
+
tekken = tokenizer.instruct_tokenizer.tokenizer
|
| 446 |
+
tekken.special_token_policy = SpecialTokenPolicy.IGNORE
|
| 447 |
+
|
| 448 |
model = Transformer.from_folder(mistral_models_path)
|
| 449 |
|
| 450 |
completion_request = ChatCompletionRequest(
|