一文詳解如何使用Python SDK在Collection中進行相似性檢索
前提條件
已創(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,代碼才能正常運行。
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ù)向量進行分組相似性檢索
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}
根據(jù)主鍵對應的向量進行分組相似性檢索
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ù)向量或者主鍵進行分組相似性檢索 + 條件過濾
ret = collection.query_group_by(
vector=[0.1, 0.2, 0.3, 0.4], # 向量檢索,也可設(shè)置主鍵檢索
group_by_field='name',
filter='age > 18', # 條件過濾,僅對age > 18的Doc進行相似性檢索
output_fields=['name', 'age'], # 僅返回name、age這2個Field
include_vector=True
)
帶有Sparse Vector的分組向量檢索
Python示例:
# 根據(jù)向量進行分組相似性檢索 + 稀疏向量
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中進行相似性檢索的文章就介紹到這了,更多相關(guān)Python相似性檢索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ubuntu 14.04+Django 1.7.1+Nginx+uwsgi部署教程
django+uwsgi的部署實在是太蛋疼了.網(wǎng)上已有的教程似乎有新版本的兼容問題。最后跑到uwsgi官網(wǎng)上找的教程終于跑通了.. 不過官網(wǎng)的教程似乎有引導教學性質(zhì),部署的時候就顯得很繞彎路,在這里記錄下來精簡內(nèi)容2014-11-11
python驗證公網(wǎng)ip與內(nèi)網(wǎng)ip的實現(xiàn)示例
本文主要介紹了python驗證公網(wǎng)ip與內(nèi)網(wǎng)ip的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Python中用altzone()方法處理時區(qū)的教程
這篇文章主要介紹了Python中用altzone()方法處理時區(qū)的教程,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05
Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動態(tài)圖)
這篇文章主要介紹了Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動態(tài)圖),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Python遞歸函數(shù) 二分查找算法實現(xiàn)解析
這篇文章主要介紹了Python遞歸函數(shù) 二分查找算法實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08

