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

Crafting Prompts, Unlocking Structured Outputs Pydantic Prompter is a lightweight tool designed for effortlessly constructing prompts and obtaining Pydantic objects as outputs.

Pydantic Prompter simplifies the art of prompt engineering, offering a seamless experience as you call LLMs like regular functions. With Pydantic Prompter, you unlock structured text generation and effortlessly obtain Pydantic objects as outputs.

The design of the library's API draws inspiration by DeclarAI. Other alternatives Outlines and Jsonformer

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

Why should you use Pydantic Prompter

💻 Seamless LLM Integration: Pydantic Prompter supported multiple LLM providers, including Cohere, Bedrock, and OpenAI, right out of the box. This meant we could easily switch between providers without modifying our code, ensuring flexibility and portability.

📦 Structured Outputs: By leveraging Pydantic models, Pydantic Prompter automatically parsed the LLM's output into structured Python objects. Manual parsing became a thing of the past, and we enjoyed consistently formatted data that was a breeze to work with.

✍️ Easy Prompt Engineering: Crafting effective prompts is an art, and Pydantic Prompter made us all masters. By defining prompts using Python classes and string interpolation, we created readable, maintainable, and reusable prompts.

🔧 Reusable Components: Pydantic Prompter encouraged a modular approach, allowing us to define reusable prompt components such as instructions, examples, and constraints. This promoted code reuse and made maintaining our code effortless.

🐛 Logging and Debugging: Built-in logging and debugging features meant we could quickly identify and resolve any issues, ensuring a smooth and efficient development process, free of bugs and errors.

Installation

To install Pydantic Prompter, use the following command:

pip install 'pydantic-prompter[openai]'

Setup

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

export OPENAI_API_KEY=<your openai token>

Diving into Pydantic Prompter

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]

Creating a Prompt Function

Now, let's create a Pydantic Prompter function using the @Prompter decorator. You can define your prompt 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 }}"

    """

Executing Your Prompt

Execute your prompt function just like a regular Python function:

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

For debugging, you can inspect the generated prompt using:

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

Explore the Possibilities

With Pydantic Prompter, you're equipped to seamlessly integrate LLMs into your projects, whether it's generating content, answering queries, or building creative applications. Dive into the Documentation to explore more examples, learn about advanced features, and discover the full potential of Pydantic Prompter.

Contribute to the open-source project, share your feedback, and join the community to shape the future of prompt engineering!

Happy Prompting! 🌟💻🤖

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

Uploaded Source

Built Distribution

pydantic_prompter-0.1.31-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_prompter-0.1.31.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.15.3 CPython/3.10.12 Linux/6.5.0-1021-azure

File hashes

Hashes for pydantic_prompter-0.1.31.tar.gz
Algorithm Hash digest
SHA256 0fe58c9c0b756079a70b06476e28fe44feef4bdeb9a031b2d2410b96f7d2dcd5
MD5 3142e6b76d356310526e01682408a05b
BLAKE2b-256 c7f85e3140b45dceb999515ff363c7959880424c190f090f1f8bfa6fac026abc

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_prompter-0.1.31-py3-none-any.whl
Algorithm Hash digest
SHA256 07c55a44f58031c259085e65b74c9b173b44a0cb823ac61c556425ae387721aa
MD5 16b65a7c5a14fa2232c1d80ebbbb8121
BLAKE2b-256 aef2cca620cbf22a1d64c074489ed7cab933111d9b5bafa8857225e5bbc269df

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