Define a default value in one of the parameter in function calling

Hi, I have this tools (function calling)

 const tools = [
      {
        "type": "function",
        "function": {
          "name": "extractShoppingDetail",
          "description": "Extract shopping detail from the user prompt",
          "parameters": {
            "type": "object",
            "properties": {
              "item": {
                "type": "string",
                "description": "The item name"
              },
              "amount": {
                "type": "number",
                "description": "The amount of the item. Default to 1"
              },
              "brand": {
                "type": "string",
                "description": "The brand of the item. Return null if not specified",
              },
              "price": {
                "type": "number",
                "description": "The price of the item. Return null if not specified"
              }
            },
            "required": ["query"]
          }
        }
      }
    ]

If my user’s prompt like this

const userPrompt = `I need a running tshirt for my son for this holiday, and my budget is $50`

The returned value only

{
  "item": "running t-shirt",
  "price": 50
}

Which is missing the brand and amount of information.
How can I give a default value in this case?

Based on the information provided in the extracts, there isn’t a direct way to set default values for missing parameters in the function calling feature of the OpenAI API. The function calling feature is designed to extract and process information based on the user’s input and the defined parameters in the function schema.

However, you can handle this in your application code. After receiving the response from the API, you can check if the brand and amount fields are null or missing, and if so, assign them default values. Here’s a simple example in JavaScript:

const response = /* the response from the API call */;

// Check if the fields are null or missing and assign default values
response.brand = response.brand || "default brand";
response.amount = response.amount || 1;

In this example, if brand is null or missing, it will be set to "default brand" , and if amount is null or missing, it will be set to 1 . Please note that this is a workaround and the actual implementation might vary based on your application’s requirements and the programming language you are using.

Thanks Paul! that’s very helpful! Will mark it as a solution.

@tukemon

Is it OK if a moderator closes this topic?

1 Like