DEV Community

Cover image for ActionAI: Call local functions using OpenAI function calling
Amal Shaji
Amal Shaji

Posted on • Originally published at exp.amal.sh

ActionAI: Call local functions using OpenAI function calling

ActionAI is a small wrapper around Openai's function calling. It works by auto-generating json schemas for your Python functions.

Repo

GitHub logo amalshaji / actionai

A small library to call local functions using openai function calling

ActionAI

A small library to run local functions using the openai function calling

Warning
This library is still in its early stages, so please use it cautiously. If you find any bugs, please create a new issue.

Install

pip install actionai
Enter fullscreen mode Exit fullscreen mode

Usage

Note
A function must be fully typed and must have a docstring

# define a new function
def get_current_weather(location: str, unit: str = "fahrenheit")
    """Function to get current weather for the given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return weather_info


import actionai

action = actionai.ActionAI()
action.register(get_current_weather)

response = action.prompt("What is the current weather in the north pole?")

print(response["choices"][0]["message"]["content"])
# The
Enter fullscreen mode Exit fullscreen mode

Usage

Install the package,

pip install actionai
Enter fullscreen mode Exit fullscreen mode

Now define a new function,

def get_current_weather(location: str, unit: str = "fahrenheit"):
    """Function to get current weather for the given location"""
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return weather_info
Enter fullscreen mode Exit fullscreen mode

Function parameters must be typed and must have a docstring. The docstring is used as the function description for openai, and typed parameters are used for autogenerating json schema(using pydantic)

Now, register your functions and start asking questions

import actionai

action = actionai.ActionAI()
action.register(get_current_weather)

response = action.prompt("What is the current weather in the north pole?")

print(response["choices"][0]["message"]["content"])
# The current weather at the North Pole is 72°F. It is sunny and windy.
Enter fullscreen mode Exit fullscreen mode

The chat completion API will decide if a function call must be made for the query and send it in response. ActionAI calls the function and returns the response to the model, which completes the conversation.

Adding context

Sometimes, you don't want to populate certain parameters yourself(via the program). You can make use of context here.

def list_todos(user: str):
    """Function to list all todos"""
    return todos[user]

action = actionai.ActionAI(context={"user": "jason"})
Enter fullscreen mode Exit fullscreen mode

The context variables are skipped when creating json schema, and the values are inserted when calling the function.

Demo

Demo running the todo example

Demo

Top comments (0)