Function Call - Arrays as Parameters

Perhaps this is obvious to some, but I was working with function call capability to get the latitude and longitude from a mailing address using the Nomanitim API.

Initially, I used the following to define my function which I named get_coordinates:

completion = openai.ChatCompletion.create(
   model="gpt-4-0613",
   messages=[{"role": "user", "content": my_prompt}],
   functions=[
   {
       "name": "get_coordinates",
       "description": "Get the latitude and longitude of a mailing address",
       "parameters": {
           "type": "object",
           "properties": {
               "address": {
                   "type": "string",
                   "description": "The mailing address to be located",
                },
               
           },
           "required": ["address"],
       },
   }
  ],
function_call="auto",
) code here

This worked well, for prompts where only a single address was part of the prompt. For example,

“Find the location of 123 Main Street, Anytown, Ontario Canada”, but if I used prompt, “Find the location for the following addresses: 123 Main Street, Anytown, Ontario Canada, 456 Second Street, Anytown, Ontario” , only the first address would be pulled as an argument, the second was ignored.

My solution was as follows:

completion = openai.ChatCompletion.create(
    model="gpt-4-0613",
    messages=[{"role": "user", "content": my_prompt}],
    functions=[
    {
    "name": "get_coordinates",
    "description": "Get the latitude and longitude of multiple mailing addresses",
    "parameters": {
        "type": "object",
        "properties": {
            "addresses": {
                "type": "array",
                "description": "The mailing addresses to be located",
                "items": {
                    "type": "string"
                }
            }
        },
        "required": ["addresses"]
    }
  }
   ],
function_call="auto",
) 

By changing the definition from a string to an array of strings, I could now put any number of addresses in my prompt and extract them to do my function calls.

14 Likes

Thanks for posting this. I was about to post about how to get multiple outputs or function calls. Very helpful!

1 Like

You need to set your function call into the system role to work properly and the request from the user in the user role as usual.
Function calling is fine-tuned for the system role. This acts now as the knowledgebase for such commands.
And I suggest to set the temperature to 0?
What is the output?

2 Likes

Useful information, thanks ! While the code works fine without the system role, I take your point. I will also do as you suggest with respect to setting the temperature to 0.
Given a prompt like:

"Can you provide the coordinates for Parliament Hill, Ottawa Canada and the White House in Washington, D.C."

type {
  "id": "chatcmpl-7SUo4zUR5DNVQ3KDJuujrCSu0CDHY",
  "object": "chat.completion",
  "created": 1687025960,
  "model": "gpt-4-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "get_coordinates",
          "arguments": "{\n  \"addresses\": [\"Parliament Hill, Ottawa Canada\", \"White House, Washington D.C.\"]\n}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
  "usage": {
    "prompt_tokens": 78,
    "completion_tokens": 28,
    "total_tokens": 106
  }
}

 Response: None

 Function Call: get_coordinates

 Addresses: ['Parliament Hill, Ottawa Canada', 'White House, Washington D.C.'] paste code here
1 Like

@PriNova what do you mean by set your function call into the system role? The “functions” and “function_call” parameters are outside the list of messages when passed to OpenAI, so I struggle to see how it could be into the system role.

It is plausible nonsense. Perhaps even unaware of the new-at-the-time function ability.

The function definition is injected into a system role behind the scenes, but you just put the function definition object as parameter for the API and the API takes care of the rest.

1 Like

I apologize for the bad wording. What I meant was, to set the instructions for using the function call API at the system role.