Views
No views yet
vLLM >= v0.6.1.post1:pip install --upgrade vllmmistral_common >= 1.4.1 installed:pip install --upgrade mistral_common1from vllm import LLM
2from vllm.sampling_params import SamplingParams
3
4model_name = "mistralai/Mistral-Small-Instruct-2409"
5
6sampling_params = SamplingParams(max_tokens=8192)
7
8# note that running Mistral-Small on a single GPU requires at least 44 GB of GPU RAM
9# If you want to divide the GPU requirement over multiple devices, please add *e.g.* `tensor_parallel=2`
10llm = LLM(model=model_name, tokenizer_mode="mistral", config_format="mistral", load_format="mistral")
11
12prompt = "How often does the letter r occur in Mistral?"
13
14messages = [
15 {
16 "role": "user",
17 "content": prompt
18 },
19]
20
21outputs = llm.chat(messages, sampling_params=sampling_params)
22
23print(outputs[0].outputs[0].text)vllm serve mistralai/Mistral-Small-Instruct-2409 --tokenizer_mode mistral --config_format mistral --load_format mistral--tensor_parallel=2curl --location 'http://<your-node-url>:8000/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer token' \
--data '{
"model": "mistralai/Mistral-Small-Instruct-2409",
"messages": [
{
"role": "user",
"content": "How often does the letter r occur in Mistral?"
}
]
}'
mistral_inference >= 1.4.1 installed.pip install mistral_inference --upgrade1from huggingface_hub import snapshot_download
2from pathlib import Path
3
4mistral_models_path = Path.home().joinpath('mistral_models', '22B-Instruct-Small')
5mistral_models_path.mkdir(parents=True, exist_ok=True)
6
7snapshot_download(repo_id="mistralai/Mistral-Small-Instruct-2409", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)mistral_inference, a mistral-chat CLI command should be available in your environment. You can chat with the model usingmistral-chat $HOME/mistral_models/22B-Instruct-Small --instruct --max_tokens 2561from mistral_inference.transformer import Transformer
2from mistral_inference.generate import generate
3
4from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
5from mistral_common.protocol.instruct.messages import UserMessage
6from mistral_common.protocol.instruct.request import ChatCompletionRequest
7
8
9tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
10model = Transformer.from_folder(mistral_models_path)
11
12completion_request = ChatCompletionRequest(messages=[UserMessage(content="How often does the letter r occur in Mistral?")])
13
14tokens = tokenizer.encode_chat_completion(completion_request).tokens
15
16out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
17result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
18
19print(result)1from mistral_common.protocol.instruct.tool_calls import Function, Tool
2from mistral_inference.transformer import Transformer
3from mistral_inference.generate import generate
4
5from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
6from mistral_common.protocol.instruct.messages import UserMessage
7from mistral_common.protocol.instruct.request import ChatCompletionRequest
8
9
10tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
11model = Transformer.from_folder(mistral_models_path)
12
13completion_request = ChatCompletionRequest(
14 tools=[
15 Tool(
16 function=Function(
17 name="get_current_weather",
18 description="Get the current weather",
19 parameters={
20 "type": "object",
21 "properties": {
22 "location": {
23 "type": "string",
24 "description": "The city and state, e.g. San Francisco, CA",
25 },
26 "format": {
27 "type": "string",
28 "enum": ["celsius", "fahrenheit"],
29 "description": "The temperature unit to use. Infer this from the users location.",
30 },
31 },
32 "required": ["location", "format"],
33 },
34 )
35 )
36 ],
37 messages=[
38 UserMessage(content="What's the weather like today in Paris?"),
39 ],
40)
41
42tokens = tokenizer.encode_chat_completion(completion_request).tokens
43
44out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
45result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
46
47print(result)transformers library to run inference using various chat templates, or fine-tune the model.
Example for inference:1from transformers import LlamaTokenizerFast, MistralForCausalLM
2import torch
3
4device = "cuda"
5tokenizer = LlamaTokenizerFast.from_pretrained('mistralai/Mistral-Small-Instruct-2409')
6tokenizer.pad_token = tokenizer.eos_token
7
8model = MistralForCausalLM.from_pretrained('mistralai/Mistral-Small-Instruct-2409', torch_dtype=torch.bfloat16)
9model = model.to(device)
10
11prompt = "How often does the letter r occur in Mistral?"
12
13messages = [
14 {"role": "user", "content": prompt},
15 ]
16
17model_input = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(device)
18gen = model.generate(model_input, max_new_tokens=150)
19dec = tokenizer.batch_decode(gen)
20print(dec)1<s>
2 [INST]
3 How often does the letter r occur in Mistral?
4 [/INST]
5 To determine how often the letter "r" occurs in the word "Mistral,"
6 we can simply count the instances of "r" in the word.
7 The word "Mistral" is broken down as follows:
8 - M
9 - i
10 - s
11 - t
12 - r
13 - a
14 - l
15 Counting the "r"s, we find that there is only one "r" in "Mistral."
16 Therefore, the letter "r" occurs once in the word "Mistral."
17</s>