Skip to main content

Python AI Functions

Simple library that can convert from python functions to a JSON schema description of those functions, suitable for use with AI libraries.

Installation

pip install ai-functions

Usage

For example:

from ai_functions import get_openai_functions, execute_function


def search_web(query: Annotated[str, "google formatted keywords to search for"]):
    """Search the web"""

print(openai_function_dict([search_web]))

Also, if you get a response.function_call from openai, you can use execute_function

openai_function_execute([search_web], function_call)

Or, if you have the name and arguments split out already:

function_execute([search_web], name, arguments)

Finally, if you want a container to handle this:

from ai_functions import AIFunctions


container = AIFunctions([search_web, add_calendar_entry])

functions = container.openai_dict()
subset_functions = container.openai_dict(["search_web"])
container.execute("search_web", {"query":"top web hosting sites"})
container.opeanai_execute({"name": "search_web", "arguments": "{\"query\":\"top web hosting sites\"}")

What stuff does this handle?

  • Converts your annotated schema into an appropriate prompt
  • Handles converting arguments to JSON if they are specified as a string.
  • Auto-casts arguments to the right types, if they aren't right.
  • Raises errors that AI engines understand if returned as a function response, instead of errors with poor descriptions.

Async execute

  • If a loop is provided to the AIFunctions constructor, or to any execute calls, it will be used to schedule a coroutine.
  • Async versions of execute are available, prefix all calls with async_

Some fine print

If you want to have a paramter that is still seen as "valid", but isn't part of the schema, you can annotate it with None as the description. But this is really an "enforcement" thing, and might not belong in this library.

Dealing with context is a beast in chat apps, so more work here might be helpful.

For example, I use meta-functions that unlock others, to prevent context-bloat.

Might put that in another lib soon, or put it here.

Metadata

Release files for ai-functions 0.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-functions 0.4.3
File Size Uploaded
ai_functions-0.4.3.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-functions 0.4.3
File Interpreter ABI Platform
ai_functions-0.4.3-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / ai_functions-0.4.3.tar.gz

Download URL ai_functions-0.4.3.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7becafbaf33a12cc000be172281d8d1c4686af01cd77e61076545216b44007d2
BLAKE2b-256 checksum
How to use checksums
42362d7f35fd94ab1e00e16dbff9e526f3d7b50f5ffcd43871369e8ce9dad0f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Windows/10

Release files / ai_functions-0.4.3-py3-none-any.whl

Download URL ai_functions-0.4.3-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31cabc3d2f3ce4095c4552ce9abbfde12ee2ce4fc74c412fee283bb6e248259f
BLAKE2b-256 checksum
How to use checksums
8ee619c2f204cee281304374f007a5767796c485132a331fade5b3ee7562f84f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Windows/10

Release history Release notifications | RSS feed

This release

0.4.3 This release

2 release files

0.4.2

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page