Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility to build "natural language functions" #66

Merged
merged 3 commits into from
Apr 24, 2023
Merged

Conversation

rlouf
Copy link
Member

@rlouf rlouf commented Apr 24, 2023

import json
import outlines.text as text
import outlines.models as models


@text.prompt
def prime_numbers(n: int):
    """Return a list that contains all prime numbers between 1 and {{n}}.

    The output must be parsable as a Python list.
    """

def parse(result):
    return json.loads(result)

get_prime_numbers = text.function(
   models.text_completion.openai("gpt-3.5-turbo"),
   prime_numbers,
   parse
)

get_prime_numbers(10)
# [2, 3, 5, 7]

For more complex outputs one can pass a Pydantic model to text.function, which will be used to parse the output:

from pydantic import BaseModel
import outlines.text as text

class Joke(BaseModel):
    joke: str
    explanation: str


@text.prompt
def joke_ppt(n: int):
    """Tell a joke and explain why the joke is funny.

    RESPONSE FORMAT:
    {
       "joke": "The joke"
       "explanation": "The explanation of why the joke is funny"
    }
    """

tell_a_joke = text.function(
   models.text_completion.openai("gpt-3.5-turbo"),
   joke_ppt,
   Joke
)

get_prime_numbers(10)
# [2, 3, 5, 7]

@rlouf rlouf merged commit 0bab88a into main Apr 24, 2023
@rlouf rlouf deleted the text-function branch April 24, 2023 10:08
@rlouf rlouf linked an issue Apr 24, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Pydantic to suggest and enforce response format
1 participant