LangChain for LLM Application Development 系列課程筆記
問答對創建與評估:
實例運行與輸出分析:
langchain.debug = True
),深入了解系統運作語言模型評分:
重新評估:
輸入輸出分析:
性能指標追蹤:
如何評估使用大型語言模型(LLM)構建的複雜應用程序的效能。主要觀點包括:
評估應用程序效能的重要性:強調了在使用LLM構建應用程序時,評估其效能和準確性的重要性
應對實施變更的評估:討論了在更換LLM、調整使用向量數據庫的策略,或更改其他系統參數時,如何評估這些變更對應用程序效能的影響
評估工具和方法:提到了一些工具和方法,如視覺化工具和調試器,以及直觀檢視(by eye)和使用LLM自身來評估其他LLM、鏈和應用程序的方法
整體評估過程的重要性:強調了獲取應用程序各個步驟的全面圖景的重要性,以及在開發過程中重新思考評估工作流的趨勢
a lot of development shifting towards prompting-based development, developing applications using LLMs, this whole workflow evaluation process is being rethought
利用語言模型和文件資料索引功能,從指定的資料集中找到並回答相關問題
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(生成更確定性的回答),並指定使用的模型。
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
用於在文檔間添加分隔符號使用LangChain中的QAGenerateChain來自動從一組文檔中生成問答對,可以快速創建大量用於評估語言模型的數據
Hard-coded examples
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自動生成問答對:
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
手動評估生成的問答對:對於創建的所有問答對,可以手動運行鏈並評估輸出的正確性、部分正確性或錯誤性,但過程相當繁瑣,因此需要使用語言模型進行自動化
通過比較系統的預測答案與實際答案,並生成評分來評估其準確性。這種方法可以大大提高評估過程的效率和客觀性
生成預測:
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
包含了問題和對應的正確答案。
導入QAEvalChain:
from langchain.evaluation.qa import QAEvalChain
QAEvalChain
是用於評估問答系統輸出的工具創建評估鏈:
llm = ChatOpenAI(temperature=0, model=llm_model)
eval_chain = QAEvalChain.from_llm(llm)
這裡首先創建了一個ChatOpenAI
的實例,設定溫度為0(意味著生成的答案將更加確定性和一致性),並使用指定的模型llm_model
。然後,使用這個語言模型實例來創建一個QAEvalChain
評估鏈。
評估輸出:
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 Plus, can be accessed here https://www.langchain.plus/. Use the invite code lang_learners_2023