SentRev ( Sen Tence Tr ansFormers ev Aluator)是一個Python軟件包,旨在運行簡單的評估測試,以幫助您選擇使用PDF文檔的最佳嵌入模型,以檢索增強生成(RAG)。
SentRev與:
sentence_transformers中的類SentenceTransformer加載的文本編碼器/嵌入式您可以使用pip安裝軟件包(更容易,但沒有自定義):
python3 -m pip install sentrev或者,您可以從源代碼(更困難但可自定義)構建它:
# clone the repo
git clone https://github.com/AstraBert/SenTrEv.git
# access the repo
cd SenTrEv
# build the package
python3 -m build
# install the package locally with editability settings
python3 -m pip install -e .SentRev應用了非常簡單的評估工作流程:
有關工作流的可視化,請參見下圖
用於評估性能的指標是:
成功率:定義為數字檢索操作,其中正確的上下文在所有檢索到的上下文中排名最高,在總檢索操作中:
平均相互排名(MRR) :MRR定義了排名正確的上下文的高度位置在檢索結果之間。使用了MRR@10,這意味著為每個檢索操作返回10個項目,並對正確上下文進行評估,然後在0到1之間進行標準化(已在SentRev中實現)。 1的MRR表示正確的上下文是第一個排名,而MRR為0表示未檢索。 MRR用以下一般方程計算:
當未檢索正確的上下文時,MRR將自動設置為0。為每個檢索操作計算MRR,然後計算並報告平均值和標準偏差。
時間性能:對於每個檢索操作,計算秒鐘內的時間性能:然後報告平均值和標準偏差。
碳排放:通過Python圖書館的codecarbon在GCO2EQ(二氧化碳的克)中計算碳排放,並對奧地利地區進行了評估。報告了所有檢索操作的全球計算負載。
您可以使用Docker輕鬆在本地運行QDRANT:
docker pull qdrant/qdrant:latest
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant:latest現在,您的矢量數據庫正在http://localhost:6333
Let's say we have three PDFs ( ~/pdfs/instructions.pdf , ~/pdfs/history.pdf , ~/pdfs/info.pdf ) and we want to test retrieval with three different encoders sentence-transformers/all-MiniLM-L6-v2 , sentence-transformers/sentence-t5-base , sentence-transformers/all-mpnet-base-v2 .
我們可以使用這個非常簡單的代碼來完成:
from sentrev . evaluator import evaluate_rag
from sentence_transformers import SentenceTransformer
from qdrant_client import QdrantClient
# load all the embedding moedels
encoder1 = SentenceTransformer ( 'sentence-transformers/all-MiniLM-L6-v2' )
encoder2 = SentenceTransformer ( 'sentence-transformers/sentence-t5-base' )
encoder3 = SentenceTransformer ( 'sentence-transformers/all-mpnet-base-v1' )
# create a list of the embedders and a dictionary that map each one with its name for the stats report which will be output by SenTrEv
encoders = [ encoder1 , encoder2 , encoder3 ]
encoder_to_names = { encoder1 : 'all-MiniLM-L6-v2' , encoder2 : 'sentence-t5-base' , encoder3 : 'all-mpnet-base-v1' }
# set up a Qdrant client
client = QdrantClient ( "http://localhost:6333" )
# create a list of your PDF paths
pdfs = [ '~/pdfs/instructions.pdf' , '~/pdfs/history.pdf' , '~/pdfs/info.pdf' ]
# Choose a path for the CSV where the evaluation stats will be saved
csv_path = '~/eval/stats.csv'
# evaluate retrieval
evaluate_rag ( pdfs = pdfs , encoders = encoders , encoder_to_name = encoder_to_names , client = client , csv_path = csv_path , distance = 'euclid' , chunking_size = 400 , mrr = 10 , carbon_tracking = "USA" , plot = True )您可以通過設置chunking_size參數或通過設置text_percentage或通過設置距離參數來檢索距離參數或使用mrr設置來調整檢索到的項目的數量(在本例10中)來測試檢索的距離(在本例10)中使用(在此情況10)中使用用於檢索的distance度量(在此情況10)中使用(在本例10)中使用(在此情況10);如果需要評估圖,也可以通過plot=True :將在CSV文件的同一文件夾中保存圖;如果您想打開碳排放跟踪,可以使用carbon_tracking選項,然後使用您所處狀態的三個字母ISO代碼。
您還可以利用QDRANT在雲數據庫解決方案(在此處進行更多有關它)。您只需要QDRANT群集URL和API鍵即可訪問它:
from qdrant_client import QdrantClient
client = QdrantClient ( url = "YOUR-QDRANT-URL" , api_key = "YOUR-API-KEY" )這是您之前唯一必須對示例中提供的代碼做出的更改。
您也可以使用SentRev將PDF塊,矢量化和上傳到QDRANT數據庫中。
from sentrev . evaluator import upload_pdfs
encoder = SentenceTransformer ( 'sentence-transformers/all-MiniLM-L6-v2' )
pdfs = [ '~/pdfs/instructions.pdf' , '~/pdfs/history.pdf' , '~/pdfs/info.pdf' ]
client = QdrantClient ( "http://localhost:6333" )
upload_pdfs ( pdfs = pdfs , encoder = encoder , client = client )至於以前,您還可以使用chunking_size參數(默認為1000)和distance參數(默認值為餘弦)來玩耍。
您還可以在帶有sentRev的QDRANT數據庫中搜索已經存在的集合:
from sentrev . utils import NeuralSearcher
encoder = SentenceTransformer ( 'sentence-transformers/all-MiniLM-L6-v2' )
collection_name = 'customer_help'
client = QdrantClient ( "http://localhost:6333" )
searcher = NeuralSearcher ( client = client , model = encoder , collection_name = collection_name )
res = searcher . search ( "Is it possible to pay online with my credit card?" , limit = 5 )結果將作為有效載荷列表返回(您上傳到Qdrant集合的元數據以及矢量點)。
如果使用SentRev upload_pdfs函數,則應該能夠以這種方式訪問結果:
text = res [ 0 ][ "text" ]
source = res [ 0 ][ "source" ]
page = res [ 0 ][ "page" ]您可以在此處參考報告案例
在此處找到所有功能和類的參考
總是歡迎捐款!
查找貢獻指南
該項目是開源的,是根據MIT許可提供的。
如果您使用SenTrEv評估您的檢索模型,請考慮引用它:
Bertelli,AC(2024)。評估三句話變形金剛文本嵌入式的性能 - SentRev的案例研究(v0.1.0)。 Zenodo。 https://doi.org/10.5281/Zenodo.14503887
如果您發現它有用,請考慮為其提供資金。