Prompt management

Opik provides a prompt library that you can use to manage your prompts. Storing prompts in a library allows you to version them, reuse them across projects, and manage them in a central location.

Using a prompt library does not mean you can’t store your prompt in code, we have designed the prompt library to be work seamlessly with your existing prompt files while providing the benefits of a central prompt library.

Managing prompts stored in code

The recommend way to create and manage prompts is using The Prompt object. This will allow you to continue versioning your prompts in code while also getting the benefit of having prompt versions managed in the Opik platform so you can more easily keep track of your progress.

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 metadata={"environment": "production"}
11)
12
13# Print the prompt text
14print(prompt.prompt)
15
16# Build the prompt
17print(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.

Using the low level SDK

If you would rather keep prompts in the Opik platform and manually update / download them, you can use the low-level Python SDK to manage you prompts.

Creating prompts

You can create a new prompt in the library using both the SDK and the UI:

1import opik
2
3opik.configure()
4client = opik.Opik()
5
6# Create a new prompt
7prompt = client.create_prompt(name="prompt-summary", prompt="Write a summary of the following text: {{text}}", metadata={"environment": "development"})

Downloading your prompts

Once a prompt is created in the library, you can download it in code using the Opik.get_prompt method:

1import opik
2
3opik.configure()
4client = opik.Opik()
5
6# Get a dataset
7dataset = client.get_or_create_dataset("test_dataset")
8
9# Get the prompt
10prompt = client.get_prompt(name="prompt-summary")
11
12# Create the prompt message
13prompt.format(text="Hello, world!")

If you are not using the SDK, you can download a prompt by using the REST API.

Viewing prompt history (all versions)

You can fetch the complete version history for a prompt by its exact name using get_prompt_history:

1import opik
2
3client = opik.Opik()
4
5history = client.get_prompt_history(name="prompt-summary")
6for version in history:
7 print(version.commit, version.prompt)

This returns a list of Prompt objects (each representing a specific version) for the given prompt name.

Searching prompts

To discover prompts by name substring and/or filters, use search_prompts. Filters are written in Opik Query Language (OQL):

1import opik
2
3client = opik.Opik()
4
5# Search by name substring only
6latest_versions = client.search_prompts(
7 filter_string='name contains "summary"'
8)
9
10# Search by name substring and tags filter
11filtered = client.search_prompts(
12 filter_string='name contains "summary" AND tags contains "alpha" AND tags contains "beta"',
13)
14
15for prompt in filtered:
16 print(prompt.name, prompt.commit, prompt.prompt)

The filter_string parameter uses Opik Query Language (OQL) with the format: "<COLUMN> <OPERATOR> <VALUE> [AND <COLUMN> <OPERATOR> <VALUE>]*"

Supported columns for prompts:

ColumnTypeOperators
idString=, !=, contains, not_contains, starts_with, ends_with, >, <
nameString=, !=, contains, not_contains, starts_with, ends_with, >, <
created_byString=, !=, contains, not_contains, starts_with, ends_with, >, <
tagsListcontains

Examples:

  • tags contains "production" - Filter by tag
  • name contains "summary" - Filter by name substring
  • created_by = "user@example.com" - Filter by creator
  • tags contains "alpha" AND tags contains "beta" - Multiple tag filtering

search_prompts returns the latest version for each matching prompt.

Linking prompts to Experiments

Experiments allow you to evaluate the performance of your LLM application on a set of examples. When evaluating different prompts, it can be useful to link the evaluation to a specific prompt version. This can be achieved by passing the prompt parameter when creating an Experiment:

1import opik
2from opik.evaluation import evaluate
3from opik.evaluation.metrics import Hallucination
4
5opik.configure()
6client = opik.Opik()
7
8# Get a dataset
9dataset = client.get_or_create_dataset("test_dataset")
10
11# Create a prompt
12prompt = opik.Prompt(name="My prompt", prompt="...")
13
14# Create an evaluation task
15def evaluation_task(dataset_item):
16 return {"output": "llm_response"}
17
18# Run the evaluation
19evaluation = evaluate(
20 experiment_name="My experiment",
21 dataset=dataset,
22 task=evaluation_task,
23 prompt=prompt,
24)

The experiment will now be linked to the prompt allowing you to view all experiments that use a specific prompt: