2023-11-22 17:13:50 +00:00
+++
disableToc = false
2026-04-03 07:46:06 +00:00
title = "OpenAI Functions and Tools"
2024-01-18 18:41:08 +00:00
weight = 17
2024-01-19 18:23:58 +00:00
url = "/features/openai-functions/"
2023-11-22 17:13:50 +00:00
+++
2024-02-27 22:24:46 +00:00
LocalAI supports running OpenAI [functions and tools API ](https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools ) with `llama.cpp` compatible models.
2023-11-22 17:13:50 +00:00

2024-02-27 22:24:46 +00:00
To learn more about OpenAI functions, see also the [OpenAI API blog post ](https://openai.com/blog/function-calling-and-other-api-updates ).
LocalAI is also supporting [JSON mode ](https://platform.openai.com/docs/guides/text-generation/json-mode ) out of the box with llama.cpp-compatible models.
2023-11-22 17:13:50 +00:00
💡 Check out also [LocalAGI ](https://github.com/mudler/LocalAGI ) for an example on how to use LocalAI functions.
## Setup
OpenAI functions are available only with `ggml` or `gguf` models compatible with `llama.cpp` .
You don't need to do anything specific - just use `ggml` or `gguf` models.
## Usage example
You can configure a model manually with a YAML config file in the models directory, for example:
```yaml
name: gpt-3.5-turbo
parameters:
# Model file name
model: ggml-openllama.bin
top_p: 80
top_k: 0.9
temperature: 0.1
```
To use the functions with the OpenAI client in python:
```python
2024-08-21 11:09:26 +00:00
from openai import OpenAI
messages = [{"role": "user", "content": "What is the weather like in Beijing now?"}]
tools = [
2023-11-22 17:13:50 +00:00
{
2024-08-21 11:09:26 +00:00
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Return the temperature of the specified region specified by the user",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "User specified region",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "temperature unit"
},
2023-11-22 17:13:50 +00:00
},
2024-08-21 11:09:26 +00:00
"required": ["location"],
2023-11-22 17:13:50 +00:00
},
},
}
]
2024-08-21 11:09:26 +00:00
client = OpenAI(
# This is the default and can be omitted
api_key="test",
base_url="http://localhost:8080/v1/"
)
response =client.chat.completions.create(
2023-11-22 17:13:50 +00:00
messages=messages,
2024-08-21 11:09:26 +00:00
tools=tools,
tool_choice ="auto",
model="gpt-4",
2023-11-22 17:13:50 +00:00
)
2024-08-21 11:09:26 +00:00
#...
2023-11-22 17:13:50 +00:00
```
2024-08-21 11:09:26 +00:00
For example, with curl:
```bash
curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "What is the weather like in Beijing now?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Return the temperature of the specified region specified by the user",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "User specified region"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "temperature unit"
}
},
"required": ["location"]
}
}
}
],
"tool_choice":"auto"
}'
```
2023-11-22 17:13:50 +00:00
2024-08-21 11:09:26 +00:00
Return data:
```json
{
"created": 1724210813,
"object": "chat.completion",
"id": "16b57014-477c-4e6b-8d25-aad028a5625e",
"model": "gpt-4",
"choices": [
{
"index": 0,
"finish_reason": "tool_calls",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"index": 0,
"id": "16b57014-477c-4e6b-8d25-aad028a5625e",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\"location\":\"Beijing\",\"unit\":\"celsius\"}"
}
}
]
}
}
],
"usage": {
"prompt_tokens": 221,
"completion_tokens": 26,
"total_tokens": 247
}
}
```
2023-11-22 17:13:50 +00:00
## Advanced
2024-05-10 15:03:56 +00:00
### Use functions without grammars
The functions calls maps automatically to grammars which are currently supported only by llama.cpp, however, it is possible to turn off the use of grammars, and extract tool arguments from the LLM responses, by specifying in the YAML file `no_grammar` and a regex to map the response from the LLM:
```yaml
2024-05-10 15:09:51 +00:00
name: model_name
parameters:
# Model file name
model: model/name
2024-05-10 15:03:56 +00:00
function:
2024-05-10 15:09:51 +00:00
# set to true to not use grammars
no_grammar: true
2024-05-31 20:52:02 +00:00
# set one or more regexes used to extract the function tool arguments from the LLM response
response_regex:
- "(?P< function > \w+)\s*\((?P< arguments > .*)\)"
2024-05-10 15:03:56 +00:00
```
The response regex have to be a regex with named parameters to allow to scan the function name and the arguments. For instance, consider:
```
(?P< function > \w+)\s*\((?P< arguments > .*)\)
```
will catch
```
function_name({ "foo": "bar"})
```
2024-02-28 14:58:31 +00:00
### Parallel tools calls
This feature is experimental and has to be configured in the YAML of the model by enabling `function.parallel_calls` :
```yaml
name: gpt-3.5-turbo
parameters:
# Model file name
model: ggml-openllama.bin
top_p: 80
top_k: 0.9
temperature: 0.1
function:
# set to true to allow the model to call multiple functions in parallel
parallel_calls: true
```
### Use functions with grammar
2023-11-22 17:13:50 +00:00
It is possible to also specify the full function signature (for debugging, or to use with other clients).
The chat endpoint accepts the `grammar_json_functions` additional parameter which takes a JSON schema object.
For example, with curl:
```bash
curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "How are you?"}],
"temperature": 0.1,
"grammar_json_functions": {
"oneOf": [
{
"type": "object",
"properties": {
"function": {"const": "create_event"},
"arguments": {
"type": "object",
"properties": {
"title": {"type": "string"},
"date": {"type": "string"},
"time": {"type": "string"}
}
}
}
},
{
"type": "object",
"properties": {
"function": {"const": "search"},
"arguments": {
"type": "object",
"properties": {
"query": {"type": "string"}
}
}
}
}
]
}
}'
```
2024-04-10 14:30:57 +00:00
Grammars and function tools can be used as well in conjunction with vision APIs:
```bash
curl http://localhost:8080/v1/chat/completions -H "Content-Type: application/json" -d '{
"model": "llava", "grammar": "root ::= (\"yes\" | \"no\")",
"messages": [{"role": "user", "content": [{"type":"text", "text": "Is there some grass in the image?"}, {"type": "image_url", "image_url": {"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" }}], "temperature": 0.9}]}'
```
2023-11-22 17:13:50 +00:00
## 💡 Examples
2025-11-19 21:21:20 +00:00
A full e2e example with `docker-compose` is available [here ](https://github.com/mudler/LocalAI-examples/tree/main/functions ).