| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Welcome to Vapi, your versatile voice assistant SDK. This guide will walk you through the process of extending Vapi's functionality with custom function calling, allowing you to focus on your core business logic.
Custom function calling in Vapi can be leveraged in various scenarios to enhance the user experience and provide tailored responses. Here are some potential use cases where custom functions can be particularly beneficial:
Function calls are a better choice when:
By utilizing custom function calls, developers can create a more versatile and responsive voice assistant that caters to the specific needs of their users and business.
To get started with function calling in Vapi, you'll need to define your functions and register them with the Vapi Assistant. These functions can be used to perform actions or fetch data in response to user queries.
Create your functions in the functions directory. Each function can be a separate file exporting an async function that takes parameters and returns a response.
Example function:
export const getRandomName = async ({ gender, nat }) => {
// Your logic to fetch random Name
return { result: "John Doe" };
};Register your functions in the functions/index.ts file. This file acts as a central registry for all your custom functions.
Example registration:
import { getCharacterInspiration } from "./getCharacterInspiration";
import { getRandomName } from "./getRandomName";
export default {
getRandomName: getRandomName,
getCharacterInspiration: getCharacterInspiration,
// Add more functions here
};When Vapi receives a function call, it will invoke the corresponding function from your registry. The api/webhook/functionCall.ts file is responsible for handling these calls.
Example handler:
import defaultFunctions from "../../functions";
export const functionCallHandler = async (payload) => {
const { functionCall } = payload;
const { name, parameters } = functionCall;
if (defaultFunctions[name]) {
return await defaultFunctions[name](parameters);
} else {
throw new Error(`Function ${name} not found`);
}
};You need to create a Vapi Assistant which will contain the function information and when to invoke it as part of the systemPrompt in the model configuration. Sample Assistant configuration is here.
{
"firstMessage": "Hello, how can I help you today?",
"model": {
"provider": "openai",
"model": "gpt-3.5-turbo",
"systemPrompt": "You are a versatile assistant...",
"functions": [
{
"name": "getRandomName",
"description": "Generates a random name based on optional gender and nationality",
"parameters": {
"type": "object",
"properties": {
"gender": { "type": "string", "enum": ["male", "female"],
"description": "The gender for which to generate a name." },
"nat": { "type": "string",
"description": "The nationality based on which to generate a name. Example: IN for India, US for United States of America or USA and so on." }
}
}
},
{
"name": "getCharacterInspiration",
"description": "Provides character inspiration based on a given query provided by the author.",
"parameters": {
"type": "object",
"properties": {
"inspiration": { "type": "string" }
}
}
}
// Add more functions here
]
}
}
With function calling, you can easily extend the capabilities of Vapi to suit your specific business needs. By following the steps outlined above, you can integrate custom logic into your voice assistant and provide a richer experience for your users.
For additional help, refer to the official Vapi documentation
| Back | FazBrowse Home | New Git URL |