Published on

Getting JSON Response from OpenAI API with Function Calling

Authors

OpenAI has released Function calling today using which we can directly get JSON object as output without us having to ask the model to explicitly do it in the prompt.

This helps to easily consume OpenAI API with confidence that we'll get the expected result only and integrate with other APIs efficiently.

How does it work?

The feature's name "function calling" might be a little misleading, as no actual function is called. Instead, we get the response in JSON format that matches the function's schema.

When making the API request, along with the prompt we need to define function with its schema and it is currently supported only in gpt-4-0613 and gpt-3.5-turbo-0613. Then the model will give us response in JSON

Quick Example - Transaction Parser

Here is a snippet and prompt on how to do it.

For the sake of simplicity, I'm using fetch here. You can also use their official SDK as well.

import fetch from "node-fetch";

const OPENAI_API_KEY = process.env.OPENAI_API_KEY;

const transactionParser = async (transactionAlert) => {
  const payload = {
    model: "gpt-3.5-turbo-0613",
    messages: [
      {
        role: "user",
        content: transactionAlert
      },
    ],
    functions: [
      {
        name: "parse_transaction",
        description:
          "Extract the financial transactional details from the input",
        parameters: {
          type: "object",
          properties: {
            date: {
              type: "string",
              description: "Date of the transaction in YYYY-MM-DD",
            },
            description: {
              type: "string",
              description:
                "Return description of the transaction, if not use merchant name",
            },
            type: {
              type: "string",
              enum: ["credit", "debit"],
            },
            amount: {
              type: "number",
              description: "Amount of the transaction",
            },
            accountNumber: {
              type: "string",
              description: "Account number",
            },
          },
          required: ["date", "description", "type", "amount"],
        },
      },
    ],
  };

  const response = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${OPENAI_API_KEY}`,
    },
    body: JSON.stringify(payload),
  });
  const { choices } = await response.json();
  // JSON response from function calling
  return choices[0].message.function_call.arguments;
};


const txnAlert = "Your ICICI Bank Credit Card XX3210 has been used for a transaction of INR 127.00 on Mar 23, 2023 at 12:40:41. Info: LINGAM VEGETABLES. The Available Credit Limit on your card is INR 1,50,321.18"

transactionParser(txnAlert)

/*
{
  "date": "2023-03-23",
  "description": "LINGAM VEGETABLES",
  "type": "debit",
  "amount": 127.00
}
*/

For reference, here is the entire response

{
    "id": "chatcmpl-....",
    "object": "chat.completion",
    "created": 1686766748,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "parse_transaction",
                    "arguments": "{\n  \"date\": \"2023-03-23\",\n  \"description\": \"LINGAM VEGETABLES\",\n  \"type\": \"debit\",\n  \"amount\": 127.00\n}"
                }
            },
            "finish_reason": "function_call"
        }
    ],
    "usage": {
        "prompt_tokens": 170,
        "completion_tokens": 48,
        "total_tokens": 218
    }
}

References

Happy JSON response!