Views
No views yet
sarvam-m is a multilingual, hybrid-reasoning, text-only language model built on Mistral-Small. This post-trained version delivers exceptional improvements over the base model:sarvam-m using Transformers.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "sarvamai/sarvam-m"
4
5# load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name, torch_dtype="auto", device_map="auto"
9)
10
11# prepare the model input
12prompt = "Who are you and what is your purpose on this planet?"
13
14messages = [{"role": "user", "content": prompt}]
15text = tokenizer.apply_chat_template(
16 messages,
17 tokenize=False,
18 enable_thinking=True, # Switches between thinking and non-thinking modes. Default is True.
19)
20
21model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
22
23# conduct text completion
24generated_ids = model.generate(**model_inputs, max_new_tokens=8192)
25output_ids = generated_ids[0][len(model_inputs.input_ids[0]) :].tolist()
26output_text = tokenizer.decode(output_ids)
27
28if "</think>" in output_text:
29 reasoning_content = output_text.split("</think>")[0].rstrip("\n")
30 content = output_text.split("</think>")[-1].lstrip("\n").rstrip("</s>")
31else:
32 reasoning_content = ""
33 content = output_text.rstrip("</s>")
34
35print("reasoning content:", reasoning_content)
36print("content:", content)[!NOTE] For thinking mode, we recommendtemperature=0.5; for no-think mode,temperature=0.2.
1from openai import OpenAI
2
3base_url = "https://api.sarvam.ai/v1"
4model_name = "sarvam-m"
5api_key = "Your-API-Key" # get it from https://dashboard.sarvam.ai/
6
7client = OpenAI(
8 base_url=base_url,
9 api_key=api_key,
10).with_options(max_retries=1)
11
12messages = [
13 {"role": "system", "content": "You're a helpful AI assistant"},
14 {"role": "user", "content": "Explain quantum computing in simple terms"},
15]
16
17response1 = client.chat.completions.create(
18 model=model_name,
19 messages=messages,
20 reasoning_effort="medium", # Enable thinking mode. `None` for disable.
21 max_completion_tokens=4096,
22)
23print("First response:", response1.choices[0].message.content)
24
25# Building messages for the second turn (using previous response as context)
26messages.extend(
27 [
28 {
29 "role": "assistant",
30 "content": response1.choices[0].message.content,
31 },
32 {"role": "user", "content": "Can you give an analogy for superposition?"},
33 ]
34)
35
36response2 = client.chat.completions.create(
37 model=model_name,
38 messages=messages,
39 reasoning_effort="medium",
40 max_completion_tokens=8192,
41)
42print("Follow-up response:", response2.choices[0].message.content)reasoning_effort can take three possible values: low, medium, and high to be consistent with the OpenAI API spec. Setting any of the three values just enables the thinking mode of sarvam-m.vllm>=0.8.5 and create an OpenAI-compatible API endpoint with vllm serve sarvamai/sarvam-m.1from openai import OpenAI
2
3# Modify OpenAI's API key and API base to use vLLM's API server.
4openai_api_key = "EMPTY"
5openai_api_base = "http://localhost:8000/v1"
6
7client = OpenAI(
8 api_key=openai_api_key,
9 base_url=openai_api_base,
10)
11
12models = client.models.list()
13model = models.data[0].id
14
15messages = [{"role": "user", "content": "Why is 42 the best number?"}]
16
17# By default, thinking mode is enabled.
18# If you want to disable thinking, add:
19# extra_body={"chat_template_kwargs": {"enable_thinking": False}}
20response = client.chat.completions.create(model=model, messages=messages)
21output_text = response.choices[0].message.content
22
23if "</think>" in output_text:
24 reasoning_content = output_text.split("</think>")[0].rstrip("\n")
25 content = output_text.split("</think>")[-1].lstrip("\n")
26else:
27 reasoning_content = ""
28 content = output_text
29
30print("reasoning content:", reasoning_content)
31print("content:", content)
32
33# For the next round, add the model's response directly as assistant turn.
34messages.append(
35 {"role": "assistant", "content": output_text}
36)