一文詳解如何使用Python SDK在Collection中進(jìn)行相似性檢索
前提條件
已創(chuàng)建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,代碼才能正常運(yùn)行。
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
根據(jù)向量進(jìn)行分組相似性檢索
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個(gè)分組
group_topk=2, # 每個(gè)分組最多返回2個(gè)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}
根據(jù)主鍵對(duì)應(yīng)的向量進(jìn)行分組相似性檢索
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示例:
# 根據(jù)向量或者主鍵進(jìn)行分組相似性檢索 + 條件過濾
ret = collection.query_group_by(
vector=[0.1, 0.2, 0.3, 0.4], # 向量檢索,也可設(shè)置主鍵檢索
group_by_field='name',
filter='age > 18', # 條件過濾,僅對(duì)age > 18的Doc進(jìn)行相似性檢索
output_fields=['name', 'age'], # 僅返回name、age這2個(gè)Field
include_vector=True
)
帶有Sparse Vector的分組向量檢索
Python示例:
# 根據(jù)向量進(jìn)行分組相似性檢索 + 稀疏向量
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',
)
到此這篇關(guān)于一文詳解如何使用Python SDK在Collection中進(jìn)行相似性檢索的文章就介紹到這了,更多相關(guān)Python相似性檢索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python開發(fā)一個(gè)現(xiàn)代化Markdown編輯器
在這個(gè)內(nèi)容為王的時(shí)代,Markdown已經(jīng)成為技術(shù)寫作的標(biāo)準(zhǔn)格式,從GitHub的README文件到技術(shù)博客,從API文檔到學(xué)術(shù)論文,Markdown以其簡(jiǎn)潔的語法和強(qiáng)大的表現(xiàn)力征服了無數(shù)開發(fā)者和內(nèi)容創(chuàng)作者,本文介紹了如何基于Python開發(fā)一個(gè)現(xiàn)代化Markdown編輯器,需要的朋友可以參考下2025-09-09
Python爬蟲Scrapy框架CrawlSpider原理及使用案例
這篇文章主要介紹了Python爬蟲Scrapy框架(CrawlSpider),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python實(shí)現(xiàn)的解析crontab配置文件代碼
這篇文章主要介紹了python實(shí)現(xiàn)的解析crontab配置文件代碼,也可以說是python版的crontab,代碼中包含大量注釋,需要的朋友可以參考下2014-06-06
Python scikit-learn 做線性回歸的示例代碼
本篇文章主要介紹了Python scikit-learn 做線性回歸的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
python中pygame針對(duì)游戲窗口的顯示方法實(shí)例分析(附源碼)
這篇文章主要介紹了python中pygame針對(duì)游戲窗口的顯示方法,以完整實(shí)例形式較為詳細(xì)的分析了pygame響應(yīng)鍵盤按鍵改變窗口顯示效果的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11
Python實(shí)戰(zhàn)之用tkinter庫(kù)做一個(gè)鼠標(biāo)模擬點(diǎn)擊器
這篇文章主要介紹了Python實(shí)戰(zhàn)之用tkinter庫(kù)做一個(gè)鼠標(biāo)模擬點(diǎn)擊器,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
對(duì)python:threading.Thread類的使用方法詳解
今天小編就為大家分享一篇對(duì)python:threading.Thread類的使用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01

