Easily define Python functions for structured output from GPT-4.
pip install autofunc
Expects OPENAI_API_KEY to be defined in your environment.
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")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.'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'
}