sarvam-m / README.md
mohit-sarvam's picture
Update README.md
3863ebc verified
|
Raw
History Blame
1.52 kB
metadata
library_name: transformers
license: apache-2.0
language:
  - en
  - bn
  - hi
  - kn
  - gu
  - mr
  - ml
  - or
  - pa
  - ta
  - te
base_model:
  - mistralai/Mistral-Small-3.1-24B-Instruct-2503

Quickstart

The following contains a code snippet illustrating how to use the model generate content based on given inputs.

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "sarvamai/sarvam-M"

# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name, torch_dtype="auto", device_map="auto"
)

# prepare the model input
prompt = "Who are you and what is your purpose on this planet?"

messages = [{"role": "user", "content": prompt}]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    enable_thinking=True,  # Switches between thinking and non-thinking modes. Default is True.
)

model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

# conduct text completion
generated_ids = model.generate(**model_inputs, max_new_tokens=8192)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]) :].tolist()
output_text = tokenizer.decode(output_ids)

if "</think>" in output_text:
    thinking_content = output_text.split("</think>")[0].rstrip("\n")
    content = output_text.split("</think>")[-1].lstrip("\n").rstrip("</s>")
else:
    thinking_content = ""
    content = output_text.rstrip("</s>")

print("thinking content:", thinking_content)
print("content:", content)