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

Uploaded Source

Built Distribution

pydantic_prompter-0.1.16-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_prompter-0.1.16.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.16.tar.gz
Algorithm Hash digest
SHA256 b78a6e0955e14e1fa980b01609df8e350c0779127b656ba9bfc8d0991c5cb9cd
MD5 4d3cad32c6032dac55409e6fc328782a
BLAKE2b-256 773c18e7107654b88da2bce19353b1f26f5545daa4c587bb85bdb072586e003f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_prompter-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 47d7cce2f410d43552d502b2f6ace3f05ef49fc250d494b8e23be2ddc1376190
MD5 d24b20a8f21dfba52cbf8a7c271e4cc9
BLAKE2b-256 eb781e4e0cb831bb0cbc34fd48e1f39e913b51531198f2cd8c443c3b726b8daf

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