Function calling, passing a list of values

Hey there.

I’m trying to build the function arguments for a function that is meant to sum up all the values in a list. I’d like to do this using the new functions capability, but I can’t seem to find any examples quite like the one I’m trying to accomplish.

In a separate process, I’m extracting and summarizing information from some documents, and preparing it in a way that looks like this:

Document ID: doc4
Lender: Mountain Peak Financial Corp.
Borrower: Greenfield Developers Ltd.
Loan Amount: $25,000,000
Interest Rate: 5.0%
Loan Term: 12 years

Document ID: doc1
Lender: ABC BANK
Borrower: XYZ CORPORATION
Loan Amount: $10,000,000
Interest Rate: 3.5%
Loan Term: 10 years

(this is all mock data)

My analyzer class looks like this:

FUNCTIONS = [
{
“name”: “compute_total_debt”,
“description”: “Sums up the total debt from a list of numbers.”,
“parameters”: {
“type”: “object”,
“properties”: {
“debts”: {
“type”: “array”, #: Is this even possible???
“description”: “Should be an array of numbers. The function will read in the array and sum the numbers.”,
},
},
“required”: [“debts”],
},
},
]

class LLMAnalyzer():
def init(self):
with open(‘config.json’, ‘r’) as file:
self.config = json.load(file)
self.prompt = self.config[‘compute_total_debt’]
print(f"self.prompt: {self.prompt}")

def do_analysis(self, input_text):
    #: Via OpenAI ChatCompletion API
    messages = [
        {"role": "system", "content": self.prompt},
        {"role": "user", "content": input_text}
    ]
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages,
        temperature=0,
        functions=FUNCTIONS,
        function_call={f"name": "compute_total_debt"}
    )
    full_message = response["choices"][0]
    print(f"full_message: {full_message}")
    function_call = full_message
    return function_call

Am I allowed to do this? Seems like a no. When I tried to this:

    function_call = llm_analyzer.do_analysis(summarized_context)
    print(colored(f"function_call: {function_call}", "green"))

i get this error:

openai.error.InvalidRequestError: <exception str() failed>

Turns out, this was an easy fix. I wasn’t understanding what JSON Schema actually was.

I needed to change the type and add the items attributes.

FUNCTIONS = [
    {
        "name": "compute_total_debt",
        "description": "Sums up the total debt from a list of numbers.",
        "parameters": {
            "type": "object",
            "properties": {
                "debts": {
                    "type": "array",
                    "items": {
                        "type": "integer"
                    },
                    "minItems": 1,
                    "maxItems": 50,
                    "description": "Should be an array of numbers.  The function will read in the array and sum the numbers.",
                },
            },
            "required": ["debts"],
        },
    },
]

The AI won’t receive properties like minItems.

The function doesn’t have to be so specialized. Just give the AI a utility that it can use appropriately.

function_list=[
{
  "name": "sum_numbers",
  "description": "Add mixed int and float numbers together",
  "parameters": {
    "type": "object",
    "properties": {
      "numbers_list": {
        "type": "string",
        "description": "required format: python list of individual numbers to add"
      }
    },
    "required": ["numbers_list"]
  }
}
]