Performance gains are even more impressive at the intersection of Indian languages and mathematics, with an outstanding +86% improvement in romanized Indian language GSM-8K benchmarks.
Learn more about sarvam-m in our detailed
blog post.
-
Hybrid Thinking Mode: A single versatile model supporting both "think" and "non-think" modes. Use the think mode for complex logical reasoning, mathematical problems, and coding tasks, or switch to non-think mode for efficient, general-purpose conversation.
-
Advanced Indic Skills: Specifically post-trained on Indian languages alongside English, embodying a character that authentically reflects and emphasizes Indian cultural values.
-
Superior Reasoning Capabilities: Outperforms most similarly-sized models on coding and math benchmarks, demonstrating exceptional reasoning abilities.
-
Seamless Chatting Experience: Full support for both Indic scripts and romanized versions of Indian languages, providing a smooth and accessible multilingual conversation experience.
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)
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)
If you want to use vLLM with python, you can do the following.
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)