Try   HackMD

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

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

Large Language Models with Semantic Search。大型語言模型與語義搜索

Finetuning Large Language Models。微調大型語言模型

LangChain for LLM Application Development


LangChain for LLM Application Development 系列課程筆記


LangChain -Evaluation

課程概要

LLM應用程序評估策略

  1. 問答對創建與評估

    • 手動檢視文檔,創建問答對
    • 利用LLM自動生成問答對,提高效率
  2. 實例運行與輸出分析

    • 輸入問題,分析系統回答,評估準確性
    • 啟用LangChain的debug模式(langchain.debug = True),深入了解系統運作
  3. 語言模型評分

    • 使用LLM評分回答的正確性

更換LLM或調整系統參數後的評估

  1. 重新評估

    • 更換LLM或調整參數後,重複評估流程
    • 比較變更前後的性能差異
  2. 輸入輸出分析

    • 使用LangChain評估平台或其他工具分析每步輸入輸出
    • 關注語言模型的輸入提示和檢索文檔
  3. 性能指標追蹤

    • 追蹤token使用量、響應時間等指標
    • 比較這些指標在變更前後的差異,評估影響

前言

如何評估使用大型語言模型(LLM)構建的複雜應用程序的效能。主要觀點包括:

  1. 評估應用程序效能的重要性:強調了在使用LLM構建應用程序時,評估其效能和準確性的重要性

  2. 應對實施變更的評估:討論了在更換LLM、調整使用向量數據庫的策略,或更改其他系統參數時,如何評估這些變更對應用程序效能的影響

  3. 評估工具和方法:提到了一些工具和方法,如視覺化工具和調試器,以及直觀檢視(by eye)和使用LLM自身來評估其他LLM、鏈和應用程序的方法

  4. 整體評估過程的重要性:強調了獲取應用程序各個步驟的全面圖景的重要性,以及在開發過程中重新思考評估工作流的趨勢

    a lot of development shifting towards prompting-based development, developing applications using LLMs, this whole workflow evaluation process is being rethought

Lab

  • 本節程式碼流程圖
    Execution and Evaluation Phase
    Application Building Phase
    Preparation Phase
    Output
    Results and Grades
    LLM Assisted Evaluation
    `QAEvalChain`
    Run Application
    and Generate Predictions
    Generate Q&A Pairs
    `QAGenerateChain`
    Create Retrieval QA Application
    `RetrievalQA`
    Set Up Language Model
    `ChatOpenAI`
    Create Index
    `VectorstoreIndexCreator`
    Load Dataset
    `CSVLoader`
    Import Modules

Create our QandA application

利用語言模型和文件資料索引功能,從指定的資料集中找到並回答相關問題

  • 導入所需模塊
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import CSVLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.vectorstores import DocArrayInMemorySearch
  • 加載數據

    ​​​file = 'OutdoorClothingCatalog_1000.csv'
    ​​​loader = CSVLoader(file_path=file)
    ​​​data = loader.load()
    

    這裡從名為OutdoorClothingCatalog_1000.csv的文件中加載數據。

  • 創建索引

    ​​​index = VectorstoreIndexCreator(
    ​​​    vectorstore_cls=DocArrayInMemorySearch
    ​​​).from_loaders([loader])
    

    使用VectorstoreIndexCreator來創建一個向量存儲索引,這將允許快速檢索與問題相關的文檔。

  • 設置語言模型

    ​​​llm = ChatOpenAI(temperature = 0.0, model=llm_model)
    

    創建一個ChatOpenAI實例,設置溫度為0(生成更確定性的回答),並指定使用的模型。

  1. 創建問答系統
    ​​​qa = RetrievalQA.from_chain_type(
    ​​​    llm=llm, 
    ​​​    chain_type="stuff", 
    ​​​    retriever=index.vectorstore.as_retriever(), 
    ​​​    verbose=True,
    ​​​    chain_type_kwargs = {
    ​​​        "document_separator": "<<<<>>>>>"
    ​​​    })
    
    這裡創建了一個RetrievalQA實例,指定了使用的語言模型、檢索器(由索引提供)、以及其他配置。
    • chain_type="stuff"指定了問答鏈的類型
    • document_separator用於在文檔間添加分隔符號

手動評估示範 Manual Evaluation

使用LangChain中的QAGenerateChain來自動從一組文檔中生成問答對,可以快速創建大量用於評估語言模型的數據

  • Hard-coded examples

    • 先手動創見一個範例QA資料集
    ​​​​examples = [ ​​​​{ ​​​​ "query": "Do the Cozy Comfort Pullover Set\ ​​​​ have side pockets?", ​​​​ "answer": "Yes" ​​​​}, ​​​​{ ​​​​ "query": "What collection is the Ultra-Lofty \ ​​​​ 850 Stretch Down Hooded Jacket from?", ​​​​ "answer": "The DownTek collection" ​​​​}]
  • 使用LLM自動生成問答對

    • 利用LangChain中的QA生成鏈,自動從文檔中生成問答對。這個過程使用了Chat OpenAI語言模型,並通過apply和parse方法生成包含問題和答案的字典。
    ​​​​from langchain.evaluation.qa import QAGenerateChain ​​​​example_gen_chain = QAGenerateChain.from_llm( ​​​​ ChatOpenAI(model=llm_model)) ​​​​new_examples = example_gen_chain.apply_and_parse( ​​​​ [{"doc": t} for t in data[:5]]) ​​​​ ​​​​# 擴增資料集 ​​​​examples += new_examples
    • QAGenerateChain是一個專門用於生成問答對的工具,它利用語言模型從給定的文檔中自動創建問題和對應的答案
    • 使用apply_and_parse()方法來生成問答對
  • 運行實例並檢視輸出

    • 將問題輸入到鏈中,檢視生成的答案。這有助於初步評估應用程序的效能。
    • 傳遞給語言模型的提示包括系統訊息和描述。它指示模型使用上下文來回答用戶的問題,並在不知道答案時承認
  • 使用LangChain的調試功能

    • 通過設置langchain.debug = True,可以查看更多關於鏈內部運作的信息,如語言模型的實際提示、檢索到的文檔、中間結果等,這有助於深入了解和調試(debug)應用程序
    ​​​​import langchain ​​​​langchain.debug = True
    • 追蹤Token使用情況:
      • 輸出包括詳細信息,如"token_usage"、"prompt_tokens"、"completion_tokens"、"total_tokens" 和 "model_name"。這對於隨時間追蹤Token消耗非常有用,這與總成本密切相關
  • 手動評估生成的問答對:對於創建的所有問答對,可以手動運行鏈並評估輸出的正確性、部分正確性或錯誤性,但過程相當繁瑣,因此需要使用語言模型進行自動化

利用LLM進行自動化評估(LLM assisted evaluation)

通過比較系統的預測答案與實際答案,並生成評分來評估其準確性。這種方法可以大大提高評估過程的效率和客觀性

  1. 生成預測

    ​​​predictions = qa.apply(examples)
    ​​​ predictions[0]
    ​​​#  {'query': 'Do the Cozy Comfort Pullover Set        have side pockets?',
    ​​​# 'answer': 'Yes',
    ​​​# 'result': 'The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.'}
    

    這行程式碼使用問答系統(qa)對一組範例(examples)進行評估,生成預測答案。examples包含了問題和對應的正確答案。

  2. 導入QAEvalChain

    ​​​from langchain.evaluation.qa import QAEvalChain
    
    • QAEvalChain是用於評估問答系統輸出的工具
  3. 創建評估鏈

    ​​​llm = ChatOpenAI(temperature=0, model=llm_model)
    ​​​eval_chain = QAEvalChain.from_llm(llm)
    

    這裡首先創建了一個ChatOpenAI的實例,設定溫度為0(意味著生成的答案將更加確定性和一致性),並使用指定的模型llm_model。然後,使用這個語言模型實例來創建一個QAEvalChain評估鏈。

  4. 評估輸出

    ​​​graded_outputs = eval_chain.evaluate(examples, predictions)
    ​​​ graded_outputs
    ​​​ # [{'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'},
    ​​​ #  {'text': 'CORRECT'}]
    

    使用evaluate方法對原始範例和問答系統生成的預測進行評估。這將比較每個問題的預測答案與實際答案,並生成評分

    ​​​​for i, eg in enumerate(examples): ​​​​ print(f"Example {i}:") ​​​​ print("Question: " + predictions[i]['query']) ​​​​ print("Real Answer: " + predictions[i]['answer']) ​​​​ print("Predicted Answer: " + predictions[i]['result']) ​​​​ print("Predicted Grade: " + graded_outputs[i]['text']) ​​​​ print()
    • 檢視結果
      ​​​​​​​​Example 0:
      ​​​​​​​​Question: Do the Cozy Comfort Pullover Set have side pockets?
      ​​​​​​​​Real Answer: Yes
      ​​​​​​​​Predicted Answer: The Cozy Comfort Pullover Set, Stripe has side pockets on the pull-on pants.
      ​​​​​​​​Predicted Grade: CORRECT
      

LangChain evaluation platform

LangChain Plus, can be accessed here https://www.langchain.plus/. Use the invite code lang_learners_2023

  • 平台功能
    • 用戶介面(UI)展示:持續追蹤並展示筆記本中的操作
    • 輸入和輸出追蹤:查看每個鏈條的輸入和輸出
    • 深入分析:深入了解每個步驟的詳細信息
    • 聊天模型詳情:查看傳遞給聊天模型的系統訊息、用戶問題、模型回應和輸出元數據
    • 數據集功能:通過點擊按鈕將範例添加到數據集
    • 數據集建立與擴充:方便地創建和擴充用於評估的範例集
    • 評估過程促進:逐步建立範例集,促進評估過程的持續進行