Text Extraction
Extract the text that guardrails scan from a chat completion request or response, and.
write guardrails’ changes back.
Input guardrails scan the latest user message: what the user just said. They do not scan the LLMClient’s own system prompt, which would make e.g. a prompt injection guardrail trigger on the operator’s instructions, nor the earlier turns of the conversation, which were scanned when they were new.
Output guardrails scan the reply’s message content, and refusal.
A message’s content may be a string, or a list of parts, of which the text parts are
scanned. Each segment has a path, e.g. messages[3].content or
messages[3].content[0].text, so that redactions can be written back to the right place.
- smarter.apps.guardrail.services.text_extraction.extract_segments(payload, stage)[source]
Return the text segments that guardrails scan, for a payload and stage.
- Parameters:
- Return type:
list[TextSegment]- Returns:
The segments, in document order. Empty or non-text fields are omitted.
- smarter.apps.guardrail.services.text_extraction.latest_user_message_index(messages)[source]
Return the index of the latest message whose role is user, or
None.
- smarter.apps.guardrail.services.text_extraction.read_segment(payload, path)[source]
Return the text at
path, orNoneif it does not resolve to a string.