Skip to content

Commit d89b958

Browse files
authored
Merge pull request #1035 from guardrails-ai/nichwch/docs
Add docs page on streaming fixes
2 parents 545422b + cbb051c commit d89b958

File tree

4 files changed

+74
-12
lines changed

4 files changed

+74
-12
lines changed

docs/concepts/streaming_fixes.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Handling Fix Results for Streaming in Guardrails
2+
3+
## Overview
4+
5+
This document describes how Guardrails handles fix results for streaming, addressing the challenges that arise from the new streaming architecture implemented to reduce latency and cost of validation.
6+
7+
## Fix Actions
8+
9+
Fix actions are a feature in Guardrails that allow you to specify an action to take when a particular validator fails. Some validators support a "FIX" on_fail action, which programmatically corrects faulty LLM output.
10+
11+
### Examples
12+
13+
1. Detect PII Validator:
14+
15+
```
16+
Input: John lives in San Francisco.
17+
Output: <PERSON> lives in <LOCATION>
18+
```
19+
20+
2. Lowercase Validator:
21+
```
22+
Input: JOHN lives IN san FRANCISCO.
23+
Output: john lives in san francisco.
24+
```
25+
26+
In non-streaming scenarios, fix results from one validator can be piped into subsequent validators.
27+
28+
## Challenges with Streaming
29+
30+
The new streaming architecture allows validators to specify how much context to accumulate before running validation. However, this poses challenges for fix actions:
31+
32+
1. Validators cannot run in sequence due to different chunk thresholds.
33+
2. Each validator accumulates chunks independently.
34+
3. Validators are unaware of fixes applied by other validators.
35+
36+
## Solution: Merging Algorithm
37+
38+
To address these challenges, Guardrails implements the following solution:
39+
40+
1. Wait for all validators to accumulate enough chunks to validate and output a fix value.
41+
2. Run a merging algorithm on the fixes from all validators.
42+
43+
### Merging Process
44+
45+
The merging algorithm combines fix outputs from multiple validators. For example:
46+
47+
```
48+
LLM Output: JOE is FUNNY and LIVES in NEW york
49+
PII Fix Output: <PERSON> is FUNNY and lives in <LOCATION>
50+
Lowercase Fix: joe is funny and lives in new york
51+
Merged Output: <PERSON> is funny and lives in <LOCATION>
52+
```
53+
54+
### Implementation Details
55+
56+
- The merging algorithm is a modified version of the `three-merge` package.
57+
- It uses Google's `diff-match-patch` algorithm under the hood.
58+
59+
## Limitations and Edge Cases
60+
61+
While the merging algorithm works well for most cases, there are some limitations:
62+
63+
- Edge cases may occur when replacement ranges overlap between multiple validators.
64+
- In rare instances, the merging algorithm might produce unexpected results.
65+
66+
## Reporting Issues
67+
68+
If you encounter any bugs related to stream fixes, please:
69+
70+
1. File an issue on the [Guardrails GitHub repository](https://github.com/guardrails-ai/guardrails).
71+
2. Mention @nichwch in the issue description.

docusaurus/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const sidebars = {
6363
"concepts/streaming",
6464
"concepts/async_streaming",
6565
"concepts/streaming_structured_data",
66+
"concepts/streaming_fixes",
6667
],
6768
},
6869
"concepts/parallelization",

guardrails/utils/docs_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ def get_chunks_from_text(
145145
raise ImportError(tiktoken_error)
146146
# FIXME is this the correct way to use tiktoken?
147147
atomic_chunks = tiktoken(text) # type: ignore
148+
elif chunk_strategy == "full":
149+
atomic_chunks = [text]
148150
else:
149151
raise ValueError(
150152
"chunk_strategy must be 'sentence', 'word', 'char', or 'token'."

guardrails/utils/validator_utils.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@
1313
from guardrails.constants import hub
1414
from guardrails.logger import logger
1515

16-
PROVENANCE_V1_PROMPT = """Instruction:
17-
As an Attribution Validator, you task is to verify whether the following contexts support the claim:
18-
19-
Claim:
20-
{}
21-
22-
Contexts:
23-
{}
24-
25-
Just respond with a "Yes" or "No" to indicate whether the given contexts support the claim.
26-
Response:"""
27-
2816

2917
def parse_rail_arguments(arg_tokens: List[str]) -> List[Any]:
3018
"""Legacy parsing logic for the Validator aruguments specified in a RAIL

0 commit comments

Comments
 (0)