Prerequisites
You will need:- a Cerebras API Key
- a Cartesia account and API key
.env
file or to the API keys section on the Cartesia Agents Platform.
Find the complete code repository for this tutorial here.
pyproject.toml
file with the following dependencies:
Implementation Overview
In this tutorial you will:- Build the agent: Define a subclass of the Cartesia
ReasoningNode
that will make API calls through Cerebras - Customize messages and tools: Define customized tool schemas to adjust the conversation context and tools for compatibility with the Cerebras API
- Handle the calls: Build an async function to handle the calls.
Build the Agent
First we need to build the voice agent, which is a subclass of Cartesia’sReasoningNode
with a customized process_context
method. This method adjusts the formats of tools and messages to be compatible with Cerebras API.
For simplicity, we’ll use the EndCallTool
in the Cartesia Line SDK, which is called when the user implies that they are ready to end the call.
if...else
statement evaluates to see if any tools were called.
If EndCallTool
was called, the agent says goodbye to the user and ends the call. Note how we send the ToolResult
event to track which tools were called along with their corresponding arguments.
Customize Messages and Tools
Next we need to build a method to transform Cartesia’s messages into a Cerebras-compatible format. The script above uses a method calledconvert_messages_to_cs
that examines each message type (user messages, agent responses, and tool calls), then assigns the appropriate role and structures the content into dictionaries with role
and content
fields.
Handle the Calls
Now we need to define the asynchandle_new_call
function. This defines our conversation node, the corresponding “bridge” to connect different events together, and handles interruptions by user.
This function also starts the session and waits for the user to speak.
Putting It All Together
Now that all the three major components are ready, we can define our app and run it in themain.py
file like this:
You will need the
toml
file that includes the project details. Make sure to modify this accordingly.https://play.cartesia.ai/agents
and connect your code through the Connect Your Code option. You will be able to see if the project deploys successfully or returns error.
When your agent is marked as ready, press call! Then you can experiment with different models and system prompts.
If you are interested in implementing more complicated voice agents, including background agents, check out our Interview Practice Agent!