Views
No views yet
mistral_common1from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
2from mistral_common.protocol.instruct.messages import UserMessage
3from mistral_common.protocol.instruct.request import ChatCompletionRequest
4
5mistral_models_path = "MISTRAL_MODELS_PATH"
6
7tokenizer = MistralTokenizer.v3()
8
9completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
10
11tokens = tokenizer.encode_chat_completion(completion_request).tokensmistral_inference1from mistral_inference.transformer import Transformer
2from mistral_inference.generate import generate
3
4model = Transformer.from_folder(mistral_models_path)
5out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
6
7result = tokenizer.decode(out_tokens[0])
8
9print(result)transformers1from transformers import AutoModelForCausalLM
2
3model = AutoModelForCausalLM.from_pretrained("mistralai/Codestral-22B-v0.1")
4model.to("cuda")
5
6generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
7
8# decode with mistral tokenizer
9result = tokenizer.decode(generated_ids[0].tolist())
10print(result)[!TIP] PRs to correct thetransformerstokenizer so that it gives 1-to-1 the same results as themistral_commonreference implementation are very welcome!
mistralai/Codestral-22B-v0.1 with mistral-inference.pip install mistral_inference1from huggingface_hub import snapshot_download
2from pathlib import Path
3
4mistral_models_path = Path.home().joinpath('mistral_models', 'Codestral-22B-v0.1')
5mistral_models_path.mkdir(parents=True, exist_ok=True)
6
7snapshot_download(repo_id="mistralai/Codestral-22B-v0.1", 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.mistral-chat $HOME/mistral_models/Codestral-22B-v0.1 --instruct --max_tokens 256Sure, here's a simple implementation of a function that computes the Fibonacci sequence in Rust. This function takes an integer `n` as an argument and returns the `n`th Fibonacci number.
fn fibonacci(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}
fn main() {
let n = 10;
println!("The {}th Fibonacci number is: {}", n, fibonacci(n));
}
This function uses recursion to calculate the Fibonacci number. However, it's not the most efficient solution because it performs a lot of redundant calculations. A more efficient solution would use a loop to iteratively calculate the Fibonacci numbers.mistral_inference and running pip install --upgrade mistral_common to make sure to have mistral_common>=1.2 installed:1from mistral_inference.transformer import Transformer
2from mistral_inference.generate import generate
3from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
4from mistral_common.tokens.instruct.request import FIMRequest
5
6tokenizer = MistralTokenizer.v3()
7model = Transformer.from_folder("~/codestral-22B-240529")
8
9prefix = """def add("""
10suffix = """ return sum"""
11
12request = FIMRequest(prompt=prefix, suffix=suffix)
13
14tokens = tokenizer.encode_fim(request).tokens
15
16out_tokens, _ = generate([tokens], model, max_tokens=256, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
17result = tokenizer.decode(out_tokens[0])
18
19middle = result.split(suffix)[0].strip()
20print(middle)num1, num2):
# Add two numbers
sum = num1 + num2
# return the sumtransformers library, first run pip install -U transformers then use the snippet below to quickly get started:1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = "mistralai/Codestral-22B-v0.1"
4tokenizer = AutoTokenizer.from_pretrained(model_id)
5
6model = AutoModelForCausalLM.from_pretrained(model_id)
7
8text = "Hello my name is"
9inputs = tokenizer(text, return_tensors="pt")
10
11outputs = model.generate(**inputs, max_new_tokens=20)
12print(tokenizer.decode(outputs[0], skip_special_tokens=True))MNLP-0.1 license.