A Python SDK for building multilingual, context-aware conversational agents on Sarvam's Samvaad platform.
Project description
Sarvam Conv AI SDK
The Sarvam Conversational AI SDK is a Python package that helps developers build and extend conversational agents. It provides core components to manage conversation flow, language preferences, and messaging, making it easier to develop interactive and context-aware AI experiences.
Overview
The Sarvam Conv AI SDK enables developers to create tools that can:
- Facilitate agentic capabilities like API calling in the middle of a conversation.
- Manage agent-specific variables and state across interactions
- Control and modify the language used during conversations
- Send dynamic messages to both the user and the underlying language model (LLM)
Installation
Install the SDK via pip:
pip install sarvam-conv-ai-sdk
Example Usage
import httpx
from pydantic import Field
from sarvam_conv_ai_sdk import (
SarvamToolLanguageName,
SarvamOnEndTool,
SarvamOnStartTool,
SarvamTool,
SarvamOnStartToolContext,
SarvamToolContext,
SarvamOnEndToolContext,
SarvamToolOutput
)
class OnStart(SarvamOnStartTool):
async def run(self, context: SarvamOnStartToolContext):
user_id = context.get_user_identifier()
async with httpx.AsyncClient() as client:
response = await client.get(f"https://sarvam-flights.com/users/{user_id}")
response.raise_for_status()
user_data = response.json()
source_destination = user_data.get("home_city")
context.set_agent_variable("source_destination", source_destination)
context.set_agent_variable("passenger_name", user_data.get("name"))
context.set_initial_language_name(SarvamToolLanguageName.ENGLISH)
context.set_initial_bot_message(
f"Hello! Would you like to book a flight from {source_destination}? Where would you like to go?",
)
return context
class BookFlight(SarvamTool):
"""Book a flight based on the user's travel preferences."""
destination: str = Field(description="City of destination")
travel_date: str = Field(description="Date of travel (YYYY-MM-DD)")
async def run(self, context: SarvamToolContext) -> SarvamToolOutput:
source_destination = context.get_agent_variable("source_destination")
booking_data = {
"source": source_destination,
"destination": self.destination,
"travel_date": self.travel_date,
"passenger_name": context.get_agent_variable("passenger_name"),
}
async with httpx.AsyncClient() as client:
response = await client.post(
"https://sarvam-flights.com/book", json=booking_data
)
response.raise_for_status()
booking_result = response.json()
if booking_result.get("status") == "confirmed":
context.set_agent_variable("booking_id", booking_result.get("booking_id"))
context.set_end_conversation()
return SarvamToolOutput(
message_to_user=f"Flight booked successfully to {self.destination}!",
context=context
)
else:
return SarvamToolOutput(
message_to_llm="Booking failed. Please suggest similar destinations.",
context=context
)
class OnEnd(SarvamOnEndTool):
async def run(self, context: SarvamOnEndToolContext):
feedback = context.get_agent_variable("feedback")
negative_words = ["bad", "poor", "disappointed", "unhappy", "problem"]
is_negative = any(word in feedback.lower() for word in negative_words)
context.set_agent_variable("feedback_sentiment", is_negative)
return context
Base Classes
The SDK exposes three base classes for tool development:
1. SarvamTool
Primary base class for all operational tools invoked during conversation flow.
Example:
class MyCustomTool(SarvamTool):
"""Brief description of the tool's purpose."""
tool_variable: type = Field(description="Description of this input parameter")
async def run(self, context: SarvamToolContext) -> SarvamToolOutput:
# Custom tool logic
return SarvamToolOutput(
message_to_user="Response to user",
message_to_llm="Context for LLM",
context=context
)
2. SarvamOnStartTool
Executed at the beginning of a conversation, typically for initialization. The class must be named OnStart.
3. SarvamOnEndTool
Executed at the end of a conversation, typically for cleanup or post-processing. The class must be named OnEnd.
Context Classes and Methods
SarvamToolContext
The context object passed to SarvamTool.run() methods.
Variable Management
-
get_agent_variable(variable_name: str) -> AnyRetrieve the value of a variable. -
set_agent_variable(variable_name: str, value: Any) -> NoneUpdate a variable's value.
Language Control
-
get_current_language() -> SarvamToolLanguageNameReturns the current language preference. -
change_language(language: SarvamToolLanguageName) -> NoneUpdate the language preference.
Conversation Flow
set_end_conversation() -> NoneExplicitly end the conversation.
SarvamOnStartToolContext
The context object passed to SarvamOnStartTool.run() methods.
Variable Management
-
get_agent_variable(variable_name: str) -> AnyRetrieve the value of a variable. -
set_agent_variable(variable_name: str, value: Any) -> NoneUpdate a variable's value.
User Information
get_user_identifier() -> strGet the user identifier.
Initialization Methods
-
set_initial_bot_message(message: str) -> NoneSet the first message sent by the agent when the conversation starts. -
set_initial_state_name(state_name: str) -> NoneSet the initial state from which the agent should start. -
set_initial_language_name(language: SarvamToolLanguageName) -> NoneDefine the initial language preference for the user.
SarvamOnEndToolContext
The context object passed to SarvamOnEndTool.run() methods.
Variable Management
-
get_agent_variable(variable_name: str) -> AnyRetrieve the value of a variable. -
set_agent_variable(variable_name: str, value: Any) -> NoneUpdate a variable's value.
User Information
get_user_identifier() -> strGet the user identifier.
Interaction Transcript
get_interaction_transcript() -> Optional[InteractionTranscript]Retrieve the conversation history containing user and agent messages in English.
Example transcript:
[
InteractionTurn(role=<InteractionTurnRole.AGENT: 'agent'>, en_text='Hello! How can I help you today?'),
InteractionTurn(role=<InteractionTurnRole.USER: 'user'>, en_text='I need to book a flight'),
InteractionTurn(role=<InteractionTurnRole.AGENT: 'agent'>, en_text='I can help you with that. Where would you like to go?'),
InteractionTurn(role=<InteractionTurnRole.USER: 'user'>, en_text='I want to go to Mumbai'),
InteractionTurn(role=<InteractionTurnRole.AGENT: 'agent'>, en_text='Great! When would you like to travel?')
]
Return Types
SarvamToolOutput
The return type for SarvamTool.run() methods. Contains:
message_to_user: Optional[str]- Message that is sent directly to the usermessage_to_llm: Optional[str]- Message that is sent to the LLM, which then respondscontext: SarvamToolContext- The updated context object
Note: At least one of message_to_llm or message_to_user must be set.
Important: When both message_to_user and message_to_llm are set, only the message_to_user is actually sent to the user, but the message_to_llm overrides the message_to_user when adding to the chat thread for the LLM's context.
Supported Languages
The SDK supports multilingual conversations using the SarvamToolLanguageName enum. Available languages include:
- Bengali
- Gujarati
- Kannada
- Malayalam
- Tamil
- Telugu
- Punjabi
- Odia
- Marathi
- Hindi
- English
Best Practices
- Always implement
run(): Therun()method is the entry point for tool execution logic. - Use
Field()for parameters: Ensures type safety and adds descriptive metadata necessary for LLM to use in the prompt. - Gracefully handle errors: Avoid accessing unset variables or using invalid types.
- Return the appropriate type:
SarvamTool.run()must returnSarvamToolOutput, whileSarvamOnStartTool.run()andSarvamOnEndTool.run()return their respective context objects. - Write meaningful docstrings: Clearly describe what each tool is intended to do as this directly impacts the performance of tool calling capabilities of the agent.
- Use async operations for I/O: For the best performance, use
async/awaitfor external API calls to avoid blocking. - Use context methods: Use the provided context methods for variable management, language control, and messaging instead of directly accessing context attributes.
Error Handling
The SDK includes built-in error handling for common scenarios:
- Variable not found: Raises ValueError when accessing undefined variables
- Variable not defined: Raises ValueError when setting variables that haven't been initialized
- Non-serializable values: Raises ValueError when variable values cannot be JSON serialized
- Invalid output: Raises ValueError when
SarvamToolOutputis created without at least one message
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sarvam_conv_ai_sdk-1.0.1.tar.gz.
File metadata
- Download URL: sarvam_conv_ai_sdk-1.0.1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23abac439cbd311b7b63a6e050936d1c31eccf9d2afa665e36c3f72eed38ee0e
|
|
| MD5 |
c4deea919675254ea1a1fe77d9e8deb3
|
|
| BLAKE2b-256 |
8fbd441746de2702493795b06f97e16e7b44b674b57e7a3de6114174461f458b
|
File details
Details for the file sarvam_conv_ai_sdk-1.0.1-py3-none-any.whl.
File metadata
- Download URL: sarvam_conv_ai_sdk-1.0.1-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d3e427c53ff97f50f5d1521b70fef2ca4fd43b9fbfe19ceb228a652f9b8b113
|
|
| MD5 |
3ad7bfa0baaf567d37420ec8f2bb4a44
|
|
| BLAKE2b-256 |
c50e7cb8d1a5ed41a286404e1a74c2dbb880f0a8731e393975622c297fdeb339
|