Skip to main content

Managing prompts stored in code

If you already have prompts stored in code, you can use the the Prompt object in the SDK to sync these prompts with the library. This allows you to store the prompt text in your code while also having it versioned and stored in the library:

import opik

# Prompt text stored in a variable
PROMPT_TEXT = "Write a summary of the following text: {{text}}"

# Create a prompt
prompt = opik.Prompt(
name="prompt-summary",
prompt=PROMPT_TEXT,
)

# Print the prompt text
print(prompt.prompt)

# Build the prompt
print(prompt.format(text="Hello, world!"))

The prompt will now be stored in the library and versioned:

Prompt library versions

tip

The Prompt object will create a new prompt in the library if this prompt doesn't already exist, otherwise it will return the existing prompt.

This means you can safely run the above code multiple times without creating duplicate prompts.