What is Sarvam AI?
Sarvam AI is an Indian Generative AI platform that provides Large Language Models (LLMs), speech AI, translation, voice agents, and AI APIs for developers.
Think of Sarvam AI as India's version of services like:
- OpenAI
- Anthropic
- Google Gemini
Using Sarvam APIs, developers can:
- Build AI chatbots
- Generate text
- Summarize documents
- Translate Indian languages
- Create voice assistants
- Build customer support systems
- Integrate AI into websites and mobile apps
Why Use Sarvam AI?
Benefits
β Indian Language Support
β Simple API Integration
β Fast Response
β Production Ready
β Multiple AI Models
β Voice and Speech Capabilities
β Developer Friendly SDKs
Architecture Overview
User β Your Application β Sarvam API β AI Model β Response
Example:
User asks:
"Explain Artificial Intelligence"
Your backend sends request to Sarvam.
Sarvam processes the request using an LLM.
The AI generates an answer.
The answer is returned to your application.
Step 1: Create a Sarvam Account
Visit:
Create an account using:
- Google Login
- Email Login
After verification, access the developer dashboard.
Step 2: Generate an API Key
After login:
- Open Dashboard
- Go to API Keys
- Click Create API Key
- Copy the generated key
Example:
sk_xxxxxxxxxxxxxxxxxxxxx
β οΈ Never expose your API key publicly.
Bad:
const apiKey = "sk_xxxxxxxxx";
Good:
SARVAM_API_KEY=sk_xxxxxxxxx
Step 3: Install SDK
JavaScript / Node.js
npm install sarvamai
or
yarn add sarvamai
Python
pip install sarvamai
Understanding Chat Completions
A Chat Completion API allows you to send messages to an AI model and receive a response.
Example:
User:
What is Machine Learning?
AI:
Machine Learning is a branch of AI...
JavaScript Example
Basic Setup
import { SarvamAIClient } from "sarvamai";
const client = new SarvamAIClient({
apiSubscriptionKey: process.env.SARVAM_API_KEY,
});
Complete Example
import { SarvamAIClient } from "sarvamai";
import dotenv from "dotenv";
dotenv.config();
const client = new SarvamAIClient({
apiSubscriptionKey: process.env.SARVAM_API_KEY,
});
async function main() {
try {
const response = await client.chat.completions({
model: "sarvam-30b",
messages: [
{
role: "user",
content: "Explain the significance of the Indian monsoon season.",
},
],
temperature: 0.5,
top_p: 1,
max_tokens: 1000,
});
console.log(response.choices[0].message.content);
} catch (error) {
console.error(error);
}
}
main();
Understanding Every Parameter
model
model: "sarvam-30b"
Specifies which AI model to use.
Think of it as selecting the AI brain.
messages
messages: [
{
role: "user",
content: "Hello"
}
]
Conversation history sent to the AI.
role
Three common roles:
user
Question from user.
{
role: "user",
content: "What is AI?"
}
assistant
Previous AI response.
{
role: "assistant",
content: "AI stands for Artificial Intelligence."
}
system
Instructions for AI behavior.
{
role: "system",
content: "You are a helpful teacher."
}
temperature
temperature: 0.5
Controls creativity.
Low Temperature
temperature: 0.1
More factual and predictable.
Medium Temperature
temperature: 0.5
Balanced output.
High Temperature
temperature: 1
More creative responses.
top_p
top_p: 1
Controls how many probable words can be selected.
Example
Predictions:
Coffee = 40%
Tea = 30%
Milk = 20%
Water = 10%
If:
top_p = 0.8
AI may only consider:
Coffee
Tea
Milk
max_tokens
max_tokens: 1000
Maximum response length.
Higher value:
- Longer answers
- Higher cost
- More tokens consumed
Multi-Turn Conversation
const response = await client.chat.completions({
model: "sarvam-30b",
messages: [
{
role: "system",
content: "You are a helpful coding mentor.",
},
{
role: "user",
content: "What is JavaScript?",
},
{
role: "assistant",
content: "JavaScript is a programming language.",
},
{
role: "user",
content: "Give me an example.",
},
],
});
The AI remembers previous messages in the conversation.
Python Example
Basic Setup
from sarvamai import SarvamAIClient
import os
client = SarvamAIClient(
api_subscription_key=os.getenv("SARVAM_API_KEY")
)
Complete Python Example
from sarvamai import SarvamAIClient
import os
client = SarvamAIClient(
api_subscription_key=os.getenv("SARVAM_API_KEY")
)
response = client.chat.completions(
model="sarvam-30b",
messages=[
{
"role": "user",
"content": "Explain Artificial Intelligence"
}
],
temperature=0.5,
top_p=1,
max_tokens=500
)
print(response.choices[0].message.content)
Express.js API Example
Create your own AI endpoint.
import express from "express";
import dotenv from "dotenv";
import { SarvamAIClient } from "sarvamai";
dotenv.config();
const app = express();
app.use(express.json());
const client = new SarvamAIClient({
apiSubscriptionKey: process.env.SARVAM_API_KEY,
});
app.post("/chat", async (req, res) => {
try {
const { message } = req.body;
const response = await client.chat.completions({
model: "sarvam-30b",
messages: [
{
role: "user",
content: message,
},
],
});
return res.json({
success: true,
response: response.choices[0].message.content,
});
} catch (error) {
return res.status(500).json({
success: false,
error: error.message,
});
}
});
app.listen(3000);
Error Handling
Always use try-catch.
try {
const response = await client.chat.completions({
model: "sarvam-30b",
messages,
});
} catch (error) {
console.error(error);
}
Common errors:
Invalid API Key
401 Unauthorized
Rate Limit
429 Too Many Requests
Server Error
500 Internal Server Error
Best Practices
Use Environment Variables
SARVAM_API_KEY=your_key
Validate Input
if (!message) {
return res.status(400).json({
error: "Message required",
});
}
Limit Token Usage
max_tokens: 300
Prevents unnecessary cost.
Add Logging
console.log("Request received");
Useful for debugging.
Real-World Use Cases
Customer Support Bot
User β Question
AI β Answer
Internal Company Assistant
Employees β Ask Questions
AI β Company Knowledge
FAQ Generator
Document β AI
AI β FAQ List
Content Generation
Topic β AI
AI β Blog Post
Education Platform
Student Question β AI
AI β Explanation
Top comments (0)