Files
auto-ragas/rag_evaluation/evaluation/__pycache__/metrics.cpython-314.pyc
T

170 lines
13 KiB
Plaintext
Raw Normal View History

2026-01-16 16:01:39 +08:00
+
k¦hif*ãó€Rt^RIHtHtHtHtHt^RIHtH t ^RI
t
RRlt !RR]4t !RR ] 4t
!R
R ] 4t!R R
] 4t!RR] 4t!RR] 4t!RR] 4t!RR] 4t!RR4tR#)z(
Metrics calculation for RAG evaluation
)ÚListÚDictÚAnyÚOptionalÚSet)ÚABCÚabstractmethodNcóF€V^8„dQhR\R\\,/#)éÚtextÚreturn)Ústrr)Úformats"Ú;D:\workspace\cc-plugin\rag_evaluation\evaluation\metrics.pyÚ __annotate__r
s€÷)ñ)”Cð)œC¤�Hñ)ócóÖ€V'g \4#VP4p\\P!RV44p\\P!RV44pW,#)zë
Tokenize text supporting both Chinese and English
For Chinese: Extract individual characters
For English: Split by whitespace and punctuation
For mixed: Combine both approaches
Args:
text: Input text
Returns:
Set of tokens
z[\u4e00-\u9fff]z\b\w+\b)ÚsetÚlowerÚreÚfindall)r Ú
chinese_charsÚ
english_wordss& rÚ _tokenizer
sS€÷ Ü‹uˆ à �:‰:‹<€DôœŸ
š
Ð#5°tÓ<Ó=€MôœŸ
š
 :¨tÓ4Ó5€Mð Õ (Ð(rcó@a€]tRt^)toRt]V3RlRl4tRtVtR#)ÚMetriczAbstract base class for metricscó <€V^8„dQhRS[/#)r
r )Úfloat)rÚ
__classdict__s"€rrÚMetric.__annotate__-sø€÷
ñ
¡Uñ
rc ó€R#)z@
Calculate metric score
Returns:
Metric score (0.0 to 1.0)
N©)ÚselfÚkwargss&,rÚ calculateÚMetric.calculate,s€ñ
rr!N) Ú__name__Ú
__module__Ú __qualname__Ú__firstlineno__Ú__doc__rr$Ú__static_attributes__Ú__classdictcell__©rs@rrr)sø‡€Ù)à÷
óö
rrcó6a€]tRt^7toRtV3RlRltRtVtR#)Ú
ContextRecallu|
Context Recall: Measures how many relevant contexts were retrieved
Formula: |retrieved ∩ ground_truth| / |ground_truth|
cóL<€V^8„dQhRS[S[,RS[S[,RS[/#©r
Úretrieved_contextsÚground_truth_contextsr ©rr
r)rrs"€rrÚContextRecall.__annotate__>s1ø€÷ñá ¡�Iðñ $¡C�yðñ
ñ rc óŒ€V'gR#\V4p\V4pW4,p\V4\V4, pV#)zÇ
Calculate context recall
Args:
retrieved_contexts: List of retrieved context IDs/texts
ground_truth_contexts: List of ground truth context IDs/texts
Returns:
Recall score (0.0 to 1.0)
ç©rÚlen)r"r2r3Ú
retrieved_setÚground_truth_setÚ intersectionÚrecalls&&& rr$ÚContextRecall.calculate>sG€÷%ÙôÐ.Ó/ˆ
ÜÐ4Ó5Ðð%Õ7ˆ ä�\Ó"¤SÐ)9Ó%:Õ:ˆØˆ
rr!N©r&r'r(r)r*r$r+r,r-s@rr/r/7sø‡€ñ÷ örr/có6a€]tRt^[toRtV3RlRltRtVtR#)ÚContextPrecisionu{
Context Precision: Measures how many retrieved contexts are relevant
Formula: |retrieved ∩ ground_truth| / |retrieved|
cóL<€V^8„dQhRS[S[,RS[S[,RS[/#r1r4)rrs"€rrÚContextPrecision.__annotate__bs1ø€÷ñá ¡�Iðñ $¡C�yðñ
ñ rc óŒ€V'gR#\V4p\V4pW4,p\V4\V4, pV#)zÍ
Calculate context precision
Args:
retrieved_contexts: List of retrieved context IDs/texts
ground_truth_contexts: List of ground truth context IDs/texts
Returns:
Precision score (0.0 to 1.0)
r7r8)r"r2r3r:r;r<Ú precisions&&& rr$ÚContextPrecision.calculatebsG€÷"ÙôÐ.Ó/ˆ
ÜÐ4Ó5Ðð%Õ7ˆ ä˜ Ó%¬¨MÓ(:Õ:ˆ ØÐrr!Nr?r-s@rrArA[sø‡€ñ÷ örrAcó6a€]tRt^toRtV3RlRltRtVtR#)ÚMRRzt
Mean Reciprocal Rank: Measures the rank of the first relevant context
Formula: 1 / rank_of_first_relevant_context
cóL<€V^8„dQhRS[S[,RS[S[,RS[/#r1r4)rrs"€rrÚMRR.__annotate__†s1ø€÷ñá ¡�Iðñ $¡C�yðñ
ñ rc óŽ€V'd V'gR#\V4p\V^R7FwrEWS9gK
RV, u# R#)zÁ
Calculate MRR
Args:
retrieved_contexts: Ordered list of retrieved context IDs/texts
ground_truth_contexts: List of ground truth context IDs/texts
Returns:
MRR score (0.0 to 1.0)
r7©Ústartçð?)rÚ enumerate)r"r2r3r;ÚrankÚcontexts&&& rr$Ú
MRR.calculate†sE€÷"×)>ÙäÐ4Ó5Ðô'Ð'9À×C‰MˆDØÖ*ؘT•zÒ!ñDñrr!Nr?r-s@rrHrHsø‡€ñ÷ örrHcó:a€]tRt^¢toRtRV3RlRlltRtVtR#)ÚNDCGzX
Normalized Discounted Cumulative Gain
Measures ranking quality with position discount
Ncób<€V^8„dQhRS[S[,RS[S[,RS[S[,RS[/#)r
r2r3Úkr )rr
rÚintr)rrs"€rrÚNDCG.__annotate__©s?ø€÷*ñ*á ¡�Ið*ñ $¡C�yð*ñ ‘C�=ð *ñ
ñ *rc ó†€V'd V'gR#Vf \V4p\V4pRp\VRV^R7F%wrgWt9dRMRpWXV^,, ,
pK' Rp \\V4V4p
\ ^V
^,4FpV RV^,, ,
p K V R8XdR#WY, p V #)zÿ
Calculate NDCG@k
Args:
retrieved_contexts: Ordered list of retrieved context IDs/texts
ground_truth_contexts: List of ground truth context IDs/texts
k: Cut-off rank (if None, use all retrieved contexts)
Returns:
NDCG score (0.0 to 1.0)
r7NrLrN)r9rrOÚminÚrange) r"r2r3rVr;ÚdcgÚirQÚ relevanceÚidcgÚ max_relevantÚndcgs &&&& rr$ÚNDCG.calculate©s€÷""×)>Ùà Š9ÜÐ&Ó'ˆAäÐ4Ó5ÐðˆÜ#Ð$6°r¸Ð$:À!×D‰JˆAØ&Ô:™ÀˆIØ   A¥Õ&Õ &ŠCñEð ˆÜœ3Ð4Ó5°qÓ9ˆ Ü�q˜,¨Õ*Ö+ˆAØ �C˜1˜q�5•MÕ !ŠDñ,ð �3Œ;Ùà�zˆØˆ rr!©Nr?r-s@rrTrT¢sø‡€ñ÷ *÷*ð*rrTcó6a€]tRt^ÖtoRtV3RlRltRtVtR#)Ú Faithfulnesszb
Faithfulness: Measures if the answer is faithful to the contexts
Uses RAGAS faithfulness metric
cóB<€V^8„dQhRS[RS[RS[S[,RS[/#)r
ÚquestionÚanswerÚcontextsr )r
rr)rrs"€rrÚFaithfulness.__annotate__Ýs7ø€÷"&ñ"&áð"&ñð"&ñ‘s•)ð "&ñ
ñ "&rc óæ€V'd V'gR#\V4pRPV4p\V4pV'gR#WF,p\V4\V4, p\VR4#)z­
Calculate faithfulness score
Args:
question: Question text
answer: Generated answer
contexts: Retrieved contexts
Returns:
Faithfulness score (0.0 to 1.0)
r7Ú rN)rÚjoinr9rZ) r"rgrhriÚ
answer_tokensÚ context_textÚcontext_tokensÚoverlapÚ faithfulnesss &&&& rr$ÚFaithfulness.calculateÝs^€÷(ŸXÙô" &Ó)ˆ
Ø—x‘x Ó)ˆ Ü" <Ó0ˆçÙàÕ0ˆÜ˜7“|¤c¨-Ó&8Õ8ˆ ä�< Ó%Ð%rr!Nr?r-s@rrereÖsø‡€ñ÷ "&ö"&rrecó6a€]tRtRtoRtV3RlRltRtVtR#)ÚAnswerRelevancyizj
Answer Relevancy: Measures if the answer is relevant to the question
Uses RAGAS answer relevancy metric
có,<€V^8„dQhRS[RS[RS[/#)r
rgrhr ©r
r)rrs"€rrÚAnswerRelevancy.__annotate__ s)ø€÷ #ñ #áð #ñð #ñ
ñ #rc óÄ€V'd V'gR#\V4p\V4pV'gR#W4,p\V4\V4, p\VR4#)z�
Calculate answer relevancy score
Args:
question: Question text
answer: Generated answer
Returns:
Relevancy score (0.0 to 1.0)
r7rN©rr9rZ)r"rgrhÚquestion_tokensrnrqÚ relevancys&&& rr$ÚAnswerRelevancy.calculate sR€÷$ŸvÙô$ HÓ-ˆÜ! &Ó)ˆ
çÙð"Õ1ˆÜ˜“L¤3 Ó#7Õ7ˆ ä�9˜cÓ"Ð"rr!Nr?r-s@rrurusø‡€ñ÷ #ö #rrucó6a€]tRtRtoRtV3RlRltRtVtR#)Ú Correctnessi,zh
Correctness: Measures if the answer is correct compared to ground truth
Uses RAGAS correctness metric
có,<€V^8„dQhRS[RS[RS[/#)r
rhÚground_truth_answerr rw)rrs"€rrÚCorrectness.__annotate__3s)ø€÷%ñ%áð%ñ!ð%ñ
ñ %rc óÄ€V'd V'gR#\V4p\V4pV'gR#W4,p\V4\V4, p\VR4#)z›
Calculate correctness score
Args:
answer: Generated answer
ground_truth_answer: Ground truth answer
Returns:
Correctness score (0.0 to 1.0)
r7rNrz)r"rhr�rnÚground_truth_tokensrqÚ correctnesss&&& rr$ÚCorrectness.calculate3sT€÷$×0Ùô" &Ó)ˆ
Ü'Ð(;Ó<Ðç"ÙàÕ5ˆÜ˜'“l¤SÐ)<Ó%=Õ=ˆ ä�; Ó$Ð$rr!Nr?r-s@rrr,sø‡€ñ÷ %ö%rrcó<a€]tRtRtoRtRtV3RlRltRtVtR#)ÚMetricsCalculatoriUz%Calculator for all evaluation metricscóØ€\4Vn\4Vn\ 4Vn\
4Vn\4Vn \4Vn \4Vn