Skip to main content

promptic

promptic is a lightweight, decorator-based Python library that simplifies the process of interacting with large language models (LLMs) using litellm. With promptic, you can effortlessly create prompts, handle input arguments, receive structured outputs from LLMs, and enable function/tool calling capabilities with just a few lines of code.

Installation

pip install promptic

Usage

Basics

from promptic import llm

@llm
def president(year):
    """Who was the President of the United States in {year}?"""

print(president(2000))
# The President of the United States in 2000 was Bill Clinton until January 20th, when George W. Bush was inaugurated as the 43rd President.

Structured Outputs

from pydantic import BaseModel
from promptic import llm

class Capital(BaseModel):
    country: str
    capital: str

@llm
def capital(country) -> Capital:
    """What's the capital of {country}?"""

print(capital("France"))
# country='France' capital='Paris'

Agents

from datetime import datetime

from promptic import llm

@llm(
    system="You are a helpful assistant that manages schedules and reminders",
    model="gpt-4o-mini"
)
def scheduler(command):
    """{command}"""

@scheduler.tool
def get_current_time():
    """Get the current time"""
    print("getting current time")
    return datetime.now().strftime("%I:%M %p")

@scheduler.tool
def add_reminder(task: str, time: str):
    """Add a reminder for a specific task and time"""
    print(f"adding reminder: {task} at {time}")
    return f"Reminder set: {task} at {time}"

@scheduler.tool
def check_calendar(date: str):
    """Check calendar for a specific date"""
    print(f"checking calendar for {date}")
    return f"Calendar checked for {date}: No conflicts found"

print(scheduler("Can you check my calendar for tomorrow and set a reminder for a team meeting at 2pm?"))
# checking calendar for 2023-10-04
# adding reminder: Team meeting at 2023-10-04T14:00:00
# I've checked your calendar for tomorrow, and there are no conflicts. I've also set a reminder for your team meeting at 2 PM.

Streaming

The streaming feature allows real-time response generation, useful for long-form content or interactive applications:

from promptic import llm

@llm(stream=True)
def generate_article(topic):
    """Write a detailed article about {topic}. Include introduction, 
    main points, and conclusion."""

for chunk in generate_article("artificial intelligence"):
    print(chunk, end="", flush=True)

Error Handling and Dry Runs

Dry runs allow you to see which tools will be called and their arguments without invoking the decorated tool functions. You can also enable debug mode for more detailed logging.

from promptic import llm

@llm(
    system="you are a posh smart home assistant named Jarvis",
    dry_run=True,
    debug=True,
)
def jarvis(command):
    """{command}"""

@jarvis.tool
def turn_light_on():
    """turn light on"""
    return True

@jarvis.tool
def get_current_weather(location: str, unit: str = "fahrenheit"):
    """Get the current weather in a given location"""
    return f"The weather in {location} is 45 degrees {unit}"

print(jarvis("Please turn the light on and check the weather in San Francisco"))
# ...
# 2024-11-21 13:29:08,587 - promptic - INFO - promptic.py:185 - [DRY RUN]: function_name = 'turn_light_on' function_args = {}
# 2024-11-21 13:29:08,587 - promptic - INFO - promptic.py:185 - [DRY RUN]: function_name = 'get_current_weather' function_args = {'location': 'San Francisco'}
# ...

Features

  • Decorator-based API: Easily define prompts using function docstrings and decorate them with @promptic.llm.
  • Argument interpolation: Automatically interpolate function arguments into the prompt using {argument_name} placeholders within docstrings.
  • Pydantic model support: Specify the expected output structure using Pydantic models, and promptic will ensure the LLM's response conforms to the defined schema.
  • Streaming support: Receive LLM responses in real-time by setting stream=True when calling the decorated function.
  • Simplified LLM interaction: No need to remember the exact shape of the OpenAPI response object or other LLM-specific details. promptic abstracts away the complexities, allowing you to focus on defining prompts and receiving structured outputs.
  • Build Agents Seamlessly: Decorate functions as tools that the LLM can use to perform actions or retrieve information.

Why promptic?

promptic is designed to be simple, functional, and robust, providing exactly what you need 90% of the time when working with LLMs. It eliminates the need to remember the specific shapes of OpenAPI response objects or other LLM-specific details, allowing you to focus on creating prompts and receiving structured outputs.

With its legible and concise codebase, promptic is reliable easy to understand. It leverages the power of litellm under the hood, ensuring compatibility with a wide range of LLMs.

License

promptic is open-source software licensed under the Apache License 2.0.

Metadata

Release files for promptic 1.1.1

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

Source distribution (sdist)

Source distribution for promptic 1.1.1
File Size Uploaded Provenance Actions
promptic-1.1.1.tar.gz 77.3 kB With provenance Details for promptic-1.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for promptic 1.1.1
File Interpreter ABI Platform Actions
promptic-1.1.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for promptic-1.1.1-py2.py3-none-any.whl

Total release size: 87.3 kB

Release files / promptic-1.1.1.tar.gz

Download URL promptic-1.1.1.tar.gz
Size 77.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9ec873eba59fa1b3043b3515eeb334e075c57191de53253b0e817588ed7612d9
BLAKE2b-256 checksum
How to use checksums
58a0d9d1c51ebd63d62552b2909fe52ea17309389f2da4251b69ca291ac18f9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2024.

Transparency log

Release files / promptic-1.1.1-py2.py3-none-any.whl

Download URL promptic-1.1.1-py2.py3-none-any.whl
Size 9.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e5161779ada233b23bba6dc3fc370a105511e0b5e17e431eda09e63c60741fea
BLAKE2b-256 checksum
How to use checksums
252fd3cbdd8385e958aba14205934492ee3b61a80348f47c61e0ee7d91b3e3da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2024.

Transparency log

Security for promptic 1.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for promptic-1.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2024.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files Full provenance

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