Skip to main content

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! 🌟💻🤖

Metadata

Release files for pydantic-prompter 0.1.33

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-prompter 0.1.33
File Size Uploaded
pydantic_prompter-0.1.33.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-prompter 0.1.33
File Interpreter ABI Platform
pydantic_prompter-0.1.33-py3-none-any.whl Python 3 none any Details

Total release size: 35.9 kB

Release files / pydantic_prompter-0.1.33.tar.gz

Download URL pydantic_prompter-0.1.33.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c47f2be6f833310dc2c5528acbf929482f890c8bc632ed242af5a4c3df72ebab
BLAKE2b-256 checksum
How to use checksums
93afbcea1d7af70711546ffbdf3717df90a05259f13e62ee94a3f0c42059b352
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.3 CPython/3.10.12 Linux/6.5.0-1021-azure

Release files / pydantic_prompter-0.1.33-py3-none-any.whl

Download URL pydantic_prompter-0.1.33-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e6d07cc7250b45fbda32dbdd422d56e8f91362774f09fd0b147660112ebe984
BLAKE2b-256 checksum
How to use checksums
6fa6689c2807e1fd3e9809856319bbeafec49ff24450678f68c1ddeeed7fc319
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.15.3 CPython/3.10.12 Linux/6.5.0-1021-azure
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page