Build A Function Call
Learn how to build a Function Call to use with your Agent.
This guide walks you through how to build a simple function call for a demo application. Please refer to this code repository for the complete application code.
Getting Started
We’ll setup a module called functions.py
that defines a collection of functions and configurations which can be used in a Voice Agent API system for customer service automation. These functions will handle customer lookups, appointments, and order management through a set of async functions and standardized function definitions.
Please note: functions.py
will rely on the business_logic.py and the client.py files to work end to end.
Prerequisites
- Python 3.7+
- Familiarity with Python
- An understanding of how to use Python Virtual environments.
- Familiarity with the Deepgram Voice Agent API
Create the file
Create a file called functions.py
and at the top of the file:
Set Up the dependencies and Import the business logic
This guide doesn’t cover the development of the business logic for this application. Please see this code for more details on the business logic used.
Implement the Functions
We’ll start with the customer lookup function:
This function handles:
- Multiple lookup methods (phone, email, ID)
- Standardized ID formatting (CUSTXXXX)
- Phone number normalization (+1XXXXXXXXXX)
- Email address parsing
Next we’ll create a function to find customer appointments for a specific customer:
This function handles:
- retrieving all appointments for a specific customer using their customer ID.
Next we’ll create a function to do appointment management:
This function handles:
- Scheduling new appointments
- Checking availability
- Retrieving existing appointments
Next we’ll create a function to do order management:
This function handles:
- Order history lookup
- Order status checking
Next will create a function to find available appointment times:
This function handles:
- Finding available appointment slots
- Pre-scheduling availability checks
- Responding to “When can I come in?” queries
Next will setup a function to handle conversational flow:
This function handles:
- Agent filler messages for natural interaction
- Proper conversation closure handling
Finally we’ll create a function to end the call gracefully.
This function handles:
- Conversation closure
- Adding appropriate farewell messages
Create Function Definitions
Next well need to setup FUNCTION_DEFINITIONS
Which is an array that defines the API contract for the Voice Agent system. It specifies all available operations, their parameters, and usage guidelines.
Each function definition follows a JSON Schema format with:
- Name
- Description
- Parameters specification
- Required fields
- Enumerated values where applicable
Create a Function Map
Finally we’ll need to create aFUNCTION_MAP
which is a dictionary that maps function names to their corresponding implementation functions. It serves as a routing mechanism to connect the function definitions with their actual implementations.
Call the functions from client.py
This guide doesn’t cover the development of the client application but you can refer to the client code for how you can connect your Voice Agent tofunctions.py
as well as the documentation Configuring the Voice Agent.
What’s Next?
- For more ideas and function code samples for using Function Calling with your Agent check out this repository.