Quick Start
Get started with the Autoblocks Python Prompt SDK to manage and execute your prompts with type safety and autocomplete.
Python Prompt SDK Quick Start
Installation
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.
- Go to the apps page
- Click “Create App”
- Select “Prompt App” as the app type
- Give your app a name and description
- 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:
Run the CLI
Installing the autoblocksai
package adds the prompts generate_v2
CLI to your path:
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:
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:
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
:
Then, throughout your application, import the entire prompt_managers
module and use the prompt managers as needed:
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: