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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_prompter-0.1.13.tar.gz
  • Upload date:
  • Size: 10.2 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.13.tar.gz
Algorithm Hash digest
SHA256 b4adb9c1c830e9eac56ee88b3198e62988c054f14743884226e6b08b024d6a90
MD5 3344f5ea6656d986a0dc2b56991270f3
BLAKE2b-256 10a9b0231d1900a1f031dec38d42df31a3acb46a151e06252cc92320ebc2d071

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_prompter-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 1fe93c83e18a4c4a5edb663e93ec3401141da5828141139bcd79495b424e55a6
MD5 d59a081b3abe4ca218c0372d02e9e04d
BLAKE2b-256 dd54b8411f017c4add70340d69ad1aa0aa3d043e3ac7ac0bff7c3f90485d0b04

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