Skip to main content

A library for writing LLM prompts as Python functions

Project description

PromptScript

PromptScript bridges natural language and programming by integrating English with Python. It introduces a single @prompt decorator along with Python conventions to seamlessly integrate LLM interactions into your code.

Key Principles

  1. Lowering Friction for Experimentation: PromptScript aims to reduce the overhead of working with LLMs in Python. By keeping syntax minimal, you can move quickly from idea to functioning prototype.

  2. Modularity and Ecosystem Integration: We build on Python’s extensive libraries and tooling. You can incorporate data handling, Pydantic models, and other Python-based systems while relying on standard Python packaging approaches.

  3. User-Focused Experience: PromptScript is designed to integrate with your existing workflow—whether that’s a code editor (like VS Code), a notebook environment, or a custom application. You write Python code; PromptScript handles the LLM aspects in a straightforward way.

Key Features

  • @prompt Decorator: Wrap Python functions that contain prompt logic.
  • Type-Safe Prompting: Optionally use type hints or Pydantic models for safer, more reliable structured outputs.
  • Multi-Modal Inputs: Use images, text, or binary attachments alongside your prompts.
  • Built-in Observability: Collect execution traces and debug logs (through our tracer and persistence modules).
  • Persistent Sessions: Save execution traces in JSON format for later analysis.
  • Minimal Dependencies: The library is self-contained and fits well in existing Python development environments.

Setup

Below are instructions for a typical setup using Python 3.10 or higher.

Using pyenv, venv, and pip (Ubuntu example)

  1. Install System Dependencies

    sudo apt update
    sudo apt install -y make build-essential libssl-dev zlib1g-dev \
        libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
        libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
        python3-openssl git
    
  2. Install pyenv Follow the official pyenv installation instructions.

  3. Set up Python environment:

    cd promptscript
    pyenv install
    pyenv local
    python -m venv venv
    source venv/bin/activate
    pip install --upgrade pip
    pip install -r requirements.txt
    

Getting Started on Windows

  1. Clone the repository:

    git clone https://github.com/jflam/promptscript.git
    cd promptscript
    
  2. Create and activate a virtual environment:

    python -m venv .venv
    .venv\Scripts\activate.bat
    
  3. Install the dependencies:

    pip install -r requirements.txt
    
  4. Optionally install PromptScript in editable mode:

    pip install -e .
    

    This installs symlinks so that editing the source immediately reflects in your environment.

  5. Set up your OpenAI API key (if you plan to use OpenAI as a provider):

    setx OPENAI_API_KEY <your-api-key-here>
    

    (Restart the terminal if needed to pick up environment changes.)

Basic Example

With PromptScript, you define a function that uses the @prompt decorator and write your prompt inside. When you call that Python function, the library manages the LLM interaction. For example:

from pscript import gen, prompt

@prompt
def tell_joke() -> str:
    """
    Write a short dad joke about Java.
    """
    return gen("Tell me a dad joke about Java.")

if __name__ == "__main__":
    joke = tell_joke()
    print(joke)
  • Use the return gen(...) syntax within your decorated function to supply the prompt text.
  • When the function is called, PromptScript invokes the LLM provider (e.g. OpenAI, Anthropic, etc.).
  • The result is captured and returned as a normal Python string.

Structured Output with Pydantic

You can optionally specify a Pydantic model return type for more robust type-safety and structured data handling:

from pydantic import BaseModel
from pscript import gen, prompt

class JokeData(BaseModel):
    setup: str
    punchline: str

@prompt
def typed_joke() -> JokeData:
    """
    Prompt the LLM to return a structured joke with fields setup and punchline.
    """
    return gen("Tell me a dad joke about Java in JSON format with 'setup' and 'punchline'.")

PromptScript will then parse the LLM’s JSON response directly into the JokeData model. If parsing fails, you get a Python exception right away.

Observability & Tracing

PromptScript has a built-in tracer that captures:

  • The function name and timestamp
  • Input data, output data, and errors
  • Execution duration

This data is stored in JSON under a .promptscript/traces directory. You can customize or disable the tracer/persistence if needed. It’s useful for logging, debugging, or replaying your experiments.

Multi-Modal Support

PromptScript supports sending additional binary attachments (e.g., images) to the LLM. For example:

from pscript import gen, prompt, Attachment

@prompt
def analyze_image(image: Attachment) -> str:
    return gen("Describe what you see in this image.", attachments=[image])

if __name__ == "__main__":
    with open("example.jpg", "rb") as f:
        img_data = f.read()
    image_attachment = Attachment(data=img_data, mime_type="image/jpeg")
    result = analyze_image(image_attachment)
    print(result)

Advanced Usage

  • Provider-Specific Settings: You can configure providers (OpenAI, Anthropic, Azure OpenAI, etc.) in the promptscript.yml config file.
  • Environment Variables: For authentication keys, you typically set environment variables (e.g. OPENAI_API_KEY, ANTHROPIC_API_KEY).
  • Concurrency: For more complex usage, you can manage concurrency or parallel calls using standard Python concurrency patterns.
  • Error Handling: If a response cannot be parsed into your Pydantic model, PromptScript raises an exception right away, letting you handle the error in pure Python.

Contributing

  1. Fork the repo and create a feature branch.
  2. Make your changes, add tests if applicable.
  3. Submit a pull request for review.

We welcome fixes, new features, and documentation improvements!

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

promptscript_python-0.4.0.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

promptscript_python-0.4.0-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file promptscript_python-0.4.0.tar.gz.

File metadata

  • Download URL: promptscript_python-0.4.0.tar.gz
  • Upload date:
  • Size: 32.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for promptscript_python-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c50963cc649525e411e16fff852368c1307a4fe98a25026d8922a2a599cc920f
MD5 07a5bc1dfb704bd3fd8fb358a0d91b58
BLAKE2b-256 e8e95a6b54305a5f9866da0ea99aeb9ecbec7bd9eca3fda41bb3c8c5db3f43e7

See more details on using hashes here.

File details

Details for the file promptscript_python-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for promptscript_python-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 26137f6674c6c4dd0764fae1a7a9bc88f7b95621c07706d934c534125a0885aa
MD5 3e09c18cabd21ec35906c97b7fbf7ec6
BLAKE2b-256 96624cc3519e82f5ae5582f571163ef4faba301ccb27d03a9fb04037fb4041a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page