Skip to content

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

langchain-sarvam

Overview

Integration details

Class Package Local Serializable JS support Downloads Version
ChatSarvam langchain-sarvam beta PyPI - Downloads PyPI - Version

Model features

Tool calling Structured output JSON mode Image input Audio input Video input Token-level streaming Native async Token usage Logprobs

Integration package connecting Sarvam AI chat completions with LangChain.

Installation

pip install langchain-sarvam

Or :

uv add langchain-sarvam

Setup

Set your Sarvam AI API key in your environment variables:

export SARVAM_API_KEY="your-api-key"

Or pass it in code:

import os
from langchain_sarvam import ChatSarvam

llm = ChatSarvam(model="sarvam-30b", sarvam_api_key=os.getenv("SARVAM_API_KEY"))

Usage

Basic Usage

from langchain_sarvam import ChatSarvam

llm = ChatSarvam(model="sarvam-30b", temperature=0.2, max_tokens=128)
resp = llm.invoke([("system", "You are helpful"), ("human", "Hello!")])
print(resp.content)

Tool Calling

Bind Python functions decorated with @tool to ChatSarvam. The model intelligently selects and formats arguments for the appropriate tool.

from langchain_core.tools import tool
from langchain_sarvam import ChatSarvam

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"32°C, Sunny in {city}"

@tool
def search_restaurants(city: str, cuisine: str) -> str:
    """Search for top-rated restaurants by cuisine type in a city."""
    return f"Top {cuisine} spots in {city}: Royal Kitchen"

# Initialize model and bind tools
llm = ChatSarvam(model="sarvam-30b", temperature=0)
model_with_tools = llm.bind_tools([get_weather, search_restaurants])

# Model generates tool call requests
response = model_with_tools.invoke("What's the weather in Mumbai?")
print(response.tool_calls)
# Output: [{'name': 'get_weather', 'args': {'city': 'Mumbai'}, 'id': '...', 'type': 'tool_call'}]

Structured Output

Extract formatted Pydantic objects or JSON using .with_structured_output(). Supports function_calling, json_schema, and json_mode.

from pydantic import BaseModel, Field
from langchain_sarvam import ChatSarvam

class AnswerWithJustification(BaseModel):
    """An answer along with justification."""
    answer: str = Field(description="The concise answer")
    justification: str = Field(description="Justification for the answer")

llm = ChatSarvam(model="sarvam-30b", temperature=0)

# Wrap LLM with structured output schema
structured_llm = llm.with_structured_output(AnswerWithJustification)

result = structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers?")
print("Answer:", result.answer)
print("Justification:", result.justification)

Agent Integration (create_agent)

Combine Tool Calling and Structured Output into an autonomous agent loop:

from pydantic import BaseModel, Field
from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_sarvam import ChatSarvam

class AnswerWithJustification(BaseModel):
    answer: str = Field(description="The concise answer")
    justification: str = Field(description="Justification for the answer")

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"32°C, Sunny in {city}"

llm = ChatSarvam(model="sarvam-30b", temperature=0)

# Create agent with tools and structured response format
agent = create_agent(
    model=llm,
    tools=[get_weather],
    response_format=AnswerWithJustification,
)

response = agent.invoke({
    "messages": [{"role": "user", "content": "What is the weather in Mumbai?"}]
})

# Access structured Pydantic object directly
result = response["structured_response"]
print("Answer:", result.answer)
print("Justification:", result.justification)

Batch Processing

from langchain_sarvam import ChatSarvam
from langchain_core.messages import HumanMessage

chat = ChatSarvam(model="sarvam-30b")

# Batch processing - use list of message lists
messages = [
    [HumanMessage(content="Tell me a joke")],
    [HumanMessage(content="What's the weather like?")]
]

responses = chat.batch(messages)
for response in responses:
    print(response.content)

Using generate() Method

from langchain_sarvam import ChatSarvam
from langchain_core.messages import HumanMessage

chat = ChatSarvam(model="sarvam-30b")

# generate() expects a list of message lists
inputs = [
    [HumanMessage(content="Tell me a joke with emojis only")],
    [HumanMessage(content="What's the weather like?")]
]

result = chat.generate(inputs)
for generation_list in result.generations:
    # generation_list is a list of ChatGeneration objects
    for generation in generation_list:
        print(generation.message.content)

Streaming

from langchain_sarvam import ChatSarvam

for chunk in ChatSarvam(model="sarvam-30b", streaming=True).stream("Tell me a joke"):
    print(chunk.content, end="")

About

An integration package connecting Sarvam-AI and LangChain

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages