Skip to main content

Easily define python functions for structured LLM output

Project description

autofunc

Easily define Python functions for structured output from GPT-4.

:warning: This was primarily written for personal use, and is neither well-tested nor documented. There are similar and much better projects out there, such as magentic.

Install

pip install autofunc

Usage

Expects OPENAI_API_KEY to be defined in your environment.

Define your function

from autofunc import AutoFunc


swap_animals = AutoFunc("Swap the animals in the sentence. Eg. The dog barked at the cat -> the cat meowed at the dog")

Call it

swap_animals("The tigers growled at each other until they saw the snakes slithering near the trees.")

'The snakes hissed at each other until they saw the tigers prowling near the trees.'

Use pydantic for structured output

from pydantic import BaseModel, Field

class TravelDescription(BaseModel):
    from_city: str = Field(description="City name including the country")
    to_city: str = Field(description="City name including the country")
    mode_of_travel: str = Field(description="eg. train or flight")
    description: str = Field(description="A one-line description of a fictional journey, in the style of an adventure game. Simple present tense.")
    duration: str

travel_fn = AutoFunc("Describe travelling between the two provided cities.", TravelDescription)
travel_fn("londres to paris")

{
 'from_city': 'London, United Kingdom',
 'to_city': 'Paris, France',
 'mode_of_travel': 'train',
 'description': 'You embark on a journey from the bustling city of London, crossing the English Channel through the Eurotunnel, to the romantic city of Paris.',
 'duration': '2 hours 20 minutes'
}

CHANGES

2023.10.05: Initial release

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

autofunc-0.1.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

autofunc-0.1.0-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

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