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

Uploaded Source

Built Distribution

pydantic_prompter-0.1.12-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_prompter-0.1.12.tar.gz
Algorithm Hash digest
SHA256 fcc03baada2f004969582315adc8b6f15ae1a23190eaff71064f8767318c5b0b
MD5 b90eb7f4f1887b32e89f089e5d99baac
BLAKE2b-256 7dfe0f7d19347c7fef4c955c4299b90d5c46f4e076af4f3c2b152edb2c2685ab

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_prompter-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 d23d7065bd801ebb206ae854e1c0b7a71b3be2aa175fc47151a9f5877f36c6f5
MD5 4b411447aa9058201143055b022bd72b
BLAKE2b-256 b06356bca570dd9077f1abcbe7b533741af4924efdd1355a4ec47d8c7e7b09ca

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