#How can I make AI assistant call a function with a parameter inside?
1 messages · Page 1 of 1 (latest)
Hi @fleet quest. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To make an AI assistant call a function with a parameter, you need to define the function schema and feed it into the assistant. Here are the steps:
- Define the function schema: This includes the function name and the parameters it accepts. The parameters are described as a JSON Schema object. For example, if you have a function
get_data_moviethat requires a movie title and the year of release, your function schema might look like this:
{
"name" : "get_data_movie",
"description": "retrieve information from a movie",
"parameters" : {
"type" : "object",
"properties": {
"title": {
"type": "string",
"description": "The name of the movie"
},
"year" : {
"type" : "string",
"description": "year of movie release",
"format": "year"
}
},
"required" : ["title", "year"]
}
}
(Source: Forum Post: Have several mandatory parameters in its function (https://community.openai.com/t/601304))
-
Feed the function schema into the assistant: When you make an API call, you can describe the function in the
functionsparameter. The assistant will then be able to call this function when needed. -
Handle the function call: When the assistant needs to perform an action, it will call the function from the pool it was given and supply input into it as defined by the function's parameters. The state of the run becomes
requires_actionwhen this happens. You then need to get the name of the function that the assistant has called, grab the data that it inputted, and in your program, call the real function of the same name and pass in the data that the assistant gave you.
- Return the function output: If your real function has output that the assistant is going to need, you can submit it back to the assistant when you have it. If there is no output from your real function, you can always just submit an affirmative like a string that says “success”.
(Source: Forum Post: Assistants functions workflow (https://community.openai.com/t/591372))
Remember, the assistant does not actually call any real functions or make requests to endpoints. It generates JSON that you can use to call the function in your code. Also, it's recommended to build in user confirmation flows before taking actions that impact the world on behalf of users.