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
-
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.
-
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.
-
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
@promptDecorator: 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)
-
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
-
Install
pyenvFollow the official pyenv installation instructions. -
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
-
Clone the repository:
git clone https://github.com/jflam/promptscript.git cd promptscript
-
Create and activate a virtual environment:
python -m venv .venv .venv\Scripts\activate.bat
-
Install the dependencies:
pip install -r requirements.txt
-
Optionally install PromptScript in editable mode:
pip install -e .
This installs symlinks so that editing the source immediately reflects in your environment.
-
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.ymlconfig 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
- Fork the repo and create a feature branch.
- Make your changes, add tests if applicable.
- Submit a pull request for review.
We welcome fixes, new features, and documentation improvements!
Metadata
Release files for promptscript-python 0.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| promptscript_python-0.4.4.tar.gz | 64.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| promptscript_python-0.4.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 139.2 kB
Release files / promptscript_python-0.4.4.tar.gz
| Download URL | promptscript_python-0.4.4.tar.gz |
|---|---|
| Size | 64.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b3fb6541a28cc5156a3b28efd07995b1827f1f02e537f31c074715ec8786d18
|
|
BLAKE2b-256 checksum How to use checksums |
2c96152be97280d893bec3cd579622497b2058789dc6fca11c8be44b0c9301a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.6
|
Release files / promptscript_python-0.4.4-py3-none-any.whl
| Download URL | promptscript_python-0.4.4-py3-none-any.whl |
|---|---|
| Size | 74.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
daec29aec3e8abc4bdc30b4fad7946ccfa2ef42fd714ff9dc66f21193808dc24
|
|
BLAKE2b-256 checksum How to use checksums |
6fdf6afd0ba8b843674a5eb37a847ac714a8e5ca82f652b09e4c1d21af175ab9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.6
|