Views
No views yet
1from transformers import AutoModelForCausalLM
2from mistral_common.protocol.instruct.messages import (
3 AssistantMessage,
4 UserMessage,
5)
6from mistral_common.protocol.instruct.tool_calls import (
7 Tool,
8 Function,
9)
10from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
11from mistral_common.tokens.instruct.normalize import ChatCompletionRequest
12
13device = "cuda" # the device to load the model onto
14
15tokenizer_v3 = MistralTokenizer.v3()
16
17mistral_query = ChatCompletionRequest(
18 tools=[
19 Tool(
20 function=Function(
21 name="get_current_weather",
22 description="Get the current weather",
23 parameters={
24 "type": "object",
25 "properties": {
26 "location": {
27 "type": "string",
28 "description": "The city and state, e.g. San Francisco, CA",
29 },
30 "format": {
31 "type": "string",
32 "enum": ["celsius", "fahrenheit"],
33 "description": "The temperature unit to use. Infer this from the users location.",
34 },
35 },
36 "required": ["location", "format"],
37 },
38 )
39 )
40 ],
41 messages=[
42 UserMessage(content="What's the weather like today in Paris"),
43 ],
44 model="test",
45)
46
47encodeds = tokenizer_v3.encode_chat_completion(mistral_query).tokens
48model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-8x22B-Instruct-v0.1")
49model_inputs = encodeds.to(device)
50model.to(device)
51
52generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
53sp_tokenizer = tokenizer_v3.instruct_tokenizer.tokenizer
54decoded = sp_tokenizer.decode(generated_ids[0])
55print(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