nemoguardrails.llm.cache.utils
Module Contents
Classes
Functions
Data
API
Bases: typing.TypedDict
llm_metadata
llm_stats
result
Bases: typing.TypedDict
metadata
stats
Bases: typing.TypedDict
model_name
provider_name
Bases: typing.TypedDict
completion_tokens
prompt_tokens
total_tokens
Create a normalized, hashed cache key from a prompt.
This function generates a deterministic cache key by normalizing the prompt and applying SHA-256 hashing. The normalization ensures that semantically equivalent prompts produce the same cache key.
Parameters:
prompt
The prompt to be cached. Can be:
- str: A single prompt string (for completion models)
- List[dict]: A list of message dictionaries for chat models (e.g., [{“type”: “user”, “content”: “Hello”}]) Note: render_task_prompt() returns Union[str, List[dict]]
normalize_whitespace
Whether to normalize whitespace characters. When True, collapses all whitespace sequences to single spaces and strips leading/trailing whitespace. Default: True
Returns: str
A SHA-256 hex digest string (64 characters) suitable for use as a cache key
Raises:
TypeError: If prompt is not a str or List[dict]
Examples: