Views
No views yet
1from transformers import AutoModelForCausalLM
2from mistral_common.protocol.instruct.messages import (
3 AssistantMessage,
4 UserMessage,
5)
6from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
7from mistral_common.tokens.instruct.normalize import ChatCompletionRequest
8
9device = "cuda" # the device to load the model onto
10
11tokenizer_v3 = MistralTokenizer.v3()
12
13mistral_query = 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 model="test",
41)
42
43encodeds = tokenizer_v3.encode_chat_completion(mistral_query).tokens
44model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x22B-Instruct-v0.1")
45model_inputs = encodeds.to(device)
46model.to(device)
47
48generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
49sp_tokenizer = tokenizer_v3.instruct_tokenizer.tokenizer
50decoded = sp_tokenizer.decode(generated_ids[0])
51print(decoded)pip install mistral-common1from mistral_common.protocol.instruct.messages import (
2 AssistantMessage,
3 UserMessage,
4)
5from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
6from mistral_common.tokens.instruct.normalize import ChatCompletionRequest
7
8from transformers import AutoTokenizer
9
10tokenizer_v3 = MistralTokenizer.v3()
11
12mistral_query = ChatCompletionRequest(
13 messages=[
14 UserMessage(content="How many experts ?"),
15 AssistantMessage(content="8"),
16 UserMessage(content="How big ?"),
17 AssistantMessage(content="22B"),
18 UserMessage(content="Noice 🎉 !"),
19 ],
20 model="test",
21)
22hf_messages = mistral_query.model_dump()['messages']
23
24tokenized_mistral = tokenizer_v3.encode_chat_completion(mistral_query).tokens
25
26tokenizer_hf = AutoTokenizer.from_pretrained('mistralai/Mixtral-8x22B-Instruct-v0.1')
27tokenized_hf = tokenizer_hf.apply_chat_template(hf_messages, tokenize=True)
28
29assert tokenized_hf == tokenized_mistral