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.v1()
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/Mistral-7B-Instruct-v0.2")
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!
[INST] and [/INST] tokens. The very first instruction should begin with a begin of sentence id. The next instructions should not. The assistant generation will be ended by the end-of-sentence token id.text = "<s>[INST] What is your favourite condiment? [/INST]"
"Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!</s> "
"[INST] Do you have mayonnaise recipes? [/INST]"apply_chat_template() method:1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3device = "cuda" # the device to load the model onto
4
5model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
6tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
7
8messages = [
9 {"role": "user", "content": "What is your favourite condiment?"},
10 {"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
11 {"role": "user", "content": "Do you have mayonnaise recipes?"}
12]
13
14encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
15
16model_inputs = encodeds.to(device)
17model.to(device)
18
19generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
20decoded = tokenizer.batch_decode(generated_ids)
21print(decoded[0])Traceback (most recent call last):
File "", line 1, in
File "/transformers/models/auto/auto_factory.py", line 482, in from_pretrained
config, kwargs = AutoConfig.from_pretrained(
File "/transformers/models/auto/configuration_auto.py", line 1022, in from_pretrained
config_class = CONFIG_MAPPING[config_dict["model_type"]]
File "/transformers/models/auto/configuration_auto.py", line 723, in getitem
raise KeyError(key)
KeyError: 'mistral'