Function Calling parameter types

What are the types that can be passed as parameters? I can only seem to set the type as a string.
can you set more complex types? i.e Array, two dimensional Array, objects, etc

I’m sure I’m doing something wrong. If I set the type to array I get 400 Bad request

properties: {
                       key: {
                           type: "string",
                           description: "example description"
                       },
}
4 Likes

The other day I do remember seeing the JSON schema for OpenAI functions. I think I even bookmarked the web page because it was three degrees of separation from the blog page.

Sadly I cannot find the page nor the link.

However ChatGPT does seem to possess some knowledge of the JSON schema so prompted ChatGPT.


Prompt
ChatGPT May 24 Version

What is the JSON schema for OpenAI function properties for a two dimensional array.

Completion

As of my knowledge cutoff in September 2021, OpenAI does not provide a specific JSON schema for function properties that includes a two-dimensional array. However, you can define the structure of a two-dimensional array within the function properties using a nested array structure. Here’s an example of how you might structure the properties for an OpenAI function with a two-dimensional array using JSON:

json

{
  "name": "MyFunction",
  "description": "This is a sample function",
  "parameters": [
    {
      "name": "array",
      "type": "array",
      "description": "Two-dimensional array",
      "items": {
        "type": "array",
        "items": {
          "type": "number"
        }
      }
    }
  ],
  "output": {
    "type": "array",
    "items": {
      "type": "array",
      "items": {
        "type": "number"
      }
    }
  }
}

In this example, the function has a parameter named “array” which is an array of arrays. The inner arrays represent rows in the two-dimensional array, and each element within the inner arrays represents a value in the two-dimensional array (in this case, numbers). The output of the function is also a two-dimensional array with the same structure.

Please note that the actual schema for OpenAI function properties may differ depending on the specific implementation and requirements of the OpenAI API. It’s always a good idea to refer to the official OpenAI documentation or resources for the most up-to-date and accurate information on function properties.


I have not tried this.



EDIT

IIRC this is what I found the other day, may or may not be correct.

This OpenAI API reference page

https://platform.openai.com/docs/api-reference/chat/create

Section: Create chat completion

notes

parameters object Optional

The parameters the functions accepts, described as a JSON Schema object. See the guide for examples, and the JSON Schema reference for documentation about the format.

Here is the noted JSON Schema

HTH

6 Likes

nice ok I will try this and see what happens :ok_hand:

You can have a look into this post and the reference @EricGT provided.

1 Like

I found using pydantic made things easier for me and worked well with str, int, lists and dictionaries.

Here is an example where I extract data from a set of user interviews.

...
from pydantic import BaseModel, Field
from typing import Optional, List

# define function structure in pydantic

class Topic(BaseModel):
    name: str = Field(..., description="Classification category")
    count: int = Field(..., description="Count for the category")

class ExtractInsightsParams(BaseModel):
    interviewee_names: List[str] = Field(..., description="The names of the interviewees, e.g. Christoph, person y")
    interviewee_count: int = Field(..., description="Number of people interviewed")
    interview_topics: List[Topic] = Field(..., description="List of top 10 classification categories with their counts")

Then I use it in my function:

def get_completion_from_messages(messages, 
                                 model="gpt-3.5-turbo-16k-0613", 
                                 temperature=0):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
        max_tokens=1000,
        functions=[
        {
          "name": "extract_insights",
          "description": "Extract insights from interviews",
          "parameters": ExtractInsightsParams.schema()
        }
    ],
        function_call="auto",
    )
    print(response)
    function_call = response.choices[0].message["function_call"]
    arguments = json.loads(function_call["arguments"])
    return arguments
6 Likes

Really clever way. Sometimes we cannot see the forest because of all the trees.

1 Like

Is this with Pydantic 2.0? It didn’t work with 1.10.x
The schema() produces “$ref” references which refer to a definitions field in the schema and I think the API doesn’t understand that.

However, in general, one must write their own function, with JSON data types.

string
number
boolean
null/empty
object
array

Lets look at a demonstration of them all,

  • how you would specify them as a “functions” API parameter;
  • how the specification is rewritten into AI language by the endpoint;
  • how the AI calls the function as it is specified.

Function specification sent to API

functions = [
{
    "name": "data_demonstration",
    "description": "This is the main function description",
    "parameters": {
        "type": "object",
        "properties": {
"string_1": {"type": "string", "description": ""},
"number_2": {"type": "number", "description": ""},
"boolean_3": {"type": "boolean", "description": ""},
"empty_4": {"type": "null","description": "This is a description of the empty_4 null property"},
"string_5_enum": {"type": "string", "enum": ["Happy", "Sad"]},
        },
        "required": ["string_1","number_2","boolean_3", "empty_4", "string_5_enum"]
    }
}
]

Function specification received by AI:

namespace functions {

// This is the main function description
type data_demonstration = (_: {
string_1: string,
number_2: number,
boolean_3: boolean,
// This is a description of the empty_4 null property
empty_4: any,
string_5_enum: "Happy" | "Sad",
}) => any;

} // namespace functions

Function used by AI:

{
  ..
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "data_demonstration",
          "arguments": "{
  "string_1": "Hello",
  "number_2": 42,
  "boolean_3": true,
  "empty_4": null,
  "string_5_enum": "Happy"
}"
        }
      },
      "finish_reason": "function_call"
    }
  ],...

(\n is rendered above for clarity)


Function specification with an inner object

functions = [
{
    "name": "data_demonstration",
    "description": "This is the main function description",
    "parameters": {
        "type": "object",
        "properties": {
            "object_1": {
                "type": "object",
                "description": "The object data type as a property",
                "properties": {
                "string_2a": {
                    "type": "string",
                    "enum": ["Happy", "Sad"]
                    },
                "string_2b": {
                    "type": "string",
                    "description": "Description in a second object is lost"
                    },
                },
            },
            "string_1": {"type": "string",
                         "description": "Not required gets a question mark"},
        },
        "required": ["object_1","placeholder_2"]
    }
}
]

Function specification received by AI, with object:

namespace functions {

// This is the main function description
type data_demonstration = (_: {
// The object data type as a property
object_1: {
  string_2a: "Happy" | "Sad",
  string_2b: string,
},
// Not required gets a question mark
string_1?: string,
}) => any;

} // namespace functions

Above we see a big failure that people don’t realize, nesting objects gets you no descriptions for the inner properties. They don’t have the “not required” question mark.

Function used by AI and output by API:

{...
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "data_demonstration",
          "arguments": "{
  "object_1": {
    "string_2a": "Happy",
    "string_2b": "Hello World"
  },
  "string_1": "Example data"
}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
...

I hope you take the time to follow each of these properties through their specification to the ultimate API output.

There is one more JSON data type, array, which is equivalent to a Python list. Left as an exercise for you.

(Not explored, the method of setting the API function_call parameter from auto to “required” or “none”.)

7 Likes

Thanks for adding to the thread. I was not aware of this. Very helpful.

3 Likes