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.

10 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