Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mlflow/genai/prompts/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from functools import lru_cache
from typing import Any


Expand All @@ -8,5 +9,11 @@ def format_prompt(prompt: str, **values: Any) -> str:
# Escape backslashes in the replacement string to prevent re.sub from interpreting
# them as escape sequences (e.g. \u being treated as Unicode escape)
replacement = str(value).replace("\\", "\\\\")
prompt = re.sub(r"\{\{\s*" + key + r"\s*\}\}", replacement, prompt)
prompt = _compiled_key_pattern(key).sub(replacement, prompt)
return prompt


@lru_cache(maxsize=64)
def _compiled_key_pattern(key: str) -> re.Pattern:
# Compile and cache the regex pattern for a template variable key
return re.compile(r"\{\{\s*" + re.escape(key) + r"\s*\}\}")