Using Prompt Slugs with Python SDK

Prompt slugs created on the RagaAI Playground can be used with the Python SDK. It covers the initialisation of the SDK, managing prompts, retrieving prompt versions, and compiling prompts using the SDK.

1. Prerequisites

  • Ensure you have the RagaAI Python SDK installed:

    pip install ragaai-catalyst
  • Obtain your access_key and secret_key from the RagaAI admin, and have your base_url for the API endpoint.

2. Initialise RagaAICatalyst and PromptManager

First, you need to initialise the RagaAICatalyst instance and the PromptManager for your specific project.

from ragaai_catalyst import RagaAICatalyst
from ragaai_catalyst.prompt_manager import PromptManager

# Initialize RagaAICatalyst instance
catalyst = RagaAICatalyst(
    access_key="your_access_key",
    secret_key="your_secret_key",
    base_url="https://your-api-base-url.com/api"
)

# Create a PromptManager for your project
project_name = "your-project-name"
prompt_manager = PromptManager(project_name)

Replace the placeholders your_access_key, your_secret_key, your-api-base-url.com, and your-project-name with your actual credentials and project details.

3. List Available Prompts

You can list all the prompts available in your project using the list_prompts method.

pythonCopy code# List available prompts in the project
prompts = prompt_manager.list_prompts()
print("Available prompts:", prompts)

This will output a list of prompt slugs that have been created in the project.

4. List Prompt Versions

Each prompt can have multiple versions. You can list the versions of a specific prompt using its slug.

# List available versions for a specific prompt
prompt_name = "your_prompt_name"
versions = prompt_manager.list_prompt_versions(prompt_name)
print("Available versions for the prompt:", versions)

Replace your_prompt_name with the name of the prompt you want to query.

5. Get a Prompt Object

You can retrieve a prompt object by its name or by specifying both its name and version. This will allow you to access its details and use it further.

# Retrieve a prompt object by name
prompt_name = "your_prompt_name"
prompt = prompt_manager.get_prompt(prompt_name)
print("Prompt details:", prompt)

# Retrieve a specific prompt object by name and version
version = "your_version"
prompt = prompt_manager.get_prompt(prompt_name, version)
print(f"Prompt details for version {version}:", prompt)

Replace your_prompt_name and your_version with the appropriate prompt name and version.

6. Get Prompt Variables

You can get a list of variables required by the prompt to generate a response. These variables need to be provided when compiling the prompt.

# Get the variables required for the prompt
prompt_variables = prompt.get_variables()
print("Prompt variables:", prompt_variables)

This will output a dictionary of variables required by the prompt.

7. Compile Prompt

Once you have the prompt object and its required variables, you can compile the prompt by providing the necessary values for the variables.

# Compile the prompt with the required variables
compiled_prompt = prompt.compile(
    query="What's the weather?", 
    context="sunny", 
    llm_response="It's sunny today"
)
print("Compiled prompt:", compiled_prompt)

Replace the query, context, and llm_response with appropriate values based on the prompt variables.

8. Get Prompt Parameters

You can also retrieve additional parameters associated with the prompt, which provide further configuration options.

# Get the additional parameters for the prompt
parameters = prompt.get_parameters()
print("Prompt parameters:", parameters)

This process enables you to effectively use prompts created in the RagaAI Playground through the Python SDK.

Last updated