#Function calling

1 messages · Page 1 of 1 (latest)

open nimbus
#

Can you tell me how the function calling from OpenAI works?

sick lagoonBOT
#

Hi @open nimbus. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Function calling in OpenAI is a powerful feature that allows AI models to generate function arguments based on provided specifications. It's important to note that the API does not execute the function; instead, it generates the JSON that you can use to call the function in your own code.

Here's a basic overview of how function calling works:

  1. Function Definitions: You need to define the functions that the model can call. This can be done by converting OpenAPI specifications into function specifications. OpenAPI is a universally accepted standard for describing the details of RESTful APIs in a format that machines can read and interpret. This can be leveraged to show GPT how to call APIs (source (https://github.com/openai/openai-cookbook/blob/main/examples/Function_calling_with_an_OpenAPI_spec.ipynb)).

  2. Calling Functions: Once the functions are defined, you can use the chat completions API to intelligently invoke these functions based on user instructions. The model can pick between generating a message or calling a function. The function calls are generated based on the user's instructions and the function definitions provided (source (https://github.com/openai/openai-cookbook/blob/main/examples/Function_calling_with_an_OpenAPI_spec.ipynb)).

  3. Handling Function Calls: The function calls generated by the model can be used in your own code to execute the function. The model does not execute the function itself. It's up to developers to execute function calls using model outputs (source (https://github.com/openai/openai-cookbook/blob/main/examples/Fine_tuning_for_function_calling.ipynb)).

#
  1. Parallel Function Calling: Newer models like gpt-4-1106-preview or gpt-3.5-turbo-1106 can call multiple functions in one turn (source (https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb)).

  2. Fine-tuning: If function calling is not accurate or reliable, you can fine-tune the model to increase function calling accuracy. This involves improving the function definitions and experimenting with prompt engineering. If these steps fail to improve function calling to a satisfactory level, then you can try fine tuning for function calling (source (https://github.com/openai/openai-cookbook/blob/main/examples/Fine_tuning_for_function_calling.ipynb)).

  3. Tokens: Functions are injected into the system message in a syntax the model has been trained on. This means functions count against the model's context limit and are billed as input tokens. If running into context limits, we suggest limiting the number of functions or the length of documentation you provide for function parameters (source (https://platform.openai.com/docs/guides/function-calling)).

For more detailed information and examples, you can refer to the OpenAI Cookbook (https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb) and the OpenAI Platform Documentation (https://platform.openai.com/docs/guides/function-calling).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: