mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-11 03:38:38 +00:00
chore(guardrails): drop docstrings added by the attachment scan
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
This commit is contained in:
parent
312c40678e
commit
84bc9db981
3 changed files with 0 additions and 32 deletions
|
|
@ -1,12 +1,3 @@
|
|||
"""
|
||||
Find the attachments in a raw request that the Bedrock guardrail has to scan or refuse.
|
||||
|
||||
ApplyGuardrail scans inline PNG and JPEG images of up to 4 MB. Every other attachment
|
||||
(documents, files, audio, video, and images sent as a remote URL, a file id, in another
|
||||
format or over the size limit) is reported as unscannable so the guardrail can block the
|
||||
request instead of letting the attachment reach the model unread.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
from collections.abc import Callable, Iterable, Mapping, Sequence
|
||||
|
|
@ -92,7 +83,6 @@ def find_request_attachments(
|
|||
latest_user_message_only: bool,
|
||||
scan_only_tool_results: bool = False,
|
||||
) -> RequestAttachments:
|
||||
"""List the scannable images, the document text and the unscannable attachments in the message content and tool results."""
|
||||
messages, classify, nested_tool_blocks = _messages_and_classifier(data, call_type)
|
||||
selected: Final = messages if not latest_user_message_only else _latest_user_message(messages)
|
||||
entries: Final = chain.from_iterable(_message_blocks(message, nested_tool_blocks) for message in selected)
|
||||
|
|
@ -119,7 +109,6 @@ def _classify_entry(entry: _Block, classify: _BlockClassifier) -> tuple[_Image |
|
|||
|
||||
|
||||
def _document_text(entry: _Block) -> str:
|
||||
"""Return the text a block carries as part of a document: a nested text block, or a document's title, context and text source."""
|
||||
block: Final = entry.block
|
||||
if entry.document_depth > 0 and block.get("type") == "text":
|
||||
text: Final = block.get("text")
|
||||
|
|
@ -343,7 +332,6 @@ def _sniffed_format(data: bytes) -> BedrockImageFormat | None:
|
|||
|
||||
|
||||
def _pixel_size(data: bytes, image_format: BedrockImageFormat) -> tuple[int, ...]:
|
||||
"""Width and height from the PNG IHDR chunk or the JPEG frame header, or () when the header is unreadable."""
|
||||
if image_format == "png":
|
||||
return (
|
||||
(int.from_bytes(data[16:20], "big"), int.from_bytes(data[20:24], "big")) if data[12:16] == b"IHDR" else ()
|
||||
|
|
@ -368,7 +356,6 @@ def _pixel_size(data: bytes, image_format: BedrockImageFormat) -> tuple[int, ...
|
|||
|
||||
|
||||
def _standard_base64(encoded: object) -> str:
|
||||
"""Return the payload as padded standard base64, accepting whitespace, missing padding and the URL-safe alphabet."""
|
||||
compact: Final = (
|
||||
"".join(encoded.split()).translate(_URL_SAFE_TO_STANDARD_BASE64) if isinstance(encoded, str) else ""
|
||||
)
|
||||
|
|
|
|||
|
|
@ -979,7 +979,6 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
|
|||
base_request: BedrockRequest,
|
||||
request_data: dict | None, # mutable-ok: proxy request body dict, read by the dynamic-params helper
|
||||
) -> tuple[dict, str | None]: # mutable-ok: ApplyGuardrail JSON request body
|
||||
"""Merge the request's dynamic ApplyGuardrail params into `base_request` and pick up its api_key."""
|
||||
bedrock_request_data: Final[dict] = dict(base_request) # mutable-ok: JSON request body
|
||||
api_key: str | None = None
|
||||
if request_data:
|
||||
|
|
@ -1005,7 +1004,6 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
|
|||
start_time: "datetime",
|
||||
allow_chunking: bool,
|
||||
) -> BedrockGuardrailResponse:
|
||||
"""Post prepared ApplyGuardrail content and log the call once, as a success or a failure."""
|
||||
credentials, aws_region_name = await run_aws_signing(
|
||||
self._load_credentials, bearer_token=bedrock_bearer_token(api_key)
|
||||
)
|
||||
|
|
@ -2542,19 +2540,6 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
|
|||
call_type: CallTypesLiteral,
|
||||
event_type: GuardrailEventHooks = GuardrailEventHooks.pre_call,
|
||||
) -> None:
|
||||
"""Scan the request's inline PNG/JPEG images and text documents, and block attachments it cannot scan.
|
||||
|
||||
Images go to ApplyGuardrail 20 per call. Text documents go through ``make_bedrock_api_request``
|
||||
as one user turn, and any intervention on them blocks the request, since their text cannot be
|
||||
rewritten with a mask.
|
||||
|
||||
Documents, files, audio, video, and images sent by URL, by file id or in another format block the
|
||||
request unless ``skip_unscannable_attachments`` is set. ``checks`` mode calls the text-only
|
||||
InvokeGuardrailChecks API, so there every image counts as unscannable too. A failed ApplyGuardrail
|
||||
call raises the same error the text scan of the same hook raises. A guardrail whose
|
||||
``apply_guardrail`` or ``make_bedrock_api_request`` is replaced, by a subclass or on the
|
||||
instance, skips this scan.
|
||||
"""
|
||||
if (
|
||||
getattr(self.apply_guardrail, "__func__", None) is not BedrockGuardrail.apply_guardrail
|
||||
or getattr(self.make_bedrock_api_request, "__func__", None) is not BedrockGuardrail.make_bedrock_api_request
|
||||
|
|
@ -2608,7 +2593,6 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
|
|||
request_data: dict, # mutable-ok: proxy request body dict, mutated by the logging helper
|
||||
event_type: GuardrailEventHooks,
|
||||
) -> None:
|
||||
"""Scan text documents like a user turn and block when the guardrail intervenes on them."""
|
||||
response: Final = await self.make_bedrock_api_request(
|
||||
source="INPUT",
|
||||
messages=[{"role": "user", "content": text} for text in document_texts],
|
||||
|
|
@ -2628,7 +2612,6 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM):
|
|||
request_data: dict, # mutable-ok: proxy request body dict, mutated by the logging helper
|
||||
event_type: GuardrailEventHooks,
|
||||
) -> HTTPException | ModifyResponseException:
|
||||
"""Log the refusal and build the block error for attachments ApplyGuardrail cannot scan."""
|
||||
reason: Final = (
|
||||
f"Bedrock guardrail cannot scan {len(unscannable)} attachment(s) "
|
||||
f"({', '.join(sorted(frozenset(unscannable)))}), so the request was blocked"
|
||||
|
|
|
|||
|
|
@ -48,8 +48,6 @@ GUARDRAIL_NAME: Final = "unified_llm_guardrails"
|
|||
|
||||
@runtime_checkable
|
||||
class RequestAttachmentScanner(Protocol):
|
||||
"""A guardrail that scans the raw request's attachments before its text is extracted."""
|
||||
|
||||
async def async_scan_request_attachments(
|
||||
self,
|
||||
data: dict, # mutable-ok: proxy request body dict, mutated by the logging helper
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue