Try   HackMD

AI / ML領域相關學習筆記入口頁面

Deeplearning.ai GenAI/LLM系列課程筆記

GenAI
RAG

Building and Evaluating Advanced RAG
建立語評估進階RAG

基于大语言模型知识问答应用落地实践 – 使用 TruLens 做自动化 RAG 项目评估测试

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Modified from 2023.12。IVAN ILIN。Advanced RAG Techniques: an Illustrated Overview

  • RAG的核心組件可分為分為兩個核心部分:檢索器(Retrieve)與生成器(Generator/LLM)
  • 絕大部分的工作都在改進檢索器的效能

RAG Triad of metrics RAG(Retrieval-Augmented Generation)三元組評估方法

🦑 TruLens 官方說明The RAG Triad

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • RAG的評估可根據組件拆分為兩個核心部分:檢索器(Retrieve)與生成器(Generator/LLM)的評估
  • 其中,評估檢索器的表現-上下文相關性(Context Relevance)是最關鍵且可控的,這是後續生成正確回憶的基礎

用來檢驗大型語言模型(LLMs)產生的回答在三個關鍵方面的品質:上下文相關性(Context Relevance)、真實性(Groundedness)和回答相關性(Answer Relevance)。確保LLM應用不會出現幻覺性錯誤(hallucinations),這種錯誤通常發生在檢索環節未能獲取足夠或相關的上下文時。
核心概念參考202309。RAGAS: Automated Evaluation of Retrieval Augmented Generation

在RAG(Retrieval Augmented Generation)評估中,利用TrueLens建立了三種評估模組,它們分別基於查詢(Query)、回應(Response)和上下文(Context)之間的配對比較。這被稱為RAG評估三角(The RAG Triad),其概念如下:

  • 上下文相關性(Context Relevance)
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      評估的第一步是確保RAG應用在檢索時,每一塊上下文都與輸入查詢相關
    • 因為LLM將使用這些上下文信息來形成回答,任何不相關的上下文都可能被編織進最終的幻覺性回答中
  • 真實性(Groundedness)
    • 在上下文被檢索之後,LLM將其形成答案。LLM經常容易偏離事實,誇大或擴展至聽起來正確的答案
    • 為了驗證回應的真實性,我們可以將回答分解成獨立的主張(separate the response into individual claims),並獨立搜尋檢索到的上下文中支持每個主張的證據
  • 回答相關性(Answer Relevance)
    • 最後,LLM的回答仍需要有幫助地回應到原始問題,可以通過評估最終回答與用戶輸入的相關性來驗證

評估方法的回饋品質/意義性上(Meaningful)和執行評估的易行性/可擴展性(Scalable)

  • Feedback Functions can be implemented in different ways

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    trulens_eval/core_concepts_feedback_functions

    評估方法根據它們提供的回饋品質/意義性上(Meaningful)和執行評估的易行性/可擴展性(Scalable)進行不同的權衡

  • Domain Expert/Ground Truth Evals(領域專家/基準真實評估)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    在早期開發階段,建議從
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    領域專家評估開始。這些評估通常由開發人員自己完成,代表了應用程序預期完成的核心用例。這可以讓您深入了解應用程序的性能,但缺乏規模。範例筆記本

    • 在意義性軸上與人類評估相近, 可擴展性最差(成本最高)
    • 基準真實評估是使用預先確定的標籤或指標來評估文本處理系統的性能
    • 從專家標註的數據集中提取標籤,然後將這些標籤應用於系統的輸出以評估性能
    • 基準真實評估方法通常被認為是文本處理系統性能評估的金標準,但也可能需要更多的時間和成本來收集和處理數據
  • Human Evals(人類評估)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    完成早期評估並對應用程式更有信心後,收集人工回饋通常很有用。這通常可以是用戶提供的二進位(向上/向下)回饋的形式。這比地面實況評估更具可擴展性,但受人為偏好影響(struggles with variance),並且收集起來仍然很昂貴

    • 通過人類參與來評估文本處理系統的性能
    • 位於意義性軸的最高點,顯示這是最具洞察力的評估方法,但在可擴展性方面有限
    • 將文本數據提交給人類評審員,然後根據他們的主觀意見來評估系統的性能
    • 人類評估方法可能包括(使用者)評分、評論等
  • Traditional NLP Evals(傳統NLP評估)

    • 最容易擴展,但在意義性上不如其他方法深入
    • 用傳統的自然語言處理技術和指標來評估文本處理系統的性能
    • 這些評估方法通常包括基於規則的評估和基於統計的評估
      • 例如,常見的傳統NLP評估指標包括准確率、召回率、精確率等
      • 傳統NLP評估方法通常著重於句法和語義的分析,但在處理自然語言的複雜性方面可能有限
  • Medium Language Model,MLM Evals(中型語言模型評估)

    • 例如BERT,這些模型提供了一個評估LLM應用的理想中點,既相對便宜又能提供細緻且有意義的反饋
    • 在某些情況下,這些模型可能需要微調以適應特定領域的反饋需求
  • Large Language Model,LLM Evals(大型語言模型評估)

    • 使用大型語言模型(LLM)來評估文本處理系統的性能
    • 可擴展性略遜MLM,但在生成有意義反饋方面更為優秀
    • 在大規模應用時可能相當昂貴
    • 通常與人類評估高度一致。它們可以通過簡單的提示來進行,並且可以與LLM提供的推理相結合
      • Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
        研究文獻中非常有趣的一個結果是,如果你讓一組人對一個問題進行評分,約有80%的一致性。有趣的是,當你使用LLM進行評估時,LLM評估與人類評估之間的一致性也約為80至85%。因此,這表明LLM評估與人類評估在它們應用的基準數據集上是相當可比的。因此,反饋函數為我們提供了一種擴展評估的方式。

評估三大指標原則- Honest(誠實性)、Harmless(無害性)和Helpful(有幫助性)

🦑 TruLens 官方說明Honest, Harmless and Helpful Evaluations

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

自然語言處理應用時可能考慮的幾個關鍵指標,這些指標分為三大類:Honest(誠實性)、Harmless(無害性)和Helpful(有幫助性)。以下是各指標的詳細解釋:

  • Honest(誠實)

    • Answer relevance(回答相關性):
      • 評估回答與查詢問題之間的相關性。確保模型產生的回答與用戶意圖一致
    • Embedding distance(嵌入距離):
      • 通過計算嵌入向量之間的距離來評估查詢與回答之間的相似度。嵌入距離可以用來量化文本之間的語義相似性
    • BLEU, ROUGE:
      • 自然語言處理中用於評估機器翻譯和文本摘要的指標
      • BLEU評估生成的文本與參考答案之間的重疊程度,而ROUGE則專注於文本摘要的質量
    • Summarization quality(摘要質量):
      • 文本摘要的質量評估,包括摘要的內容是否準確、完整以及是否包含重要信息
    • Context Relevance(上下文相關性):
      • 評估回答是否與對話上下文相關,是否能夠理解和回應先前提到的信息
    • Groundedness(真實性):
      • 評估回答是否有可靠的支持證據或來源,是否可以提供合理的解釋或推理
    • Custom evaluations(自定義評估):
      • 根據特定應用場景或需求定制的評估方法,可以根據具體的任務和目標進行調整和擴展。
  • Harmless(無害)

    • PII Detection(個人身份信息檢測):
      • 檢測輸出中是否包含個人身份信息(PII),如姓名、地址、電話號碼等
      • 保護用戶隱私和數據安全的關鍵指標,特別是在處理敏感信息時
    • Toxicity(有害信息):
      • 檢測輸出中是否含有有害或不當言論,如侮辱、仇恨言論等
      • 確保輸出內容符合社會規範和道德標準的重要指標,維護平台的健康和安全
    • Stereotyping(成見):
      • 檢測回答是否存在刻板印象或偏見,如對特定群體的負面刻板印象
      • 確保輸出內容不會引起歧視或不公平對待
    • Jailbreaks:
      • 評估模型是否會產生非預期的行為或回答,即模型是否會“越獄”或偏離預期行為
      • 保模型的穩健性和安全性,在實際應用中避免不良後果
  • Helpful(有幫助)

    • Sentiment(情感):
      • 析回答中的情感傾向,包括正面、負面或中性情感
      • 確定回答的情感態度,以及是否符合用戶的期望或需求
      • 情感分析可應用於許多方面,如情感檢測、用戶情感分析、產品評價等
    • Language mismatch(語言不匹配):
      • 判斷回答是否與問題的語言設定不符
      • 這指的是回答的語言風格、用詞選擇是否與用戶提問的語言風格不匹配
      • 語言不匹配可能會導致用戶的誤解或不滿,因此需要確保回答與用戶的溝通風格保持一致
    • Conciseness(簡潔性):
      • 評估回答的簡潔度,即回答是否言之有物、切中要點,沒有多餘的冗長或不必要的信息
      • 簡潔的回答可以提高用戶的理解和記憶效率,增強溝通效果
    • Coherence(連貫性):
      • 評估回答是否邏輯清晰、內容連貫,即是否在語義上連貫,思路清晰,避免了內容上的自相矛盾或語義模糊
      • 連貫性是確保回答易於理解和接受的關鍵因素,尤其是在文本生成和對話系統中


上面是理論與原則部分

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
以下是整合Llmaindex與TruLens的評估實作。使用TruLens特殊的API及範例

Lab Lesson 2: RAG Triad of metrics

Feedback function

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • TruLens官方指南core_concepts_feedback_functions

    反饋函數(Feedback Functions)類似於標籤函數(labeling functions),提供了一種以程序化方式在應用運行中生成評估的方法。TruLens的反饋函數實現包裹了一個支持的提供者模型,例如相關性模型或情感分類器,這些模型被重新配置以提供評估。通常,為了獲得最大的靈活性,這個模型可以是另一個大型語言模型(LLM)

import nest_asyncio nest_asyncio.apply() from trulens_eval import OpenAI as fOpenAI provider = fOpenAI()
  • Structure of Feedback function
    image

    Feedback functions, analogous to labeling functions, provide a programmatic method for generating evaluations on an application run.

    用於生成對應用運行的評估的程序化方法。這個結構包含了幾個部分:

    • provider = fOpenAI():這表示使用了一個名為 fOpenAI 的大型語言模型(LLM),作為運行反饋函數的提供者
    • f_qa_relevance:這是被定義的反饋函數方法的實例
    • Feedback():這是一個函數,它接受一個提供者的模型(在這個案例中是 provider.relevance),這個模型是為了評估而重新用途的,比如相關性模型或情感分類器
    • name="Answer Relevance":這是反饋函數的人類可讀名稱,用於評估儀表板上的顯示
    • .on_input().on_output():這些方法指向用戶查詢和應用輸出,允許反饋函數訪問這些數據以產生評估

    標籤函數(labeling functions)是一種在機器學習中用於生成訓練數據標籤的程序化方法。這些函數允許研究人員使用專業知識、規則或模型來創建標籤,而不是手動標注整個數據集

    • Example of labeling functions
      ​​​​​​​​def food_related(tweet): ​​​​​​​​ food_keywords = ['pizza', 'restaurant', 'dinner', 'lunch'] ​​​​​​​​ return any(keyword in tweet.lower() for keyword in food_keywords) ​​​​​​​​# 假設 `tweets` 是一個推文列表 ​​​​​​​​labels = [food_related(tweet) for tweet in tweets]

以下為Feedback function在 Answer Relevance、Context Relevance與Groundedness三項目的使用範例

1. Answer Relevance

Is the final response useful?

image

from trulens_eval import Feedback f_qa_relevance = Feedback( provider.relevance_with_cot_reasons, name="Answer Relevance" ).on_input_output()
  • Q: “How can altruism be beneficial in building a career?”
    image

    • 回應詳細解釋了利他主義如何在建立職業生涯中起到積極作用。它指出,即使在專注於個人職業成長的同時幫助他人,也能為個體帶來更好的結果。這種行為能夠建立積極的聲譽和人脈網絡,進而導致新機會和合作的出現。此外,幫助他人還能帶來成就感和目的感,從而有助於整體的職業滿意度和福祉。

    image

    • 支持證據: 強調了回應如何清晰地提供了利他主義在職業發展中的益處。它提到,通過幫助他人,個體可以為自己獲得更好的結果,創造良好的聲譽和網絡,並導向新的機會和合作。它還突顯了幫助他人能夠提供成就感和目的感,這對職業滿意度和個人福祉有正面貢獻。
    • 回答相關性(Answer Relevance)的評分為0.9
      • 顯示這個回應與原始查詢非常相關,並且以很高的相關性直接回答了問題。這個高分意味著回應非常接近於查詢所要求的信息,並且有效地涵蓋了查詢的主題。

2. Context Relevance

How good is the retrieval?

image

from trulens_eval import TruLlama context_selection = TruLlama.select_source_nodes().node.text
  • qs_relevance

    ​​​​import numpy as np ​​​​f_qs_relevance = ( ​​​​ Feedback(provider.qs_relevance, ​​​​ name="Context Relevance") ​​​​ .on_input() ​​​​ .on(context_selection) ​​​​ .aggregate(np.mean) ​​​​)
  • qs_relevance_with_cot_reasons

    • 使用思維鏈(chain of thought reasoning,COT) 解釋評估過程如何得到這個分數

    The second piece is the supporting evidence or the rationale or the chain of thought reasoning behind why the evaluation produced this score.

    ​​​​f_qs_relevance = ( ​​​​ Feedback(provider.qs_relevance_with_cot_reasons, ​​​​ name="Context Relevance") ​​​​ .on_input() ​​​​ .on(context_selection) ​​​​ .aggregate(np.mean) ​​​​)
  • Q: “How can altruism be beneficial in building a career?”

    image

    回應包含了從一個指南或書籍的節選,這段文本提供了關於找到合適工作並增加找到支持個人職業成長職位的機會的建議。建議包括研究角色和公司、與朋友交談,並選擇性地安排與吸引人的公司中的人進行非正式的信息性面試。此外,如果可能的話,從內部人士那裡獲得推薦

    • 上下文相關性(Context Relevance)得分為0.7
      • 顯示這段回應與原始查詢有相當的相關性。這個得分反映出檢索到的內容與問題的主題密切相關,但可能不是完美匹配,因為問題是關於利他主義如何在職業生涯中有益,而回應似乎沒有直接提到利他主義。

    image

    支持證據部分說明了回應如何提供了建立職業生涯的有用信息,提供了關於潛在工作機會的洞察,並允許個體做出有關他們職業道路的明智決定
    雖然回應提供了有關職業建設的實用建議,但沒有充分涉及查詢中提到的利他主義概念。

3. Groundedness

from trulens_eval.feedback import Groundedness

grounded = Groundedness(groundedness_provider=provider)

f_groundedness = (
    Feedback(grounded.groundedness_measure_with_cot_reasons,
             name="Groundedness"
            )
    .on(context_selection)
    .on_output()
    .aggregate(grounded.grounded_statements_aggregator)
)

Evaluate and Iterate 原則

  • Start with LlamaIndex Basic RAG
    開始時,使用LlamaIndex的基本RAG模型進行評估
  • Evaluate with TruLens RAG Triad
    接著,使用TruLens的RAG Triad模型進行評估
    • Failure modes related to context size
      觀察到與上下文大小相關的失敗模式,模型對於更大範圍的上下文難以處理
  • Iterate with LlamaIndex Sentence Window RAG
    根據之前評估的結果,對LlamaIndex進行了迭代,引入了句子窗口RAG模型
  • Re-evaluate with TruLens RAG Triad
    • Do we see improvements in Context Relevance?
      再次使用TruLens的RAG Triad模型進行評估
    • What about other metrics?
      檢查上下文相關性是否有改善,以及其他評估指標是否有改善
  • Experiment with different window sizes
    進行實驗,嘗試不同的窗口大小
    • What window size results in the best eval metrics?
      觀察哪種窗口大小會產生最佳的評估指標

Evaluation of the RAG application

  • 使用TruLlama建立紀錄器
    TruLens 提供與 Llama-Index 深度集成的 TruLlama,允許您檢查和評估使用 Llama-Index 構建的應用程序的內部結構
    📓 Llama-Index Integration

    ​​​​from trulens_eval import TruLlama ​​​​from trulens_eval import FeedbackMode ​​​​tru_recorder = TruLlama( ​​​​ sentence_window_engine, ​​​​ app_id="App_1", ​​​​ feedbacks=[ ​​​​ f_qa_relevance, ​​​​ f_qs_relevance, ​​​​ f_groundedness ​​​​ ] ​​​​)
    • sentence_window_engine:這個參數是指句子窗口引擎,它是一個用於分析和處理輸入問題的核心程式碼。在這裡,它被傳遞給 TruLlama,以便該物件可以利用這個引擎進行問題的分析。
    • app_id:這個參數是應用程序的 ID,用於識別不同的應用程序。在這個例子中,應用程序的 ID 被設置為 "App_1"。
    • feedbacks:這是一個列表,包含了要捕獲的反饋模式。在這個例子中,列表中包含了三個反饋模式:f_qa_relevancef_qs_relevancef_groundedness。這些反饋模式用於評估問題與回答之間的相關性,以及回答的合理性。
  • 建立評估用的問題集

    ​​​​eval_questions = [] ​​​​with open('eval_questions.txt', 'r') as file: ​​​​ for line in file: ​​​​ # Remove newline character and convert to integer ​​​​ item = line.strip() ​​​​ eval_questions.append(item) ​​​​eval_questions
    ​​​​['What are the keys to building a career in AI?', ​​​​ 'How can teamwork contribute to success in AI?', ​​​​ 'What is the importance of networking in AI?', ​​​​ 'What are some good habits to develop for a successful career?', ​​​​ 'How can altruism be beneficial in building a career?', ​​​​ 'What is imposter syndrome and how does it relate to AI?', ​​​​ 'Who are some accomplished individuals who have experienced imposter syndrome?', ​​​​ 'What is the first step to becoming good at AI?', ​​​​ 'What are some common challenges in AI?', ​​​​ 'Is it normal to find parts of AI challenging?', ​​​​ 'How can I be successful in AI?']
  • 進行評估

    ​​​​for question in eval_questions: ​​​​with tru_recorder as recording: ​​​​ sentence_window_engine.query(question) ​​​​ ​​​​records, feedback = tru.get_records_and_feedback(app_ids=[]) ​​​​records.head() ​​​​ ​​​​import pandas as pd ​​​​pd.set_option("display.max_colwidth", None) ​​​​records[["input", "output"] + feedback]

    image
    image

  • 呼叫leaderboard與dashboard

    ​​​​tru.get_leaderboard(app_ids=[])

    image

    ​​​​tru.run_dashboard()

    image
    image
    image

Supplyment

Llamaindex內建的評估模組與文件

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
(多模態評估)指標太過簡陋、建議使用RAGAS或TrueLens

2024.02。humanloop。Evaluating LLM Applications

learn how to apply traditional software evaluation techniques to AI, understand the different types of evaluations and when to use them, and see what the lifecycle of evaluating LLM applications looks like at the frontier of Generative AI.

image

Typical makeup of a test suite in software development CI. Unit tests tend to be the hardest to emulate for LLMs.

  • 單元測試(Unit) - 數量眾多,針對特定的程式碼單元進行測試,運行速度快。
  • 整合測試(Integration) - 數量較少,涵蓋多個程式碼塊,運行速度比單元測試慢,可能需要模擬外部服務。
  • 端對端測試(End-to-end) - 模擬最終使用者或API呼叫者的體驗;運行速度較慢,通常需要與系統的實時版本進行互動。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
由於LLMs的隨機性、主觀性、成本和延遲以及評估範圍的問題,單元測試在LLMs上的應用變得困難,這使得它們難以進行傳統的單元測試。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Observability and Traceability

  • 使用模型進行判斷評估成趨勢(Model sourced judgments are increasingly promising)
    image
    > Typical makeup of different sources of evaluation judgments. AI evaluators are a good sweet spot for scaling up your evaluation process, while still providing Human-level performance.
    • 启發式/代碼:使用簡單的決策規則基礎的判斷,對成本、令牌使用、延遲、輸出上的正則表達式規則等屬性進行判斷。這些通常在大規模運行時快速且便宜

      Heuristic/Code - using simple deterministic rules based judgments against attributes like cost, token usage, latency, regex rules on the output, etc. These are generally fast and cheap to run at scal

    • 模型(或'AI'):使用其他基礎模型提供對組件輸出的判斷。這允許以人類判斷的一小部分成本提供更多的定性和細緻的判斷

      Model (or 'AI') - using other foundation models to provide judgments on the output of the component. This allows for more qualitative and nuanced judgments for a fraction of the cost of human judgments.

    • 人類:從您的應用的終端用戶或內部領域專家獲得金標準判斷。這可能是最昂貴和最慢的選項,但也是最可靠的

      Human - getting gold standard judgments from either end users of your application, or internal domain experts. This can be the most expensive and slowest option, but also the most reliable.

LLM/AI 做為評估器的問題

  • 《Judging LLM-as-a-Judge》LLM評估器(評估模型)與人類判斷達到了超過80%的一致性;這等同於人類之間的一致性水平。

  • 《Benchmarking Cognitive Biases in Large Language Models as Evaluators在15種不同的LLM變體上測量了6種認知偏見。他們發現,將結果的順序呈現給模型的簡單細節可以對評估產生重大影響

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    The cognitive bias benchmark for LLMs. Simple details such as the order of the results presented to the model can have material impact on the evaluation. Source: https://arxiv.org/pdf/2309.17012

  • 團隊應該考慮提升人類判斷努力,專注於改進模型評估器,以實現更可擴展、可重複且經濟的評估過程

    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      fine-tuning specialist, more economical evaluator models using human judgements

判斷標準

  • 一般性能(延遲、成本和錯誤閾值)
  • 行為(語氣、寫作風格、多樣性、事實性、相關性等)
  • 倫理(偏見、安全性、隱私等)
  • 用戶體驗(參與度、滿意度、生產力等)
  • Different stages of evaluation are necessary

    Recommended stages for a robust evaluation process. Interactive, offline and online.

    有3個互補的評估階段對於支持LLM塊相關干預的快速迭代循環具有最高的投資報酬率:

    • 互動式(Interactive)
      • 有一個互動式的類似於playground的編輯環境,允許對模型的組件進行快速實驗,並提供立即的評估者反饋。
      • 這通常在相對較少的情況下最有效。這允許團隊(無論是技術還是非技術)快速探索LLM應用的設計空間,並獲得一種非正式的感覺,哪些東西表現得很好。
    • 批量離線(Batch offline)
      • 對最有前途的版本/變體(variations)進行基準測試或回歸測試,以提供更系統化的評估
        • 將應用的不同組件或變體放在一組精心挑選的情境下進行測試,以確定哪些變體在實際應用中可能表現最佳。
        • 理想情況下,這一階段會有多種評估者參與,他們根據各自負責的應用組件進行評估,並將測試結果與預期的黃金標準結果進行比較
        • 這種方法不僅有助於識別和修正潛在的問題,還能確保應用在部署前達到一定的質量標準。此外,批量離線測試可以自然地融入現有的持續集成(CI)流程中,使得整個開發過程更加高效和系統化
    • 在線監控(Monitoring online)
      • 部署後,實際用戶的互動可以持續評估,以監控模型的性能
      • 這個過程可以驅動警報,收集額外的情況以進行離線評估,並通知何時進行進一步的干預
      • 通過內部環境的分段部署,或者首先對選定的用戶群進行beta測試

高質量的數據集仍然是至關重要的

  • 應用的個別組件預期行為的覆蓋範圍
  • 好的評估數據集應該涵蓋行為的完整分佈
  • 利用公共/學術基準,從您自己的系統收集數據並創建合成數據

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
真實用戶互動是最有價值的數據來源

  • 透過互動環境,內部專家可以合成預期生產互動範例,形成後續離線評估的基準數據集。
  • 緊密整合觀察性數據和評估管理環境,有助於隨時間將真實場景納入基準數據集。
  • 設置應用以從一開始就捕獲豐富的用戶反饋,可以選擇性地加入到基準數據集中。

合成數據正處於上升趨勢

  • 利用少量高質量數據,LLMs能夠生成額外的輸入範例,幫助擴大數據集。
  • 惡意攻擊模擬 model red-teaming
    • 使用模型紅隊操作或合成對抗性範例來測試系統的弱點
    • 透過合成器模型(synthesizer model)生成旨在破壞系統的範例
    • 《Red Teaming Language Models with Language Models》中,透過少次範例提示的變體揭露LLM聊天機器人的冒犯回復、數據洩露和其他漏洞
    • 利用預訓練的冒犯性分類器(pre-trained offensive classifier)來自動化評估過程
      • LLM偏見導致的多樣性限制
      • 需要生成並過濾數十萬個合成範例

image

Illustration of model red teaming; one LLM makes adversarial requests to another to elicit bad behaviour. Source: https://arxiv.org/pdf/2202.03286

RAG evaluation metrics

幾種RAG指標的整理

2024.01。confident-ai。LLM Evaluation Metrics: Everything You Need for LLM Evaluation

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
high quality LLM outputs is the product of a great retriever and generator.
image

2024.02。Nvidia。Evaluating Retriever for Enterprise-Grade RAG

2024.02。Pratik Bhavsar Galileo Labs。Mastering RAG: Improve RAG Performance With 4 Powerful RAG Metrics

Galileo 公司提出的評估指標,可與Trulens的做對照
image
Choosing your Guardrail Metrics

細節見後面:

  • Output Quality Metrics:

    • Uncertainty:
    • Correctness
    • BLEU & ROUGE-1
    • Prompt Perplexity
  • RAG Quality Metrics

    • Chunk Attribution: (Boolean)
      • A chunk-level boolean metric that measures whether a ‘chunk’ was used to compose the response.
    • Chunk Utilization: (float)
      • A chunk-level float metric that measures how much of the chunk text that was used to compose the response.
    • Completeness:
      • A response-level metric measuring how much of the context provided was used to generate a response
    • Context Adherence:
      • A response-level metric that measures whether the output of the LLM adheres to (or is grounded in) the provided context.
    • Context Relevance
      • Measures how relevant the context provided was to the user query.
  • Insight:

    • 分別從Chunk與Response 層級來探究檢索器的表現
    • Chunk層級
      • 是否被使用(Chunk Attribution) - 有點多餘
    • 被使用比率(Chunk Utilization)
      • 有多少 chunk text被用於撰寫回復
      • 以chunk為單位計算被使用率? 但如果傳回的top_k很少?
    • 上下文相關性(Context Relevance)
      • 應該也是基於Chunk到的文本為單位,去檢視與"Query"的相關程度
    • Response 層級
      • 所提供的上下文中有多少被用於生成回答(Completeness)
      • 直接看chunk內文數量來計算內文被使用比例? ,如果chunk size越小;可能得到的比例越高,會有既有的偏差存在
      • LLM 的輸出是否符合所提供的上下文(Context Adherence)
      • 類似TrueLens中"Groundess"定義的"Is the response supported by the context?"
    • 從Response層級來檢驗,還是受到生成階段的LLM效能影響,最直接的還是看Query在檢索階段表現,例如Truelens的"Context Relevance" : "Is the retrieved context relevant to the query?" ,直接看檢索到的內文與Query的關聯性。是以每個chunk為單位來計算關聯性分數。

RAGAS關於Context 評估的相關指標

image

Ragas/📚Context 檢索上下文相關指標
  • 📚Context_recall

    • 測量檢索到的上下文在多大程度上與標註答案(ground truth)對齊

      measures the extent to which the retrieved context aligns with the annotated answer, treated as the ground truth.

    context recall=|GT sentences that can be attributed to context||Number of sentences in GT|

  • 📚Context_precision

    • 評估所有真實相關項目是否在上下文中排名較高

      evaluates whether all of the ground-truth relevant items present in the contexts are ranked higher or not.

    Context Precision@K=k=1K(Precision@k×vk)Total number of relevant items in the top K results

    Mean Reciprocal Rank (MRR)
    MRR 是用來衡量檢索系統返回的相關項目在排名中的位置,計算公式為:

    MRR=1Ni=1N1ranki
    其中
    N
    是測試問題的數量,
    ranki
    是第
    i
    個問題的第一個正確答案的排名。

    Context Precision MRR
    評估範圍 注重在前K名中的所有相關項目,反映了多個項目的排名精度 只關注第一個正確答案的排名,反映了找到首個正確答案的效率
    計算方法 使用每個位置的
    精度進行加權平均
    使用第一個正確答案的
    倒數排名進行平均
  • 📚Context entities recall

    • 基於真實值和上下文中存在的實體數量,衡量檢索上下文的召回率

      measure of recall of the retrieved context, based on the number of entities present in both ground_truths and contexts relative to the number of entities present in the ground_truths alone

context entity recall=|CEGE||GE|

  • 📚Context Relevancy
    • 評估檢索上下文的相關性
      context relevancy=|S||Total number of sentences in retrieved context|
Ragas/📚Answer Correctness。回答正確性
  • 回答正確性評估生成的答案與標準答案的準確度。分數範圍從0到1,分數越高,表示生成的答案與標準答案越接近,正確性越高。

  • 核心要點

    • 語義相似度ground truth:生成答案與標準答案的語義相似性。
    • 事實相似度Answer semantic similarity:生成答案與標準答案的語意相似度Answer semantic similarity
  • 計算方法

    • 使用真陽 (True Positive, TP)、假陽 (False Positive, FP) 和假陰 (False Negative, FN) 來量化事實重合度(factual overlap)

    • 計算 F1 分數來表示事實正確性ground truth

      F1 Score=|TP||TP|+0.5×(|FP|+|FN|)

    • 結合語義相似度和事實相似度,使用加權平均法得到最終的正確性分數

    • ground truth

      • Factual correctness quantifies the factual overlap between the generated answer and the ground truth answer. This is done using the concepts of:
        • TP (True Positive): Facts or statements that are present in both the ground truth and the generated answer.
        • FP (False Positive): Facts or statements that are present in the generated answer but not in the ground truth.
        • FN (False Negative): Facts or statements that are present in the ground truth but not in the generated answer.
          F1 Score=|TP(|TP|+0.5×(|FP|+|FN|))
    • weights:

      • a list of two weights corresponding to factuality and semantic similarity
      • Defaults [0.75, 0.25]
      • score = np.average( [f1_score, similarity_score], weights=self.weights, )