Managing prompts 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:

1import opik
2
3# Prompt text stored in a variable
4PROMPT_TEXT = "Write a summary of the following text: {{text}}"
5
6# Create a prompt
7prompt = opik.Prompt(
8 name="prompt-summary",
9 prompt=PROMPT_TEXT,
10)
11
12# Print the prompt text
13print(prompt.prompt)
14
15# Build the prompt
16print(prompt.format(text="Hello, world!"))

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

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.

Built with