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.20.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

pydantic_prompter-0.1.20-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_prompter-0.1.20.tar.gz
Algorithm Hash digest
SHA256 6118c9054a2dd8910e4ba4ea86f3a99c747fb961b4aa39a30972ab6caa0f7d1d
MD5 c768bed373ce65e90a067d256b82735c
BLAKE2b-256 c8ee56ba8a7b0138906876ff077d3b628f39e1ab9804f8113f3d328451054ed9

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_prompter-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 2ce77af1b57de7f642c77169f61c2e70c3608a0cddac323018c5331be685676a
MD5 01bc9692a81585be719795cbc123935b
BLAKE2b-256 b493f8163b2275e622b5b2b2b68fe837166145bad445768b8760350cb50b83e2

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