Ir para o conteúdo

Lição 20 · Tracing and LLM Apps

Version Prompts in the Prompt Registry

Store prompt templates in the MLflow Prompt Registry, create versions, use aliases to release and roll back, and evaluate a prompt change before shipping.

A prompt is part of your program. A one-word edit can change how your app behaves. The Prompt Registry stores prompt templates with versions, so you know which text ran and can switch back.

Register a prompt

Python
import mlflow

prompt = mlflow.genai.register_prompt(
    name="support-answer",
    template=(
        "Answer the question using only the context.\n"
        "Context: {{ context }}\n"
        "Question: {{ question }}\n"
        "If the context does not contain the answer, say you do not know."
    ),
    commit_message="First version with a rule for missing information",
)
print(prompt.version)

Calling register_prompt again with the same name creates a new version. Versions are immutable. Write a commit message that explains why you changed the text.

Variables use double braces, such as {{ question }}. Templates that use {% %} control flow are treated as Jinja2 templates. If a framework expects single braces, the docs provide to_single_brace_format().

Load and use

Python
pinned = mlflow.genai.load_prompt("prompts:/support-answer/2")
released = mlflow.genai.load_prompt("prompts:/support-answer@production")

text = released.format(context=docs, question=user_question)
URI Loads
prompts:/support-answer/2 Exactly version 2
prompts:/support-answer@production The version that the alias points to
prompts:/support-answer@latest The newest version (a reserved alias)

Avoid @latest in production, because any new version would go live at once.

Aliases and caching

Point an alias at a version with mlflow.genai.set_prompt_alias. To roll back, move the alias to an older version. MLflow caches loaded prompts. As documented, loads by version are cached without expiry, while loads by alias are cached for 60 seconds by default, so an alias change can take a moment to appear. You can change this per call or with environment variables.

How templating works

This stand-in fills variables and refuses to render when one is missing, which catches bugs early.

Python Stand-in: version, alias and render a prompt

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Test before you move the alias

Treat a prompt change like a code change:

  1. Register the new version.
  2. Evaluate both versions on the same test set.
  3. Compare scores, read failures, and check length, cost and latency.
  4. Move the alias only if the new version is better where it matters, with no serious losses elsewhere.
  5. Watch live feedback and be ready to move back.

Make your evaluation predict_fn load a specific version, not an alias. Then the result stays tied to the exact text you tested.

Prompt safety and teamwork

Do not store secrets or real customer data inside templates, because anyone with read access can see them. Also remember that user text placed into a variable can contain instructions meant to override your prompt (prompt injection). Templating does not protect against that. See prompt engineering.

A registry also helps teams. A product writer can propose a new version, while engineers keep control of the alias that production uses. Record the model settings that go with a prompt too. The register function accepts a model_config argument for this.

When a metric changes, you want to know which prompt caused it. Record the prompt name and version with each evaluation run and, where possible, with each trace:

Python
prompt = mlflow.genai.load_prompt("prompts:/support-answer/2")

with mlflow.start_run(run_name="eval-prompt-v2"):
    mlflow.log_params({"prompt_name": "support-answer", "prompt_version": 2})
    mlflow.genai.evaluate(data=data, predict_fn=predict_v2, scorers=scorers)

The names predict_v2 and scorers stand for your own function and list. With this record, a table of evaluation runs shows each version next to its scores.

Chat-style prompts and settings

Many applications separate a system instruction from the user message. A registry entry can hold more than one plain string, depending on the prompt type that your version supports, so read the documentation before you design the structure. Whatever the format, remember that the behavior depends on the text and the model and the settings. Changing the model while keeping the prompt can change results just as much as editing the text.

Writing commit messages

A good commit message explains the reason:

Weak Better
"update" "Add rule: say 'I do not know' when context is empty"
"fix tone" "Remove jokes after complaints about refunds"

Months later, the message tells you why a line exists. Without it, someone may delete a rule that fixed a real problem.

One change at a time

If you change the wording, the examples and the format together, and scores move, you do not know which change did it. Make small changes, evaluate each, and keep notes. Also test old failure cases again. A new instruction often fixes one problem and quietly causes another. Your evaluation set is the safety net that catches these side effects.

Before you release a prompt

Run through a short checklist. Does the new version render with all variables? Does it behave well when the context is empty? Did the cases that failed before now pass, and did earlier passing cases stay passing? Do you know which version you will return to if something goes wrong? Is the change described in the commit message? If you can answer yes to each question, move the alias, and then watch the first real traffic closely.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What happens when you register an existing prompt name again?

    Versions are immutable, so a change creates the next version.
  2. 2. Which URI loads the version that an alias named production points to?

    The at sign selects an alias.
  3. 3. Why avoid @latest in production?

    A controlled alias lets you decide when a new version is released.

Summary

  • Register prompt templates as immutable versions.
  • Load by version for repeatability, or by alias for controlled releases.
  • Caching can delay alias changes briefly.
  • Evaluate before you move an alias.

Next: Run a Tracking Server

Seu progresso fica salvo neste navegador. Não precisa de conta.