找回密码
 立即注册
首页 业界区 业界 如何通过Python SDK在Collection中进行相似性检索 ...

如何通过Python SDK在Collection中进行相似性检索

敖可 4 小时前
本文介绍如何通过Python SDK在Collection中按分组进行相似性检索。
前提条件


  • 已创建Cluster
  • 已获得API-KEY
  • 已安装最新版SDK
接口定义

Python示例:
  1. Collection.query_group_by(
  2.         self,
  3.         vector: Optional[Union[List[Union[int, float]], np.ndarray]] = None,
  4.         *,
  5.         group_by_field: str,
  6.         group_count: int = 10,
  7.         group_topk: int = 10,
  8.         id: Optional[str] = None,
  9.         filter: Optional[str] = None,
  10.         include_vector: bool = False,
  11.         partition: Optional[str] = None,
  12.         output_fields: Optional[List[str]] = None,
  13.         sparse_vector: Optional[Dict[int, float]] = None,
  14.         async_req: bool = False,
  15.     ) -> DashVectorResponse:
复制代码
使用示例

说明
需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
Python示例:
  1. import dashvector
  2. import numpy as np
  3. client = dashvector.Client(
  4.     api_key='YOUR_API_KEY',
  5.     endpoint='YOUR_CLUSTER_ENDPOINT'
  6. )
  7. ret = client.create(
  8.     name='group_by_demo',
  9.     dimension=4,
  10.     fields_schema={'document_id': str, 'chunk_id': int}
  11. )
  12. assert ret
  13. collection = client.get(name='group_by_demo')
  14. ret = collection.insert([
  15.     ('1', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 1, 'content': 'xxxA'}),
  16.     ('2', np.random.rand(4), {'document_id': 'paper-01', 'chunk_id': 2, 'content': 'xxxB'}),
  17.     ('3', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 1, 'content': 'xxxC'}),
  18.     ('4', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 2, 'content': 'xxxD'}),
  19.     ('5', np.random.rand(4), {'document_id': 'paper-02', 'chunk_id': 3, 'content': 'xxxE'}),
  20.     ('6', np.random.rand(4), {'document_id': 'paper-03', 'chunk_id': 1, 'content': 'xxxF'}),
  21. ])
  22. assert ret
复制代码
根据向量进行分组相似性检索

Python示例:
  1. ret = collection.query_group_by(
  2.     vector=[0.1, 0.2, 0.3, 0.4],
  3.     group_by_field='document_id',  # 按document_id字段的值分组
  4.     group_count=2,  # 返回2个分组
  5.     group_topk=2,   # 每个分组最多返回2个doc
  6. )
  7. # 判断是否成功
  8. if ret:
  9.     print('query_group_by success')
  10.     print(len(ret))
  11.     print('------------------------')
  12.     for group in ret:
  13.         print('group key:', group.group_id)
  14.         for doc in group.docs:
  15.             prefix = ' -'
  16.             print(prefix, doc)
复制代码
参考输出如下
  1. query_group_by success
  2. 4
  3. ------------------------
  4. group key: paper-01
  5. - {"id": "2", "fields": {"document_id": "paper-01", "chunk_id": 2, "content": "xxxB"}, "score": 0.6807}
  6. - {"id": "1", "fields": {"document_id": "paper-01", "chunk_id": 1, "content": "xxxA"}, "score": 0.4289}
  7. group key: paper-02
  8. - {"id": "3", "fields": {"document_id": "paper-02", "chunk_id": 1, "content": "xxxC"}, "score": 0.6553}
  9. - {"id": "5", "fields": {"document_id": "paper-02", "chunk_id": 3, "content": "xxxE"}, "score": 0.4401}
复制代码
根据主键对应的向量进行分组相似性检索

Python示例:
  1. ret = collection.query_group_by(
  2.     id='1',
  3.     group_by_field='name',
  4. )
  5. # 判断query接口是否成功
  6. if ret:
  7.     print('query_group_by success')
  8.     print(len(ret))
  9.     for group in ret:
  10.         print('group:', group.group_id)
  11.         for doc in group.docs:
  12.             print(doc)
  13.             print(doc.id)
  14.             print(doc.vector)
  15.             print(doc.fields)
复制代码
带过滤条件的分组相似性检索

Python示例:
  1. # 根据向量或者主键进行分组相似性检索 + 条件过滤
  2. ret = collection.query_group_by(
  3.     vector=[0.1, 0.2, 0.3, 0.4],   # 向量检索,也可设置主键检索
  4.     group_by_field='name',
  5.     filter='age > 18',             # 条件过滤,仅对age > 18的Doc进行相似性检索
  6.     output_fields=['name', 'age'], # 仅返回name、age这2个Field
  7.     include_vector=True
  8. )
复制代码
带有Sparse Vector的分组向量检索

Python示例:
  1. # 根据向量进行分组相似性检索 + 稀疏向量
  2. ret = collection.query_group_by(
  3.     vector=[0.1, 0.2, 0.3, 0.4],   # 向量检索
  4.     sparse_vector={1: 0.3, 20: 0.7},
  5.     group_by_field='name',
  6. )
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册