当前位置: 代码网 > it编程>前端脚本>Python > 一文详解如何使用Python SDK在Collection中进行相似性检索

一文详解如何使用Python SDK在Collection中进行相似性检索

2025年10月16日 Python 我要评论
前提条件已创建cluster已获得api-key已安装最新版sdk接口定义python示例:collection.query_group_by( self, vector:

前提条件

已创建cluster

已获得api-key

已安装最新版sdk

接口定义

python示例:

collection.query_group_by(
        self,
        vector: optional[union[list[union[int, float]], np.ndarray]] = none,
        *,
        group_by_field: str,
        group_count: int = 10,
        group_topk: int = 10,
        id: optional[str] = none,
        filter: optional[str] = none,
        include_vector: bool = false,
        partition: optional[str] = none,
        output_fields: optional[list[str]] = none,
        sparse_vector: optional[dict[int, float]] = none,
        async_req: bool = false,
    ) -> dashvectorresponse:

使用示例

说明

需要使用您的api-key替换示例中的your_api_key、您的cluster endpoint替换示例中的your_cluster_endpoint,代码才能正常运行。

python示例:

import dashvector
import numpy as np

client = dashvector.client(
    api_key='your_api_key',
    endpoint='your_cluster_endpoint'
)
ret = client.create(
    name='group_by_demo',
    dimension=4,
    fields_schema={'document_id': str, 'chunk_id': int}
)
assert ret

collection = client.get(name='group_by_demo')

ret = collection.insert([
    ('1', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 1, 'content': 'xxxa'}),
    ('2', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 2, 'content': 'xxxb'}),
    ('3', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 1, 'content': 'xxxc'}),
    ('4', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 2, 'content': 'xxxd'}),
    ('5', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 3, 'content': 'xxxe'}),
    ('6', np.random.rand(4), {'document_id': 'paper-03', 'chunk_id': 1, 'content': 'xxxf'}),
])
assert ret

根据向量进行分组相似性检索

python示例:

ret = collection.query_group_by(
    vector=[0.1, 0.2, 0.3, 0.4],
    group_by_field='document_id',  # 按document_id字段的值分组
    group_count=2,  # 返回2个分组
    group_topk=2,   # 每个分组最多返回2个doc
)
# 判断是否成功
if ret:
    print('query_group_by success')
    print(len(ret))
    print('------------------------')
    for group in ret:
        print('group key:', group.group_id)
        for doc in group.docs:
            prefix = ' -'
            print(prefix, doc)

参考输出如下

query_group_by success
4
------------------------
group key: paper-01
 - {"id": "2", "fields": {"document_id": "paper-01", "chunk_id": 2, "content": "xxxb"}, "score": 0.6807}
 - {"id": "1", "fields": {"document_id": "paper-01", "chunk_id": 1, "content": "xxxa"}, "score": 0.4289}
group key: paper-02
 - {"id": "3", "fields": {"document_id": "paper-02", "chunk_id": 1, "content": "xxxc"}, "score": 0.6553}
 - {"id": "5", "fields": {"document_id": "paper-02", "chunk_id": 3, "content": "xxxe"}, "score": 0.4401}

根据主键对应的向量进行分组相似性检索

python示例:

ret = collection.query_group_by(
    id='1',
    group_by_field='name',
)
# 判断query接口是否成功
if ret:
    print('query_group_by success')
    print(len(ret))
    for group in ret:
        print('group:', group.group_id)
        for doc in group.docs:
            print(doc)
            print(doc.id)
            print(doc.vector)
            print(doc.fields)

带过滤条件的分组相似性检索

python示例:

# 根据向量或者主键进行分组相似性检索 + 条件过滤
ret = collection.query_group_by(
    vector=[0.1, 0.2, 0.3, 0.4],   # 向量检索,也可设置主键检索
    group_by_field='name',
    filter='age > 18',             # 条件过滤,仅对age > 18的doc进行相似性检索
    output_fields=['name', 'age'], # 仅返回name、age这2个field
    include_vector=true
)

带有sparse vector的分组向量检索

python示例:

# 根据向量进行分组相似性检索 + 稀疏向量
ret = collection.query_group_by(
    vector=[0.1, 0.2, 0.3, 0.4],   # 向量检索
    sparse_vector={1: 0.3, 20: 0.7},
    group_by_field='name',
)

到此这篇关于一文详解如何使用python sdk在collection中进行相似性检索的文章就介绍到这了,更多相关python相似性检索内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com