Python Prompt SDK Quick Start

Installation

poetry add autoblocksai

The prompt SDK requires pydantic v2 to be installed.

You can install it with poetry add pydantic or pip install pydantic.

Create a Prompt App

Before creating prompts, you need to create a prompt app. Apps are the top-level organizational unit in Autoblocks and help you manage access and track usage of your prompts.

  1. Go to the apps page
  2. Click “Create App”
  3. Select “Prompt App” as the app type
  4. Give your app a name and description
  5. Configure access settings for your team members

All prompts you create will be associated with this app. Make sure to choose a name that reflects the purpose of your prompts (e.g., “Content Generation” or “Customer Support”).

Autogenerate Prompt Classes

The prompt SDK ships with a CLI that generates Python classes with methods and arguments that mirror the structure of your prompt’s templates and parameters. This gives you type safety and autocomplete when working with Autoblocks prompts in your codebase.

Set Your API Key

Get your Autoblocks API key from the settings page and set it as an environment variable:

export AUTOBLOCKS_API_KEY=...

Run the CLI

Installing the autoblocksai package adds the prompts generate_v2 CLI to your path:

poetry run prompts generate-v2 --output-dir my_project/autoblocks_prompts

Running the CLI will create a file at the outfile-dir location you have configured. You will need to run the prompts generate-v2 CLI any time you deploy a new major version of a prompt.

When a new major version of a prompt is available and you want to update your codebase to use it, the process will be:

  • run prompts generate-v2
  • update any broken code

If you’re not using poetry, make sure to activate the virtualenv where the autoblocksai package is installed so that the prompts generate-v2 CLI can be found.

Import and Use a Prompt Manager

For each application in your organization, there will be a factory named after the application slug. For example, if the application slug is "my_app", then you can import like from autoblocks_prompts import my_app. Using that factory, you can initialize a prompt manager for each prompt in the application. If you have a prompt with the ID "text-summarization", then you can initialize a prompt manager like my_app.text_summarizatio_prompt_manager.

Initialize the Prompt Manager

Create a single instance of the prompt manager for the lifetime of your application. The only required argument when initializing a prompt manager is the minor version. To specify the minor version, use the enum that was autogenerated by the CLI:

from my_project.autoblocks_prompts import my_app

mgr = my_app.text_summarization_prompt_manager(
  major_version="1",
  minor_version="0",
)

When the version is set to "latest", the prompt manager periodically refresh the in-memory prompt in the background according to the refresh_interval. See the AutoblocksPromptManager reference for more information.

Execute a Prompt

The exec method on the prompt manager starts a new prompt execution context. It is a context manager that creates a PromptExecutionContext instance that gives you fully-typed access to the prompt’s templates and parameters:

with mgr.exec() as prompt:
    params = dict(
        model=prompt.params.model,
        temperature=prompt.params.temperature,
        max_tokens=prompt.params.max_tokens,
        messages=[
            dict(
                role="system",
                content=prompt.render_template.system(
                    language_requirement=prompt.render_template.util_language(
                        language="Spanish",
                    ),
                    tone_requirement=prompt.render_template.util_tone(
                        tone="formal",
                    ),
                ),
            ),
            dict(
                role="user",
                content=prompt.render_template.user(
                  document="mock document",
                ),
            ),
        ],
    )

    response = openai.chat.completions.create(**params)

Organizing Multiple Prompt Managers

If you are using many prompt managers, we recommend initializing them in a single file and importing them as a module:

prompt_managers.py:

from my_project.autoblocks_prompts import my_app

text_summarization = my_app.text_summarization_prompt_manager(
  major_version="1",
  minor_version="0",
)

flashcard_generator = my_app.flashcard_generator_prompt_manager(
  major_version="1",
  minor_version="0",
)

study_guide_outline = my_app.study_guide_outline_prompt_manager(
  major_version="1",
  minor_version="0",
)

Then, throughout your application, import the entire prompt_managers module and use the prompt managers as needed:

from my_project import prompt_managers

with prompt_managers.text_summarization.exec() as prompt:
  ...

with prompt_managers.flashcard_generator.exec() as prompt:
  ...

with prompt_managers.study_guide_outline.exec() as prompt:
  ...

This is preferable over importing each prompt manager individually, as it keeps the context of it being a prompt manager in the name. If you were to import each manager individually, it is hard to tell at a glance that it is a prompt manager:

from my_project.prompt_managers import text_summarization

# Somewhere deep in a file, it is not clear
# what the `text_summarization` variable is
with text_summarization.exec() as prompt:
  ...