Functions, Enums, Descriptions

Hello
I have the following function spec.

let spec = {
  name: "getPopulation",
  description:
    "Find the population of a given state. The state is represented by an ID named stateId. The user will give a state name and the corresponding stateId needs to be obtained",
  parameters: {
    type: "object",
    properties: {
      stateId: {
        type: "integer",
        description: "The ID of the state",
        enum: [
          { value: 1, description: "Alabama" },
          { value: 2, description: "Alaska" },
          { value: 3, description: "Arizona" },
          { value: 4, description: "Arkansas" },
          { value: 5, description: "California" },

        ],
      },
    },
    required: ["stateId"],
  },
};
export default spec;

I am trying to get the stateId based on the users query

What is the population of Alabama?

I was hoping it would give the function call response like below

   "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "getPopulation",
                    "arguments": "{\n  \"stateId\": 1\n}"
                }
            },
            "finish_reason": "function_call"
        }
    ],

But it always gives me some random number.
Any suggestions?

1 Like

I tried your function and it worked as you expected it.
In my testing, I did not include any system prompt.

Apologies to hijack your question for a side topic, but I’m curious about this syntax:

enum: [
          { value: 1, description: "Alabama" },
          { value: 2, description: "Alaska" }
          ...
]

I’ve never seen this documented. Every other example I’ve seen specifies enums as strings without a description, such as enum: [ "Alabama", "Alaska" ]. Is this something you’ve seen officially documented, or was it just a guess? It’s never clear with LLMs how much of the API is fixed and how much is just a suggestion to be interpreted by the LLM itself!

According to JSON schema’s own documentation:

6.1.2 enum

The value of this keyword MUST be an array. This array SHOULD have at least one element. Elements in the array SHOULD be unique.

...

Elements in the array might be of any type, including null.

JSON Schema Validation: A Vocabulary for Structural Validation of JSON (json-schema.org)

I’m a bit new to ChatGPT functions and I’m curious – would the difference in styling between the following create any difference in performance as far as ChatGPT’s selection of the parameter? I see from the function calling documentation that it can hallucinate answers as well. Any thoughts on this would be appreciated.

First Stated Format

enum: [
          { value: 1, description: "Alabama" },
          { value: 2, description: "Alaska" }
          ...
]

Second Stated Format

enum: [ "Alabama", "Alaska" ]

@joseph.valencia see my medium post on how to prevent hallucinations in function calls:

You need to add a schema validator and feedback loop.

2 Likes

Why don’t you jusut describe the value for each state in the description. I have found that this works very well for small lists.