Manage Prompt with the SDK

In this guide, we'll explore all actions performed through the SDK: creating variants, committing changes, deploying changes, and fetching configurations. For a simpler introduction, please refer to the prompt mangement tutorial. Before starting, we recommend to get acquainted with how versioning works in Lexica. You can find more details on the concepts page, or read the quick summary below.

Add alt text and captions to your images

Lexica follows a structure similar to git for prompt versioning. Instead of a single commit history, you can create multiple branches—called variants. Each variant represents a distinct approach or solution you're exploring.

To move from experimentation to deployment, Lexica uses environments like development, staging, and production. You can deploy specific versions of your variants to these environments, controlling what gets tested and what goes live.

The workflow of deploying something to production is therefore as follows:

  1. (optionally) Create a new variant (branch)

  2. Commit a change to that variant

  3. Deploy that commit (variant/version) to an environment

Setup

Before using the SDK, you need to initialize it using the ag.init() method.

Creating a new prompt

Each prompt in Lexica is a unique application. Currently creating a prompt is only available in the web UI. To create a new prompt, just click on create a new prompt and select whether it's a chat or completion prompt in the web UI.

Committing changes

Creating a New Variant

To create a new variant, use the VariantManager.create method.

This command will create a new variant and initialize it with the first commit containing the parameters provided

  • Parameters:

    • app_slug: The slug of your application.

    • variant_slug: The slug of the new variant.

    • parameters: A dictionary containing the initial configuration parameters.

Note: If a variant with the same slug and version already exists, the SDK will raise an exception.

Sample Output:

Committing Changes to a Variantarrow-up-right

To save changes to a variant (creating a new version), use the VariantManager.commit method with explicit parameters.

circle-info

IMMUTABILITY

Each commit creates a new version of the variant. Versions are immutable once created.

Sample Output:

Deploying to an Environmentarrow-up-right

To deploy a variant to an environment, use the DeploymentManager.deploy method with the variant reference and environment_slug: The slug of the environment (development, staging, or production).

Sample Output:

Fetching Configurationsarrow-up-right

You can fetch the configurations from a variant reference (app_slug, variant_slug, variant_version) or an environment reference (app_slug, environment_slug). The default behavior when fetching is to fetch the latest configuration from the production environment. If you don't provide a _version parameter but only a variant_slug or an environment_slug, the SDK will fetch the latest version of the variant from the specified environment/variant.

Default Behavior when fetchingarrow-up-right

If you don't provide either variant or environment identifiers, the SDK fetches the latest configuration deployed to the production environment.

Fetching by Variant Referencearrow-up-right

Sample Output:

Fetching by Environment Referencearrow-up-right

Sample Output:

Deleting a Variantarrow-up-right

To delete a variant, use the VariantManager.delete method.

Listing All Variantsarrow-up-right

To list all variants of an application, use the VariantManager.list method.

Sample Output:

Fetching a Variant's historyarrow-up-right

To list all versions for a variant of an application, use the VariantManager.list method.

Sample Output:

Asynchronous Operationsarrow-up-right

If your application uses asynchronous programming, you can use the async versions of the methods (see the a prefix in the function name).

Last updated