ActionAI: Call local functions using OpenAI function calling

Amal ShajiAmal Shaji
2 min read

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

Repo

Usage

Install the package,

pip install actionai

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

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.

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"})

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

Demo

Demo running the todo example

0
Subscribe to my newsletter

Read articles from Amal Shaji directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amal Shaji
Amal Shaji

Software Engineer. Exploring AI and dev tools. Python, Rust, Go, Typescript, Svelte