Milvus 視角看重排序模型(Rerankers)

在信息檢索和生成式人工智能領域,重排序器是優化初始搜索結果順序的重要工具。重排序器與傳統的嵌入模型不同,它將查詢和文檔作為輸入,并直接返回相似度得分,而不是嵌入。該得分表示輸入查詢和文檔之間的相關性。

重排序器通常在第一階段檢索之后使用,通常通過向量近似最近鄰 (ANN) 技術完成。雖然 ANN 搜索能夠高效地獲取大量潛在相關的結果,但它們并不總是根據結果與查詢的實際語義接近程度來確定優先級。此時,重排序器會通過更深入的上下文分析來優化結果順序,通常會利用 BERT 或其他基于 Transformer 的高級機器學習模型。通過這種方式,重排序器可以顯著提高呈現給用戶的最終結果的準確性和相關性。

PyMilvus 模型庫集成了重排序功能,用于優化初始搜索返回結果的排序。從 Milvus 檢索到最近的嵌入后,您可以利用這些重排序工具來優化搜索結果,從而提高搜索結果的準確率。

Rerank FunctionAPI or Open-sourced
BGEOpen-sourced
Cross EncoderOpen-sourced
VoyageAPI
CohereAPI
Jina AIAPI

示例 1:使用 BGE rerank 函數根據查詢對文檔進行重新排序

在此示例中,我們演示了如何使用基于特定查詢的BGE 重新排序器對搜索結果進行重新排序。

要將重新排序器與PyMilvus 模型庫一起使用,首先安裝 PyMilvus 模型庫以及包含所有必要的重新排序實用程序的模型子包:

pip install pymilvus[model]
# or pip install "pymilvus[model]" for zsh.

要使用 BGE 重新排序器,首先導入BGERerankFunction類:

from pymilvus.model.reranker import BGERerankFunction

然后,創建一個BGERerankFunction重新排名的實例:

bge_rf = BGERerankFunction(model_name="BAAI/bge-reranker-v2-m3",  # Specify the model name. Defaults to `BAAI/bge-reranker-v2-m3`.device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)

要根據查詢對文檔重新排序,請使用以下代碼:

query = "What event in 1956 marked the official birth of artificial intelligence as a discipline?"documents = ["In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.","The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.","In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.","The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
]bge_rf(query, documents)

預期輸出類似于以下內容:

[RerankResult(text="The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.", score=0.9911615761470803, index=1),RerankResult(text="In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.", score=0.0326971950177779, index=0),RerankResult(text='The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.', score=0.006514905766152258, index=3),RerankResult(text='In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.', score=0.0042116724917325935, index=2)]

示例 2:使用重新排序器增強搜索結果的相關性

在本指南中,我們將探索如何利用search()PyMilvus 中的方法進行相似性搜索,以及如何使用重排序器增強搜索結果的相關性。我們的演示將使用以下數據集:

entities = [{'doc_id': 0, 'doc_vector': [-0.0372721,0.0101959,...,-0.114994], 'doc_text': "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence."}, {'doc_id': 1, 'doc_vector': [-0.00308882,-0.0219905,...,-0.00795811], 'doc_text': "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals."}, {'doc_id': 2, 'doc_vector': [0.00945078,0.00397605,...,-0.0286199], 'doc_text': 'In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.'}, {'doc_id': 3, 'doc_vector': [-0.0391119,-0.00880096,...,-0.0109257], 'doc_text': 'The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.'}
]

數據集組件

  • doc_id:每個文檔的唯一標識符。
  • doc_vector:表示文檔的向量嵌入。有關生成嵌入的指導,請參閱嵌入。
  • doc_text:文檔的文本內容。

準備工作

在啟動相似性搜索之前,您需要與 Milvus 建立連接,創建一個集合,并準備數據并將其插入到該集合中。以下代碼片段演示了這些準備步驟。

from pymilvus import MilvusClient, DataTypeclient = MilvusClient(uri="http://10.102.6.214:19530" # replace with your own Milvus server address
)client.drop_collection('test_collection')# define schemaschema = client.create_schema(auto_id=False, enabel_dynamic_field=True)schema.add_field(field_name="doc_id", datatype=DataType.INT64, is_primary=True, description="document id")
schema.add_field(field_name="doc_vector", datatype=DataType.FLOAT_VECTOR, dim=384, description="document vector")
schema.add_field(field_name="doc_text", datatype=DataType.VARCHAR, max_length=65535, description="document text")# define index paramsindex_params = client.prepare_index_params()index_params.add_index(field_name="doc_vector", index_type="IVF_FLAT", metric_type="IP", params={"nlist": 128})# create collectionclient.create_collection(collection_name="test_collection", schema=schema, index_params=index_params)# insert data into collectionclient.insert(collection_name="test_collection", data=entities)# Output:
# {'insert_count': 4, 'ids': [0, 1, 2, 3]}

數據插入后,使用該方法進行相似性搜索search

# search results based on our queryres = client.search(collection_name="test_collection",data=[[-0.045217834, 0.035171617, ..., -0.025117004]], # replace with your query vectorlimit=3,output_fields=["doc_id", "doc_text"]
)for i in res[0]:print(f'distance: {i["distance"]}')print(f'doc_text: {i["entity"]["doc_text"]}')

預期輸出類似于以下內容:

distance: 0.7235960960388184
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
distance: 0.6269873976707458
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
distance: 0.5340118408203125
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

使用重新排序器來增強搜索結果

然后,通過重新排序步驟來提高搜索結果的相關性。在本例中,我們使用CrossEncoderRerankFunction內置的 PyMilvus 對結果進行重新排序,以提高準確率。

# use reranker to rerank search resultsfrom pymilvus.model.reranker import CrossEncoderRerankFunctionce_rf = CrossEncoderRerankFunction(model_name="cross-encoder/ms-marco-MiniLM-L-6-v2",  # Specify the model name.device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)reranked_results = ce_rf(query='What event in 1956 marked the official birth of artificial intelligence as a discipline?',documents=["In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.","The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.","In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.","The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."],top_k=3
)# print the reranked results
for result in reranked_results:print(f'score: {result.score}')print(f'doc_text: {result.text}')

預期輸出類似于以下內容:

score: 6.250532627105713
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
score: -2.9546022415161133
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
score: -4.771512031555176
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/81345.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/81345.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/81345.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C語言:gcc 如何調用 Win32 打開文件對話框 ?

在 Windows 平臺上使用 gcc 調用原生 Win32 API 實現文件打開對話框是可行的,但需要直接使用 Win32 的 GetOpenFileName 函數(位于 commdlg.h 頭文件,依賴 comdlg32.lib 庫)。以下是完整實現步驟和代碼示例: 編寫 file…

計算機視覺與深度學習 | Python實現EMD-SSA-VMD-LSTM時間序列預測(完整源碼和數據)

EMD-SSA-VMD-LSTM混合模型 一、環境配置與依賴二、數據生成(示例數據)三、多級信號分解1. 經驗模態分解(EMD)2. 奇異譜分析(SSA)3. 變分模態分解(VMD) 四、數據預處理1. 歸一化處理2…

vue配置子路由,實現點擊左側菜單,內容區域顯示不同的內容

文章目錄 一、路由鏈路二、實現步驟準備二級路由下的.vue文件配置子路由聲明router-view標簽為菜單項 el-menu-item 設置index屬性,設置點擊后的路由路徑 三、參考資料 一、路由鏈路 二、實現步驟 準備二級路由下的.vue文件 配置子路由 router/index.js import {…

ModuleNotFoundError: No module named ‘SDToolbox‘

(py311) C:>python Python 3.11.11 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:34:19) [MSC v.1929 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. from SDToolbox import PostShock_eq Tracebac…

Hi3516DV500刷寫固件

hi3516DV500刷固件 1、硬件連接 2、軟件準備 3、刷固件步驟 一、硬件連接 特別注意的是,串口的接線順序 通過網線連接好筆記本和開發板后,需要確認一下網口水晶頭是否閃爍,以確認網絡物理是否連通 二、軟件資源準備 固件包準備 打開工具…

正則表達式r前綴使用指南

正則表達式中的 r:解鎖字符串轉義的魔法 正則表達式是處理字符串的強大工具,但它常常伴隨著轉義字符的復雜性。如果你曾因 \n、\t 或 \\ 的使用而困惑,那么這篇文章將為你揭開謎底,解釋為什么 r 是正則表達式中的「神奇武器」。本…

網絡攻防模擬:城市安全 “數字預演”

在當今數字化快速發展的時代,網絡安全和城市安全面臨著前所未有的挑戰。為有效應對這些挑戰,利用先進的技術搭建模擬演練平臺至關重要。圖撲軟件的 HT for Web 技術,為網絡攻防模擬與城市安全演練提供了全面且高效的解決方案。 三維場景搭建&…

AI模型開發全流程筆記

一、訓練數據準備階段 數據采集標準 格式要求:嚴格QA對形式(1問1答) 數量基準: 基礎量:500組QA對 優化量:800-1000組QA對 內容規范: 聚焦單一業務節點(如售后場景) …

1688 數據接口調用秘籍:高效獲取商品實時信息的開發指南

在電商行業競爭白熱化的當下,企業想要搶占市場先機,實時掌握商品信息至關重要。作為國內 B2B 電商巨頭,1688 平臺匯聚海量商品資源,通過高效調用其數據接口獲取商品實時信息,能為企業價格策略制定、庫存管理、競品分析…

milvus學習筆記

本文主要由AI生成,請注意自己查看源代碼校驗。 Milvus v2.4 系統架構概覽 Milvus 采用分布式微服務架構,將計算層(Proxy、QueryCoord、QueryNode、IndexCoord、DataCoord、DataNode 等)與存儲層(Pulsar、MinIO/S3、e…

使用教程:8x16模擬開關陣列可級聯XY腳雙向導通自動化接線

以下通過點亮LED進行基本使用流程演示,實際可以連接復雜外設(SPI、CAN、ADC等) 單模塊使用 RX、TX、5V和GND接到串口模塊;X5接5V;Y2接LED;LED-接GND 串口模塊插上電腦后,LED沒有亮;因為此時模…

HarmonyOS NEXT~鴻蒙應用上架指南:HarmonyOS應用發布全流程解析

HarmonyOS NEXT~鴻蒙應用上架指南:HarmonyOS應用發布全流程解析 引言 隨著華為鴻蒙操作系統(HarmonyOS)生態的快速發展,越來越多的開發者希望將自己的應用上架到鴻蒙應用市場。本文將詳細介紹鴻蒙應用上架的全流程,幫助開發者順…

20250517 我設想一個空間,無限大,空間不與其中物質進行任何作用,甚至這個空間能容納可以伸縮的空間

1.我設想一個空間,無限大,空間不與其中物質進行任何作用,甚至這個空間能容納可以伸縮的空間 您設想的這個空間具有一些有趣的特點: 無限大:空間本身沒有邊界或限制,理論上可以容納無限多的物質或結構。非…

使用 Kaniko來構建鏡像

使用 Kaniko來構建鏡像 Kaniko 是一種專注于容器鏡像構建的開源工具,其核心設計理念與 Docker 存在顯著差異。以下從功能定位、技術實現和適用場景三方面進行對比分析: 一、Kaniko 的核心特性 無需 Docker 守護進程 Kaniko 直接在容器或 Kubernetes 集…

webman用nginx代理靜態json文件的異步跨域

場景 有.json文件置于webman的public目錄下,使用了nginx做代理,直接訪問文件是可以正常加載的,但跨域瀏覽器就無法加載文件。 nginx配置 文件是否存在于跟目錄,存在則設置請求頭,不存在則將請求交給webman處理即可。…

JDK 21新特性全面解析

Java Development Kit (JDK) 21作為Oracle長期支持(LTS)版本,于2023年9月正式發布,帶來了多項令人振奮的新特性和改進。本文將全面介紹JDK 21中的主要更新,幫助開發者了解如何利用這些新功能提升開發效率和代碼質量。 一、虛擬線程(Virtual …

如何選擇高性價比的 1T 服務器租用服務?

選擇高性價比的 1T 服務器租用服務?,可參考以下內容: 1、根據需求選配置? 明確自身業務需求是關鍵。若為小型網站或輕量級應用,數據存儲與處理需求不高,選擇基礎配置服務器即可。如個人博客網站,普通的 Intel Xeon …

JavaScript性能優化實戰(11):前沿技術在性能優化中的應用

引言 隨著Web應用復雜度和性能需求不斷提高,傳統的JavaScript優化技術已經無法滿足某些高性能計算場景的需求。本文將深入探討前沿Web技術如何突破JavaScript的性能瓶頸,為Web應用提供接近原生應用的性能體驗。從底層計算到圖形渲染,從并發處理到動畫優化,我們將通過實際案…

package.json 和 package-lock.json 的區別

package.json?? ??作用?? ??聲明項目元數據??:如項目名稱、版本、描述、入口文件等。??定義依賴范圍??:在 dependencies 和 devDependencies 中聲明項目??直接依賴??的包及其??版本范圍??(如 ^1.2.3)。??…

Rollup入門與進階:為現代Web應用構建超小的打包文件

我們常常面臨Webpack復雜配置或是Babel轉譯后的冗余代碼,結果導致最終的包體積居高不下加載速度也變得異常緩慢,而在眾多打包工具中Rollup作為一個輕量且高效的選擇,正悄然改變著這一切,本文將帶你深入了解這個令人驚艷的打包工具…