I tested your functions and modify them a bit then wrote a simple system prompt to tell the AI what to do and it works as you expect it, calling the order summary after add to cart or remove from cart.
Here are the modified functions:
{
"name": "add_to_cart",
"description": "Add products to cart.",
"parameters": {
"type": "object",
"properties": {
"products": {
"description": "An array of products to be added to the cart. Each product is defined by its ID, quantity, optional or required modifiers, and optional special instructions.",
"type": "array",
"items": {
"type": "object",
"properties": {
"productId": {
"type": "string",
"description": "The unique identifier of the product to be added to the cart. This must correspond to a valid product in the store's inventory."
},
"quantity": {
"type": "number",
"description": "The number of units of the product to be added to the cart. This must be a positive integer."
}
},
"required": ["productId", "quantity"]
}
}
},
"required": ["products"]
}
},
{
"name": "remove_from_cart",
"description": "Remove products from cart given their productId.",
"parameters": {
"type": "object",
"properties": {
"products": {
"description": "An array of productId to be removed from the cart.",
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["products"]
}
},
{
"name": "get_order_summary",
"description": "Gets the order summary.",
"parameters": {
"type": "object",
"properties": {
"language": {
"type": "string",
"description": "The language used in given text in ISO 639 format."
}
},
"required": ["language"]
}
}
Here is a simple system prompt:
let system_prompt = `You are a helpful assistant.\n` +
`# Available Tools\n` +
`You have the following available tools that you can use depending on user query.\n` +
`- add_to_cart, when the user wants to add products in the cart.\n` +
`- remove_from_cart, when the user wants to remove products from the cart.\n` +
`- get_order_summary, when the user wants to see the cart.\n` +
`After calling either add_to_cart or remove_from_cart, call get_order_summary to show the user the current orders.`
Here are sample output:
- Add to Cart
Current shopping cart:
{
shopping_cart: [
{ productId: 'nsp3012-05', quantity: 2 },
{ productId: 'wae5001-02', quantity: 1 }
]
}
user query:
{ query: 'please add three usb0001-01 in the cart.' }
output:
// loop 0
{
id: 'call_AgCuHeAGBgzZy1Jx3U8Nr4dV',
type: 'function',
function: {
name: 'add_to_cart',
arguments: '{"products":[{"productId":"usb0001-01","quantity":3}]}'
}
}
// mock tool output
[
{
tool_call_id: 'call_AgCuHeAGBgzZy1Jx3U8Nr4dV',
role: 'tool',
name: 'add_to_cart',
content: '{\n "status": "success",\n "message": "1 items added to cart."\n}'
}
]
// 2nd Chat Completions API call
{
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_AgCuHeAGBgzZy1Jx3U8Nr4dV',
type: 'function',
function: [Object]
}
]
}
//loop 1
{
id: 'call_AgCuHeAGBgzZy1Jx3U8Nr4dV',
type: 'function',
function: { name: 'get_order_summary', arguments: '{"language":"en"}' }
}
// mock tool output
[
{
tool_call_id: 'call_AgCuHeAGBgzZy1Jx3U8Nr4dV',
role: 'tool',
name: 'get_order_summary',
content: '{\n' +
' "status": "success",\n' +
' "message": "This is the current order summary:\\nproductId, quantity\\nnsp3012-05, 2\\nwae5001-02, 1\\nusb0001-01, 3"\n' +
'}'
}
]
// 3rd Chat Completions API call
{
role: 'assistant',
content: 'Three USB0001-01 have been added to the cart. Here is the current order summary:\n' +
'- NSP3012-05, 2\n' +
'- WAE5001-02, 1\n' +
'- USB0001-01, 3'
}
- Remove from cart
Current shopping cart:
{
shopping_cart: [
{ productId: 'nsp3012-05', quantity: 2 },
{ productId: 'wae5001-02', quantity: 1 }
]
}
user query:
{ query: 'please remove wae5001-02 from the cart.' }
Output:
// loop 0
{
id: 'call_8NuUInOzLUVK2yo1Yzj5whN0',
type: 'function',
function: {
name: 'remove_from_cart',
arguments: '{"products":["wae5001-02"]}'
}
}
// mock tool output
[
{
tool_call_id: 'call_8NuUInOzLUVK2yo1Yzj5whN0',
role: 'tool',
name: 'remove_from_cart',
content: '{\n "status": "success",\n "message": "1 items removed from cart."\n}'
}
]
// 2nd chat completions api call
{
role: 'assistant',
content: null,
tool_calls: [
{
id: 'call_VdGFh9yOhm5GwqR9BDEn13Cn',
type: 'function',
function: [Object]
}
]
}
// loop 1
{
id: 'call_VdGFh9yOhm5GwqR9BDEn13Cn',
type: 'function',
function: { name: 'get_order_summary', arguments: '{"language":"en"}' }
}
// mock tool output
[
{
tool_call_id: 'call_VdGFh9yOhm5GwqR9BDEn13Cn',
role: 'tool',
name: 'get_order_summary',
content: '{\n' +
' "status": "success",\n' +
' "message": "This is the current order summary:\\nproductId, quantity\\nnsp3012-05, 2"\n' +
'}'
}
]
// 3rd chat completions api call
{
role: 'assistant',
content: 'The product "wae5001-02" has been successfully removed from the cart. The current order summary is as follows:\n' +
'- Product ID: nsp3012-05, Quantity: 2'
}
I think the system prompt is the important part that made it possible to execute the operation as you expected it.