> ## Documentation Index
> Fetch the complete documentation index at: https://docs.autoblocks.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 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

<CodeGroup>
  ```bash poetry
  poetry add autoblocksai
  ```

  ```bash pip
  pip install autoblocksai
  ```
</CodeGroup>

<Note>
  The prompt SDK requires [`pydantic`](https://docs.pydantic.dev/latest/) v2 to be installed.

  You can install it with `poetry add pydantic` or `pip install pydantic`.
</Note>

## 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](https://app-v2.autoblocks.ai/apps)
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

<Note>
  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").
</Note>

## 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](https://app-v2.autoblocks.ai/settings/api-keys)
page and set it as an environment variable:

```bash
export AUTOBLOCKS_V2_API_KEY=...
```

### Run the CLI

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

```bash
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.

<Note>
  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
</Note>

<Note>
  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.
</Note>

## 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:

<CodeGroup>
  ```python specific minor version
  from my_project.autoblocks_prompts import my_app

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

  ```python latest minor version
  from my_project.autoblocks_prompts import my_app

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

  ```python specific revision
  from my_project.autoblocks_prompts import my_app

  mgr = my_app.text_summarization_prompt_manager(
    major_version="undeployed",
    minor_version="clvods6wq0003m44zc8sizv2l",
  )
  ```

  ```python latest undeployed revision
  from my_project.autoblocks_prompts import my_app

  mgr = my_app.text_summarization_prompt_manager(
    major_version="undeployed",
    minor_version="latest",
  )
  ```
</CodeGroup>

<Note>
  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`](/v2/guides/prompt-management/python/sdk-reference#autoblocks-prompt-manager) reference for more information.
</Note>

## 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`](/v2/guides/prompt-management/python/sdk-reference#prompt-execution-context)
instance that gives you fully-typed access to the prompt's templates and parameters:

```python
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`:

```python
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:

```python
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:

```python
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:
  ...
```
