Python: 错误: 通过 Python Redis 连接器进行向量搜索 (FT.SEARCH) 失败

作者: daric93创建于 2026年4月20日更新于 2026年9月30日
标签bugpythonstale
  1. Install Python SK per Python/DEV_SETUP.md:
bash
cd Python && make install-sk PYTHON_VERSION=3.12
  1. Start Redis Stack on port 6379:
bash
podman run -d -p 6379:6379 Docker.io/Redis/Redis-stack:latest
  1. From Python/, run the following script:
python
# repro_search.py
import asyncio
from dataclasses import dataclass, field
from typing import Annotated
from uuid import uuid4
from semantic_kernel.connectors.Redis import RedisHashsetCollection
from semantic_kernel.data.vector import VectorStoreField, vectorstoremodel


@vectorstoremodel
@dataclass
class MyModel:
    vector: Annotated[
        list[float] | None,
        VectorStoreField("vector", index_kind="hnsw", dimensions=3,
                         distance_function="cosine_similarity", type="float"),
    ] = None
    id: Annotated[str, VectorStoreField("key", type="str")] = field(
        default_factory=lambda: str(uuid4())
    )
    content: Annotated[str, VectorStoreField("data", type="str")] = "hello"


async def main():
    async with RedisHashsetCollection(
        record_type=MyModel,
        collection_name="repro_search",
        prefix_collection_name_to_key_names=True,
    ) as col:
        await col.ensure_collection_deleted()
        await col.ensure_collection_exists()
        await col.upsert([MyModel(id="1", content="test", vector=[0.1, 0.2, 0.3])])

        # This raises AttributeError: 'StorageType' object has no attribute 'index'
        results = await col.search(vector=[0.1, 0.2, 0.3], top=1)
        async for r in results.results:
            print(r.record.id, r.record.content)
        await col.ensure_collection_deleted()


asyncio.run(main())

内容来源: microsoft/semantic-kernel