Skip to content

Commit 3d2bcfc

Browse files
authored
edit (#1486)
Edits to the memory-cache docs
1 parent ba6178d commit 3d2bcfc

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ user-guides/advanced/nemoguard-jailbreakdetect-deployment
6868
user-guides/advanced/kv-cache-reuse
6969
user-guides/advanced/safeguarding-ai-virtual-assistant-blueprint
7070
user-guides/advanced/tools-integration
71+
user-guides/advanced/model-memory-cache
7172
```
7273

7374
```{toctree}

docs/user-guides/advanced/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ Advanced
2222
nemoguard-contentsafety-deployment
2323
nemoguard-topiccontrol-deployment
2424
safeguarding-ai-virtual-assistant-blueprint
25-
model-memory-cache

docs/user-guides/advanced/model-memory-cache.md

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
# Memory Model Cache
44

5-
Guardrails supports an in-memory cache which avoids making LLM calls for repeated prompts. It stores user-prompts and the corresponding LLM response. Prior to making an LLM call, Guardrails first checks if the prompt matches one already in the cache. If the prompt is found in the cache, the stored response is returned from the cache, rather than prompting the LLM. This improves latency.
6-
In-memory caches are supported for all Nemoguard models ([Content-Safety](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-content-safety), [Topic-Control](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-topic-control), and [Jailbreak Detection](https://build.nvidia.com/nvidia/nemoguard-jailbreak-detect)). Each model can be configured independently.
7-
The cache uses exact-matching (after removing whitespace) on LLM prompts with a Least-Frequently-Used (LFU) algorithm for cache evictions.
8-
For observability, cache hits and misses are visible in OTEL telemetry, and stored in logs on a configurable cadence.
9-
To get started with caching, an example configuration is shown below. The rest of the page has a deep-dive into how the cache works, telemetry, and considerations when enabling caching in a horizontally-scalable service.
5+
Guardrails supports an in-memory cache that avoids making LLM calls for repeated prompts. The cache stores user prompts and their corresponding LLM responses. Prior to making an LLM call, Guardrails checks if the prompt already exists in the cache. If found, the stored response is returned instead of calling the LLM, improving latency.
6+
7+
In-memory caches are supported for all Nemoguard models: [Content-Safety](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-content-safety), [Topic-Control](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-topic-control), and [Jailbreak Detection](https://build.nvidia.com/nvidia/nemoguard-jailbreak-detect). Each model can be configured independently.
8+
9+
The cache uses exact matching (after removing whitespace) on LLM prompts with a Least-Frequently-Used (LFU) algorithm for cache evictions.
10+
11+
For observability, cache hits and misses are visible in OpenTelemetry (OTEL) telemetry and stored in logs on a configurable cadence.
12+
13+
To get started with caching, refer to the example configurations below. The rest of this page provides a deep dive into how the cache works, telemetry, and considerations when enabling caching in a horizontally scalable service.
14+
15+
---
1016

1117
## Example Configuration
1218

13-
Let's walk through an example of adding caching to a Content-Safety Guardrails application. The initial `config.yml` without caching is shown below.
14-
We are using a [Llama 3.3 70B-Instruct](https://build.nvidia.com/meta/llama-3_3-70b-instruct) main LLM to generate responses. Inputs are checked by [Content-Safety](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-content-safety), [Topic-Control](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-topic-control) and [Jailbreak detection](https://build.nvidia.com/nvidia/nemoguard-jailbreak-detect) models. The LLM response is also checked by the [Content-Safety](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-content-safety) model.
15-
The input rails check the user prompt before sending it to the Main LLM to generate a response. The output rail checks both the user input and Main LLM response to make sure the response is safe.
19+
The following example configurations show how to add caching to a Content-Safety Guardrails application.
20+
The examples use a [Llama 3.3 70B-Instruct](https://build.nvidia.com/meta/llama-3_3-70b-instruct) as the main LLM to generate responses. Inputs are checked by the [Content-Safety](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-content-safety), [Topic-Control](https://build.nvidia.com/nvidia/llama-3_1-nemoguard-8b-topic-control), and [Jailbreak Detection](https://build.nvidia.com/nvidia/nemoguard-jailbreak-detect) models. The LLM response is also checked by the Content-Safety model.
21+
The input rails check the user prompt before sending it to the main LLM to generate a response. The output rail checks both the user input and main LLM response to ensure the response is safe.
22+
23+
### Without Caching
24+
25+
The following `config.yml` file shows the initial configuration without caching.
1626

1727
```yaml
18-
# Content-Safety config.yml (without caching)
1928
models:
2029
- type: main
2130
engine: nim
@@ -51,10 +60,12 @@ rails:
5160
api_key_env_var: NVIDIA_API_KEY
5261
```
5362
54-
The yaml file below shows the same configuration, with caching enabled on the Content-Safety, Topic-Control, and Jailbreak detection Nemoguard NIMs. All three caches have a size of 10,000 records. The caches log their statistics every 60 seconds.
63+
### With Caching
64+
65+
The following configuration file shows the same configuration with caching enabled on the Content-Safety, Topic-Control, and Jailbreak Detection Nemoguard NIM microservices.
66+
All three caches have a size of 10,000 records and log their statistics every 60 seconds.
5567
5668
```yaml
57-
# Content-Safety config.yml (with caching)
5869
models:
5970
- type: main
6071
engine: nim
@@ -108,26 +119,39 @@ rails:
108119
api_key_env_var: NVIDIA_API_KEY
109120
```
110121
111-
## How does the Cache work?
122+
---
123+
124+
## How the Cache Works
125+
126+
When the cache is enabled, Guardrails checks whether a prompt was already sent to the LLM before making each call. This uses an exact-match lookup after removing whitespace.
112127
113-
When the cache is enabled, prior to each LLM call we first check to see if we already sent the same prompt to the LLM, and return the response if so. This uses an exact-match lookup, after removing whitespace.
114-
If there's a cache hit (i.e. the same prompt was sent to the same LLM earlier and the response was stored in the cache), then the response can be returned without calling the LLM.
115-
If there's a cache miss (i.e. we don't have a stored LLM response for this prompt in the cache), then the LLM is called as usual. When the response is received, this is stored in the cache.
128+
If there is a cache hit (that is, the same prompt was sent to the same LLM earlier and the response was stored in the cache), the response is returned without calling the LLM.
116129
117-
For security reasons, user prompts are not stored directly. After removing whitespace, the user-prompt is hashed using SHA256 and then used as a cache key.
130+
If there is a cache miss (that is, there is no stored LLM response for this prompt in the cache), the LLM is called as usual. When the response is received, it is stored in the cache.
131+
132+
For security reasons, user prompts are not stored directly. After removing whitespace, the user prompt is hashed using SHA256 and then used as a cache key.
118133
119134
If a new cache record needs to be added and the cache already has `maxsize` entries, the Least-Frequently Used (LFU) algorithm is used to decide which cache record to evict.
120135
The LFU algorithm ensures that the most frequently accessed cache entries remain in the cache, improving the probability of a cache hit.
121136

122-
## Telemetry and logging
137+
---
123138

124-
Guardrails supports OTEL telemetry to trace client requests through Guardrails and any calls to LLMs or APIs. The cache operation is reflected in these traces, with cache hits having a far shorter duration and no LLM call and cache misses having an LLM call. This OTEL telemetry is a good fit for operational dashboards.
125-
The cache statistics are also logged on a configurable cadence if `cache.stats.enabled` is set to `true`. Every `log_interval` seconds, the cache statistics are logged with the format below.
126-
The most important metric below is the "Hit Rate", which is the proportion of LLM calls returned from the cache. If this value remains low, the exact-match may not be a good fit for your usecase.
127-
These statistics accumulate for the time Guardrails is running.
139+
## Telemetry and Logging
128140

141+
Guardrails supports OTEL telemetry to trace client requests through Guardrails and any calls to LLMs or APIs. The cache operation is reflected in these traces:
129142

130-
```
143+
- **Cache hits** have a far shorter duration with no LLM call
144+
- **Cache misses** include an LLM call
145+
146+
This OTEL telemetry is suited for operational dashboards.
147+
148+
The cache statistics are also logged on a configurable cadence if `cache.stats.enabled` is set to `true`. Every `log_interval` seconds, the cache statistics are logged with the format shown below.
149+
150+
The most important metric is the *Hit Rate*, which represents the proportion of LLM calls returned from the cache. If this value remains low, the exact-match approach might not be a good fit for your use case.
151+
152+
These statistics accumulate while Guardrails is running.
153+
154+
```text
131155
"LFU Cache Statistics - "
132156
"Size: 0.23453 | "
133157
"Hits: 20 | "
@@ -138,20 +162,25 @@ These statistics accumulate for the time Guardrails is running.
138162
"Updates: 0"
139163
```
140164

141-
These metrics are detailed below:
165+
The following list describes the metrics included in the cache statistics:
166+
167+
- **Size**: The number of LLM calls stored in the cache.
168+
- **Hits**: The number of cache hits.
169+
- **Misses**: The number of cache misses.
170+
- **Hit Rate**: The proportion of calls returned from the cache. This is a float between 1.0 (all calls returned from the cache) and 0.0 (all calls sent to the LLM).
171+
- **Evictions**: The number of cache evictions.
172+
- **Puts**: The number of new cache records stored.
173+
- **Updates**: The number of existing cache records updated.
174+
175+
---
176+
177+
## Horizontal Scaling and Caching
142178

143-
* Size: The number of LLM calls stored in the cache.
144-
* Hits: The number of cache hits.
145-
* Misses: The number of cache misses.
146-
* Hit Rate: The proportion of calls returned from the cache. This is a float between 1.0 (all calls returned from cache) and 0.0 (all calls sent to LLM)
147-
* Evictions: Number of cache evictions.
148-
* Puts: Number of new cache records stored.
149-
* Updates: Number of existing cache records updated.
179+
This cache is implemented in-memory on each Guardrails node. When operating as a horizontally-scaled backend service, multiple Guardrails nodes run behind an API Gateway and load balancer to distribute traffic and meet availability and performance targets.
150180

181+
The current cache implementation maintains a separate cache on each node without sharing cache entries between nodes. For a cache hit to occur, the following conditions must be met:
151182

152-
## Horizontal scaling and caching
183+
1. The request must have been previously sent and stored in a cache.
184+
2. The load balancer must direct the subsequent request to the same node.
153185

154-
This cache is implemented in-memory on each Guardrails node. When operating as a horizontally-scaled backend-service, there are many Guardrails nodes running behind an API Gateway and load-balancer to distribute traffic and meet availability and performance targets.
155-
The current cache implementation has a separate cache on each node, with no sharing of cache entries between nodes.
156-
Because the load balancer spreads traffic over all Guardrails nodes, requests have to both be stored in cache, with the load balancer directing the same request to the same node.
157-
In practice, frequently-requested user prompts will likely be spread over Guardrails nodes by the load balancer, so the performance impact may be less significant.
186+
In practice, the load balancer spreads traffic across all Guardrails nodes, distributing frequently-requested user prompts across multiple nodes. This reduces cache hit rates in horizontally-scaled deployments compared to single-node deployments.

0 commit comments

Comments
 (0)