For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
CommunityAPI StatusAPI PricingSign Up
DocumentationAPI ReferencesCookbookIntegrationDeveloper Tools
DocumentationAPI ReferencesCookbookIntegrationDeveloper Tools
  • Getting Started
    • Welcome
    • Quickstart
    • SDKs & Libraries
    • Building for Indian Languages
    • Models
    • Credits & Rate Limits
    • Errors & Troubleshooting
    • Talk to us
    • Pricing
    • Changelog
  • API Guides & Tutorials
      • Overview
        • List your chat messages
        • Control response randomness
        • Control response diversity
        • Adjust the model's thinking level
        • Improve response factual accuracy
        • Encourage new topics in response
        • Reduce repetition words or phrases in response
        • Get repeatable results
        • Control the response length
        • Control where the model stops
LogoLogo
CommunityAPI StatusAPI PricingSign Up
On this page
  • Why is this important?
  • Roles explained:
  • Example: Listing messages in a conversation
API Guides & TutorialsChat CompletionHow-to

How to list your chat messages

||View as Markdown|
Was this page helpful?
Previous

How to control response randomness with temperature

Next
Built with

The messages parameter defines your entire conversation so far — this is how you “teach” the model what has happened in the chat.

Each message is an object with two fields:

KeyValue
role"system", "user", or "assistant"
contentThe message text (string)

Why is this important?

  • The model uses the conversation history to generate context-aware replies.
  • The order of the messages matters — the model reads them top to bottom.
  • Including previous assistant responses helps the model maintain coherence and memory.

Roles explained:

  • system (Optional, but Recommended)
    Sets initial behavior, tone, or instructions for the assistant.
    Example: "You are a helpful assistant."

  • user (Required)
    Represents questions, requests, or inputs from the user.
    Example: "Tell me about Indian classical music."

  • assistant (Optional, only for context in multi-turn)
    Contains previous replies from the model, which help it stay consistent in tone and content.
    Example: "Indian classical music is one of the oldest musical traditions..."

Example: Listing messages in a conversation

First, install the SDK:

$pip install -Uqq sarvamai

Then use the following Python code:

1from sarvamai import SarvamAI
2
3# Initialize the SarvamAI client with your API key
4client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
5
6# Example 1: Default example - single "user" message which is required (no prior context)
7response = client.chat.completions(
8 model="sarvam-105b",
9 messages=[
10 {"role": "user", "content": "Hey, what is the capital of India?"}
11 ],
12)
13
14print(response.choices[0].message.content)
1# Example 2: Multi-turn example — maintaining conversation context
2from sarvamai import SarvamAI
3
4client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")
5
6response = client.chat.completions(
7 model="sarvam-105b",
8 messages=[
9 {"role": "system", "content": "You are a helpful assistant."},
10 {"role": "user", "content": "Tell me about Indian classical music."},
11 {"role": "assistant", "content": "Indian classical music is one of the oldest musical traditions in the world, with roots in ancient texts like the Natya Shastra."},
12 {"role": "user", "content": "What are the two main styles?"}
13 ],
14)
15
16# Receive assistant's reply as output
17print(response.choices[0].message.content)