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.

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:
(optionally) Create a new variant (branch)
Commit a change to that variant
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 Variant
To save changes to a variant (creating a new version), use the VariantManager.commit method with explicit parameters.
IMMUTABILITY
Each commit creates a new version of the variant. Versions are immutable once created.
Sample Output:
Deploying to an Environment
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 Configurations
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 fetching
If you don't provide either variant or environment identifiers, the SDK fetches the latest configuration deployed to the production environment.
Fetching by Variant Reference
Sample Output:
Fetching by Environment Reference
Sample Output:
Deleting a Variant
To delete a variant, use the VariantManager.delete method.
Listing All Variants
To list all variants of an application, use the VariantManager.list method.
Sample Output:
Fetching a Variant's history
To list all versions for a variant of an application, use the VariantManager.list method.
Sample Output:
Asynchronous Operations
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