- 大家好,我是同學(xué)小張,日常分享AI知識和實戰(zhàn)案例
- 歡迎 點贊 + 關(guān)注 ??,持續(xù)學(xué)習(xí),持續(xù)干貨輸出。
- +v: jasper_8017 一起交流??,一起進步??。
- 微信公眾號也可搜【同學(xué)小張】 ??
本站文章一覽:文章來源:http://www.zghlxwxcb.cn/news/detail-850348.html
上篇文章【AI大模型應(yīng)用開發(fā)】【RAG評估】0. 綜述:一文了解RAG評估方法、工具與指標, 我們盤點了當前RAG系統(tǒng)評估的一些主流方法、工具和評估指標。本文我們針對其中的RAGAS評估方法進行詳細介紹。我們將深入其原理,理解其評估指標背后是怎么實現(xiàn)的。都是我根據(jù)自己的理解用大白話解釋,保證大家能看懂。
- RAGAS論文地址:https://arxiv.org/pdf/2309.15217.pdf
0. 簡介及評估指標
RAGAS是一個對檢索增強生成(RAG)pipeline進行無參考評估的框架。
考慮標準的RAG設(shè)置,即給定一個問題q,系統(tǒng)首先檢索一些上下文c(q),然后使用檢索到的上下文生成答案as(q)。在構(gòu)建RAG系統(tǒng)時,通常無法訪問人工標注的數(shù)據(jù)集或參考答案,因此該工作將重點放在 完全獨立且無參考的度量指標上。
該方法有四個評估指標:
- 評估檢索質(zhì)量:
- context_relevancy(上下文相關(guān)性,也叫 context_precision)
- context_recall(召回性,越高表示檢索出來的內(nèi)容與正確答案越相關(guān))
- 評估生成質(zhì)量:
- faithfulness(忠實性,越高表示答案的生成使用了越多的參考文檔(檢索出來的內(nèi)容))
- answer_relevancy(答案的相關(guān)性)
1. 在LangChain中使用
1.1 首先構(gòu)建你的RAG程序
retriever的構(gòu)建步驟就不展開了,需要的同學(xué)可以去看下我之前的文章:【AI大模型應(yīng)用開發(fā)】【LangChain系列】4. 從Chain到LCEL:探索和實戰(zhàn)LangChain的巧妙設(shè)計
構(gòu)建完的代碼示例如下:
from langchain.chains import RetrievalQA
......
# ?。。。?!主要應(yīng)用點:RetrievalQA構(gòu)建的qa_chain的返回結(jié)果
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=retriever,
return_source_documents=True,
)
question = "How did New York City get its name?"
result = qa_chain({"query": question})
關(guān)鍵點:使用RetrievalQA
去獲取結(jié)果,因為使用RetrievalQA
去獲取的結(jié)果中包含key:query, result, source_documents,這是LangChain集成的Ragas直接需要的,不用自己再組裝數(shù)據(jù)結(jié)構(gòu)了:
-
上述result中包含query, result, source_documents字段,這三個字段可以直接用來評估出 context_relevancy, faithfulness, answer_relevancy 三個指標,context_recall無法評估
-
要想評估 context_recall 指標,需要人工添加預(yù)期的答案,并添加到 result 的key=“ground_truths” 的字段,例如下面的代碼
result_with_truth = result
result_with_truth["ground_truths"] = "XXXXXXXXXXXX"
1.2 評估
- 引入Ragas封裝:
RagasEvaluatorChain
- 引入Ragas評估指標
- 構(gòu)造評估的chain,需傳入構(gòu)造的chain的評估指標類型
- 將上面RAG的結(jié)果傳入這個評估chain,獲得評估結(jié)果
from ragas.langchain.evalchain import RagasEvaluatorChain
from ragas.metrics import (
faithfulness,
answer_relevancy,
context_precision,
context_recall,
)
# create evaluation chains
faithfulness_chain = RagasEvaluatorChain(metric=faithfulness)
answer_rel_chain = RagasEvaluatorChain(metric=answer_relevancy)
context_rel_chain = RagasEvaluatorChain(metric=context_precision)
context_recall_chain = RagasEvaluatorChain(metric=context_recall)
# 獲取結(jié)果
eval_result = faithfulness_chain(result)
eval_result = answer_rel_chain(result)
eval_result = context_rel_chain(result)
eval_result = context_recall_chain(result_with_truth)
1.3 結(jié)果示例
2. 原理
2.1 faithfulness
This measures the factual consistency of the generated answer against the given context. It is calculated from answer and retrieved context. The answer is scaled to (0,1) range. Higher the better.
這衡量了生成的答案在給定上下文中的事實一致性。它是根據(jù)答案和檢索到的上下文來計算的。答案按比例縮放到(0,1)范圍。越高越好。
2.1.1 測量步驟
(1)首先使用LLM來根據(jù)問題和答案提取一組語句S。這一步驟的目的是將較長的句子分解為更短、更集中的斷言。
該步驟的Prompt如下:
Given a question and answer, create one or more statements from each sentence in the given answer.
question: [question]
answer: [answer]
(2)針對生成的每個語句s,再次使用大模型或驗證函數(shù)來判斷這個語句是否能用上下文中的信息來支撐。
該步驟的Prompt如下(最后輸出Yes或No):
Consider the given context and following statements, then determine whether they are supported by the information present in the context. Provide a brief explanation for each statement before arriving at the verdict (Yes/No). Provide a final verdict for each statement in order at the end in the given format. Do not deviate from the specified format.
statement: [statement 1]
...
statement: [statement n]
(3)最后分數(shù)的計算,計算公式
其中V為可以被支撐的s的數(shù)量,S為生成的statement數(shù)量。
2.2 answer_relevancy
答案與問題的相關(guān)程度。不考慮答案的正確性,但是對答案不完整或包含冗余信息的情況進行懲罰。
2.2.1 測量步驟
(1)根據(jù)最終答案,利用大模型生成針對該問題的多個潛在的問題。
Prompt如下:
Generate a question for the given answer.
answer: [answer]
(2)針對生成的每個潛在問題,利用OpenAI的嵌入模型 text-embedding-ada-002 來計算與原始問題的向量相似度(余弦距離)。
不懂向量相似度或余弦距離是什么的,可以參考下我之前的文章:【AI大模型應(yīng)用開發(fā)】【補充知識】文本向量化與向量相似度(含Python代碼)
(3)最后分數(shù)的計算,計算公式
即最終對所有的向量相似度取個平均數(shù)。
2.3 context_relevancy
檢索回的上下文與原始問題之間的相關(guān)性,對其中的冗余信息進行懲罰
2.3.1 測量步驟
(1)利用大模型,從給定的context上下文信息中,提取出所有對最終答案直接相關(guān)或重要的句子,不改變句子內(nèi)容。
Prompt如下:
Please extract relevant sentences from the provided context that can potentially help answer the following question. If no relevant sentences are found, or if you believe the question cannot be answered from the given context, return the phrase "Insufficient Information". While extracting candidate sentences you’re not allowed to make any changes to sentences from given context.
(2)最后分數(shù)計算,計算公式
即:對答案有用的句子數(shù)量 / 上下文中全部句子的數(shù)量
2.4 context_recall
論文中沒提到這個指標,待查。這個需要認為給定參考答案,在RAGAS評估中比較少用。
3. 其它接口
langchain中的ragas還提供了其它的評估接口,簡單看一個。
3.1 批量評估 evaluate()
給定一系列需要測試的輸出結(jié)果,批量生成評估結(jié)果。
# run the queries as a batch for efficiency
predictions = qa_chain.batch(examples)
# evaluate
print("evaluating...")
r = faithfulness_chain.evaluate(examples, predictions)
# output
[{'faithfulness_score': 1.0},
{'faithfulness_score': 0.5},
{'faithfulness_score': 1.0},
{'faithfulness_score': 1.0},
{'faithfulness_score': 0.8}]
4. 總結(jié)
本文詳細介紹了RAGAS的原理及在LangChain中的使用方式。對于RAGAS的原理更是用通俗易懂的語言進行了深入講解,相信大家都能看懂!
從上面的評測步驟可以看到,RAGAS各個指標的評測都依賴了大模型的能力。所以也會有一定的不穩(wěn)定性。
如果覺得本文對你有幫助,麻煩點個贊和關(guān)注唄 ~~~
- 大家好,我是同學(xué)小張,日常分享AI知識和實戰(zhàn)案例
- 歡迎 點贊 + 關(guān)注 ??,持續(xù)學(xué)習(xí),持續(xù)干貨輸出。
- +v: jasper_8017 一起交流??,一起進步??。
- 微信公眾號也可搜【同學(xué)小張】 ??
本站文章一覽:
文章來源地址http://www.zghlxwxcb.cn/news/detail-850348.html
到了這里,關(guān)于【AI大模型應(yīng)用開發(fā)】【RAG評估】1. 通俗易懂:深度理解RAGAS評估方法的原理與應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!