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 tool designed for effortlessly constructing prompts and obtaining Pydantic objects as outputs.
Seamlessly call LLMs like functions in Python with Pydantic Prompter. It handles prompt creation and output parsing to custom models for providers like Cohere, Bedrock, and OpenAI. Get OpenAi function calling API capabilities for any LLM. Structured text generation with less code.
The design of the library's API draws inspiration by DeclarAI. Other alternatives Outlines and Jsonformer
📄 Documentation https://helmanofer.github.io/pydantic-prompter
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>
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_prompter-0.1.30.tar.gz
.
File metadata
- Download URL: pydantic_prompter-0.1.30.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.13.0 CPython/3.10.12 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91d652913e0f16f2977a8867edabc601e61ffd09bd0cedc7afdd7c84e6fb9d50 |
|
MD5 | ec2ee31112fb8e68685795fad7d4f674 |
|
BLAKE2b-256 | 50374649b9f4867d23ab60318d1a32f07d483b54b88f7770b47cfabad70d13c5 |
Provenance
File details
Details for the file pydantic_prompter-0.1.30-py3-none-any.whl
.
File metadata
- Download URL: pydantic_prompter-0.1.30-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.13.0 CPython/3.10.12 Linux/6.5.0-1016-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91e699a2183cdf14723c9fb3760b7a88e7360a48edef11d21b47d9c5dc896856 |
|
MD5 | 1f22411393c30c1bf7e2f32ee48a2442 |
|
BLAKE2b-256 | 4189fb81f730b2755eee1763f3a28ac9dc4d9dd8d8824b2c1dfbe136ada39a64 |