Sentrev ( Sen Tence Tr Ansformers EV Aluator)는 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은 올바른 컨텍스트가 먼저 순위가 매겨 졌음을 의미하는 반면, 0의 MRR은 검색되지 않았 음을 의미합니다. MRR은 다음 일반 방정식으로 계산됩니다.
올바른 컨텍스트가 검색되지 않으면 MRR은 자동으로 0으로 설정됩니다. 각 검색 작업에 대해 MRR이 계산되면 평균 및 표준 편차가 계산되고보고됩니다.
시간 성능 : 각 검색 작업에 대해 시간 성능이 초의 시간 성능을 계산합니다. 평균 및 표준 편차 가보고됩니다.
탄소 배출 : 탄소 배출량은 파이썬 라이브러리 codecarbon 통해 GCO2EQ (CO2 동등한 그램)에서 계산되며 오스트리아 지역에 대해 평가되었습니다. 그것들은 모든 검색 작업의 글로벌 컴퓨팅 부하에 대해보고됩니다.
Docker를 사용하여 로컬로 Qdrant를 쉽게 실행할 수 있습니다.
docker pull qdrant/qdrant:latest
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant:latest 이제 벡터 데이터베이스가 http://localhost:6333 에서 듣고 있습니다
우리는 3 개의 pdfs ( ~/pdfs/instructions.pdf , ~/pdfs/history.pdf , ~/pdfs/info.pdf )를 가지고 있으며 세 가지 다른 인코더 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 설정하거나 distance 인수를 설정하여 검색에 사용되는 거리 메트릭을 설정하거나 검색된 항목의 수를 조정하여 검색에 사용되는 거리 메트릭을 설정하여 mrr 의 청크로 플레이 할 수 있습니다 (이 경우 10). 평가를 위해 플롯을 원하는 경우 plot=True 를 전달할 수도 있습니다. 플롯은 CSV 파일의 동일한 폴더에 저장됩니다. 탄소 배출 추적을 켜려면 carbon_tracking 옵션과 3 글자 ISO 코드를 사용할 수 있습니다.
Qdrant on-Cloud 데이터베이스 솔루션을 이용할 수도 있습니다 (자세한 내용은 여기). 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" ]여기에보고 된 테스트 사례를 참조 할 수 있습니다
여기에서 모든 기능과 클래스에 대한 참조를 찾으십시오.
기부금은 항상 환영합니다!
Contributing.md에서 기여 가이드 라인을 찾으십시오
이 프로젝트는 오픈 소스이며 MIT 라이센스에 따라 제공됩니다.
SenTrEv 사용하여 검색 모델을 평가 한 경우 다음을 인용하십시오.
Bertelli, AC (2024). 세 문장 변압기 텍스트 임베더의 성능 평가 - Sentrev (v0.1.0)에 대한 사례 연구. 제노도. https://doi.org/10.5281/zenodo.14503887
유용하다고 생각되면 자금 조달을 고려하십시오.