Function Calling
Introduction
OctoAI’s Chat Completions API supports function calling as introduced by OpenAI. You can describe the functions and arguments to make available to the LLM model. This is a powerful technique that turns LLMs models into “agents” that can take action within your application.
When the model makes a function call, it will output a JSON object containing arguments to call one or many of the tools that have been defined. After calling any invoked tools, you can provide those results back to the model in subsequent Chat Completions API calls.
tool_choice
parameter. For instance, in the examples below, you can substitute tool_choice=auto
with tool_choice={"type": "function", "function": {"name": "get_flight_status"}}
. In the example below, we’ll use the OpenAI SDK, but reset the base URL and model arguments to call OctoAI’s Chat Completions API.
Supported Models
meta-llama-3.1-8b-instruct
meta-llama-3.1-70b-instruct
meta-llama-3.1-405b-instruct
Function Calling Example: Flight Status in Python
In the python example below, we:
- Define the
get_flight_status
function. - Create a list of tools to specify the function available to the model.
- Define the messages for the conversation.
- Create a chat completion request with the model, messages, and tools.
- Process the tool calls returned by the model and get the function’s output.
- Append the function’s response to the messages and generate the final enriched response.
Output
And there you have it!
In this tutorial, we explored how to use OctoAI’s Chat Completions API to integrate a simple function calling exercise within a chatbot scenario. We demonstrated setting up the OpenAI client with a specific API endpoint, defining a function (get_flight_status
), and configuring the chatbot to handle and process function calls dynamically. By following the steps outlined, you can extend this approach to include various functions and enhance the capabilities of your chatbot applications using the scalable, low latency, low cost endpoints offered by OctoAI.