mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-03 18:00:36 +00:00
changes according the the comments
This commit is contained in:
parent
3c672f4293
commit
1abb78bd94
9 changed files with 203 additions and 57 deletions
|
|
@ -4,11 +4,8 @@
|
|||
# This source code is licensed under the terms described in the LICENSE file in
|
||||
# the root directory of this source tree.
|
||||
|
||||
from llama_stack.providers.utils.vector_io.vector_utils import (
|
||||
generate_chunk_id,
|
||||
sanitize_metadata_for_attributes,
|
||||
)
|
||||
from llama_stack_api import Chunk, ChunkMetadata
|
||||
from llama_stack.providers.utils.vector_io.vector_utils import generate_chunk_id
|
||||
from llama_stack_api import Chunk, ChunkMetadata, VectorStoreFileObject
|
||||
|
||||
# This test is a unit test for the chunk_utils.py helpers. This should only contain
|
||||
# tests which are specific to this file. More general (API-level) tests should be placed in
|
||||
|
|
@ -83,25 +80,75 @@ def test_chunk_serialization():
|
|||
assert "chunk_id" in serialized_chunk
|
||||
|
||||
|
||||
def test_sanitize_metadata_for_attributes():
|
||||
"""Test sanitization of metadata for VectorStoreSearchResponse.attributes."""
|
||||
# metadata with lists should be converted to strings
|
||||
metadata = {
|
||||
"tags": ["transformers", "h100-compatible", "region:us"],
|
||||
"model_name": "granite-3.3-8b",
|
||||
"score": 0.95,
|
||||
"active": True,
|
||||
"count": 42,
|
||||
"nested": {"key": "value"}, # Should be filtered out
|
||||
}
|
||||
result = sanitize_metadata_for_attributes(metadata)
|
||||
def test_vector_store_file_object_attributes_validation():
|
||||
"""Test VectorStoreFileObject validates and sanitizes attributes at input boundary."""
|
||||
# Test with metadata containing lists, nested dicts, and primitives
|
||||
from llama_stack_api.vector_io import VectorStoreChunkingStrategyAuto
|
||||
|
||||
file_obj = VectorStoreFileObject(
|
||||
id="file-123",
|
||||
attributes={
|
||||
"tags": ["transformers", "h100-compatible", "region:us"], # List -> string
|
||||
"model_name": "granite-3.3-8b", # String preserved
|
||||
"score": 0.95, # Float preserved
|
||||
"active": True, # Bool preserved
|
||||
"count": 42, # Int -> float
|
||||
"nested": {"key": "value"}, # Dict filtered out
|
||||
},
|
||||
chunking_strategy=VectorStoreChunkingStrategyAuto(),
|
||||
created_at=1234567890,
|
||||
status="completed",
|
||||
vector_store_id="vs-123",
|
||||
)
|
||||
|
||||
# Lists converted to comma-separated strings
|
||||
assert result["tags"] == "transformers, h100-compatible, region:us"
|
||||
assert file_obj.attributes["tags"] == "transformers, h100-compatible, region:us"
|
||||
# Primitives preserved
|
||||
assert result["model_name"] == "granite-3.3-8b"
|
||||
assert result["score"] == 0.95
|
||||
assert result["active"] is True
|
||||
assert result["count"] == 42.0 # int -> float
|
||||
assert file_obj.attributes["model_name"] == "granite-3.3-8b"
|
||||
assert file_obj.attributes["score"] == 0.95
|
||||
assert file_obj.attributes["active"] is True
|
||||
assert file_obj.attributes["count"] == 42.0 # int -> float
|
||||
# Complex types filtered out
|
||||
assert "nested" not in result
|
||||
assert "nested" not in file_obj.attributes
|
||||
|
||||
|
||||
def test_vector_store_file_object_attributes_constraints():
|
||||
"""Test VectorStoreFileObject enforces OpenAPI constraints on attributes."""
|
||||
from llama_stack_api.vector_io import VectorStoreChunkingStrategyAuto
|
||||
|
||||
# Test max 16 properties
|
||||
many_attrs = {f"key{i}": f"value{i}" for i in range(20)}
|
||||
file_obj = VectorStoreFileObject(
|
||||
id="file-123",
|
||||
attributes=many_attrs,
|
||||
chunking_strategy=VectorStoreChunkingStrategyAuto(),
|
||||
created_at=1234567890,
|
||||
status="completed",
|
||||
vector_store_id="vs-123",
|
||||
)
|
||||
assert len(file_obj.attributes) == 16 # Max 16 properties
|
||||
|
||||
# Test max 64 char keys are filtered
|
||||
long_key_attrs = {"a" * 65: "value", "valid_key": "value"}
|
||||
file_obj = VectorStoreFileObject(
|
||||
id="file-124",
|
||||
attributes=long_key_attrs,
|
||||
chunking_strategy=VectorStoreChunkingStrategyAuto(),
|
||||
created_at=1234567890,
|
||||
status="completed",
|
||||
vector_store_id="vs-123",
|
||||
)
|
||||
assert "a" * 65 not in file_obj.attributes
|
||||
assert "valid_key" in file_obj.attributes
|
||||
|
||||
# Test max 512 char string values are truncated
|
||||
long_value_attrs = {"key": "x" * 600}
|
||||
file_obj = VectorStoreFileObject(
|
||||
id="file-125",
|
||||
attributes=long_value_attrs,
|
||||
chunking_strategy=VectorStoreChunkingStrategyAuto(),
|
||||
created_at=1234567890,
|
||||
status="completed",
|
||||
vector_store_id="vs-123",
|
||||
)
|
||||
assert len(file_obj.attributes["key"]) == 512
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue