Skip to main content

A small utility to generate JSON schemas for python functions.

Project description

Function schema

This is a small utility to generate JSON schemas for python functions. With power of type annotations, it is possible to generate a schema for a function without describing it twice.

At this moment, extracting schema from a function is only useful for OpenAI API function-call feature. But it can be used for other purposes for example to generate documentation in the future.

Installation

pip install function-schema

Usage

from typing import Annotated, Optional
import enum

def get_weather(
    city: Annotated[str, "The city to get the weather for"],
    unit: Annotated[
        Optional[str],
        "The unit to return the temperature in",
        enum.Enum("Unit", "celcius fahrenheit")
    ] = "celcius",
) -> str:
    """Returns the weather for the given city."""
    return f"Weather for {city} is 20°C"

Function description is taken from the docstring. Type hinting with typing.Annotated for annotate additional information about the parameters and return type.

  • type can be typing.Union, typing.Optional. (T | None for python 3.10+)
  • string value of Annotated is used as a description
  • enum value of Annotated is used as an enum schema
import json
from function_schema import get_function_schema

schema = get_function_schema(get_weather)
print(json.dumps(schema, indent=2))

Will output:

{
  "name": "get_weather",
  "description": "Returns the weather for the given city.",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "The city to get the weather for"
      },
      "unit": {
        "type": "string",
        "description": "The unit to return the temperature in",
        "enum": [
          "celcius",
          "fahrenheit"
        ],
        "default": "celcius"
      }
    },
  }
  "required": [
    "city"
  ]
}

You can use this schema to make a function call in OpenAI API:

import openai
openai.api_key = "sk-..."

result = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
      {"role": "user", "content": "What's the weather like in Seoul?"}
    ],
    functions=[
        get_function_schema(get_weather)
    ],
    function_call="auto",
)

CLI usage

function_schema mymodule.py my_function

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

function-schema-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file function-schema-0.1.0.tar.gz.

File metadata

  • Download URL: function-schema-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for function-schema-0.1.0.tar.gz
Algorithm Hash digest
SHA256 791443e8c538592f86a57f00302a7c8c6e5ca3804d96d9ce47765795c7547caf
MD5 3ec6aecd226eb05fad81f22ce91c1734
BLAKE2b-256 9130942cf1e3418e335c72d7b6282847612c89f0c146b1bc7f8178dfb2a34d8f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page