Chat Completion API Using Sarvam Model

Overview

This notebook provides a step-by-step guide on how to use the Chat Completion API for generating text completions using Sarvam. It includes instructions for installation, setting up the API key, and making API calls to generate completions.

0. Installation

Before you begin, ensure you have the necessary Python libraries installed. Run the following commands to install the required packages:

1pip install requests

1. Import Required Libraries

This section imports the necessary Python libraries for making HTTP requests:

1import requests
  • requests: For making HTTP requests to the API.

2. Set Up the API Endpoint and Payload

To use the Sarvam API, you need an API key. Follow these steps to set up your API key:

  1. Obtain your API key: If you don’t have an API key, sign up on the Sarvam AI Dashboard to get one.
  2. Replace the placeholder key: In the code below, replace “YOUR_API_KEY_HERE” with your actual API key.
1import os
2
3# Replace with your actual API key
4SARVAM_API_KEY = "Bearer YOUR_API_KEY_HERE"
5SARVAM_API_URL = "https://api.sarvam.ai/v1/chat/completions"

2.1 Setting Up the API Headers and Payload

This section defines the headers and payload for the chat completion request:

1headers = {
2 "Authorization": SARVAM_API_KEY,
3 "Content-Type": "application/json",
4}
5
6payload = {
7 "model": "sarvam-m",
8 "messages": [
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "What is the capital of India?"},
11 ],
12 "temperature": 0.7,
13 "top_p": 1.0,
14 "max_tokens": 100,
15 "n": 1,
16}

3. Making the API Request

This section demonstrates how to make a request to the Chat Completion API and handle the response:

1def get_chat_completion(api_url, headers, payload):
2 try:
3 response = requests.post(api_url, headers=headers, json=payload)
4 if response.status_code == 200:
5 result = response.json()
6 return result
7 else:
8 print("Request failed:", response.status_code, response.text)
9 return None
10 except Exception as e:
11 print(f"Error making request: {e}")
12 return None

3.1 Sending the Request and Processing the Response

1# Make the API request
2response = get_chat_completion(SARVAM_API_URL, headers, payload)
3
4# Process and display the response
5if response:
6 reply = response["choices"][0]["message"]["content"]
7 print("Response:", reply)

Example output:

1{
2 "id": "20250526_816b21fd-98c4-42c8-884e-086bd9d059e8",
3 "model": "sarvam-m",
4 "created": 1748266512,
5 "usage": {
6 "prompt_tokens": 19,
7 "completion_tokens": 100,
8 "total_tokens": 119
9 },
10 "choices": [
11 {
12 "index": 0,
13 "finish_reason": "length",
14 "message": {
15 "role": "assistant",
16 "content": "The capital of India is **New Delhi**. It serves as the seat of the central government, housing key institutions like the Rashtrapati Bhavan (President's Office), Parliament, and the Supreme Court. While the British established Delhi as the capital in 1911, and it became the official capital of independent India in 1947, the city's history dates back much further, with roots in ancient civilizations and later rule by empires like the Delhi Sultanate."
17 }
18 }
19 ]
20}

4. Conclusion

This tutorial demonstrated how to use the Sarvam Chat Completion API for generating text completions. By following the steps, you can easily integrate the API into your applications for various use cases like chatbots, content generation, and more.

5. Additional Resources

For more details, refer to the official Sarvam API documentation and join the community for support:

6. Final Notes

  • Keep your API key secure
  • Adjust parameters like temperature and max_tokens based on your use case
  • Monitor your API usage and stay within your subscription limits

Keep Building! 🚀