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:
- Prompts stored in code
- Prompts stored in a file
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!"))
import opik
# Read the prompt from a file
with open("prompt.txt", "r") as f:
prompt_text = f.read()
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:
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.