Skip to main content

Pydantic Prompter is a lightweight utility designed to facilitate the construction of prompts using YAML and generate Pydantic objects as outputs.

Project description

Pydantic Prompter

Pydantic Prompter is a lightweight utility designed to facilitate the construction of prompts using YAML and generate Pydantic objects as outputs.

Documentation https://helmanofer.github.io/pydantic-prompter

Installation

To install Pydantic Prompter, use the following command:

pip install pydantic-prompter

Setup

Before using Pydantic Prompter, ensure that you set your OpenAI API key as an environment variable:

export OPENAI_API_KEY=<your openai token>

Basic usage

Begin by defining your output model using Pydantic:

from pydantic import BaseModel, Field
from typing import List


class RecommendedEntry(BaseModel):
    id: str
    name: str
    reason: str = Field(
        description="Why this entry fits the query", default=""
    )


class RecommendationResults(BaseModel):
    title: str
    entries: List[RecommendedEntry]

Next, create a Prompter function, which is defined as a YAML string with Jinja2 templating or simple string formatting:

from pydantic_prompter import Prompter


@Prompter(llm="openai", jinja=True, model_name="gpt-3.5-turbo-16k")
def rank_recommendation(entries, query) -> RecommendationResults:
    """
    - system: You are a movie ranking expert
    - user: >
        Which of the following JSON entries fit best to the query. 
        order by best fit descending
        Base your answer ONLY on the given JSON entries, 
        if you are not sure, or there are no entries

    - user: >
        The JSON entries:
        {{ entries }}

    - user: "query: {{ query }}"

    """

Execute your function as follows:

my_entries = "[{\"text\": \"Description: Four everyday suburban guys come together as a ...."
print(rank_recommendation(entries=my_entries, query="Romantic comedy"))

For debugging purposes, inspect your prompt with:

print(rank_recommendation.build_string(entries=my_entries, query="Romantic comedy"))

For additional details, refer to the Documentation

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

pydantic_prompter-0.1.15.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

pydantic_prompter-0.1.15-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_prompter-0.1.15.tar.gz.

File metadata

  • Download URL: pydantic_prompter-0.1.15.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.9.1 CPython/3.10.13

File hashes

Hashes for pydantic_prompter-0.1.15.tar.gz
Algorithm Hash digest
SHA256 f477ed741316b54d30a0e0ca2dece39117791e99c402c99acea46fce4d6d96ed
MD5 0964cea53279d0340167f5fc4499ecce
BLAKE2b-256 a9033556d3addc3829a8b63338ae38b6fba8a192714ff8b118d0850691a42810

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_prompter-0.1.15-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_prompter-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 0760e562cc3e33dc7222ceea3d15dcee8ebf53f16a330efc092b632c9d584072
MD5 4029d2e6e61c28564ffe430df611c247
BLAKE2b-256 d2679b80bdfa47bd37b539317b5d7f042672c0383223ea37113700559c23f2a9

See more details on using hashes here.

Provenance

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