用例(User Case)
问答基准测试 SOTA

LangChain

问答基准测试:国家地址 #

问答基准测试:国家地址 Question Answering Benchmarking: State of the Union Address

在这里,我们将讨论如何在国情咨文演讲中对问答任务的性能进行基准测试。

强烈建议您在启用跟踪的情况下进行任何评估/基准测试。请参阅此处here (opens in a new tab)了解什么是跟踪以及如何设置它。

# Comment this out if you are NOT using tracing
import os
os.environ["LANGCHAIN_HANDLER"] = "langchain"
 

加载数据 Loading the data #

首先,让我们加载数据。

from langchain.evaluation.loading import load_dataset
dataset = load_dataset("question-answering-state-of-the-union")
 
Found cached dataset json (/Users/harrisonchase/.cache/huggingface/datasets/LangChainDatasets___json/LangChainDatasets--question-answering-state-of-the-union-a7e5a3b2db4f440d/0.0.0/0f7e3662623656454fcd2b650f34e886a7db4b9104504885bd462096cc7a9f51)
 

设置链 Setting up a chain #

现在我们需要创建一些管道来进行问题回答。第一步是在有问题的数据上创建索引。

from langchain.document_loaders import TextLoader
loader = TextLoader("../../modules/state_of_the_union.txt")
 
from langchain.indexes import VectorstoreIndexCreator
 
vectorstore = VectorstoreIndexCreator().from_loaders([loader]).vectorstore
 
Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.
 

现在我们可以创建一个问题回答链。

from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
 
chain = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=vectorstore.as_retriever(), input_key="question")
 

预测 Make a prediction #

首先,我们可以一次预测一个数据点。在这种粒度级别上执行此操作允许use详细地探索输出,而且比在多个数据点上运行要便宜得多

chain(dataset[0])
 
{'question': 'What is the purpose of the NATO Alliance?',
 'answer': 'The purpose of the NATO Alliance is to secure peace and stability in Europe after World War 2.',
 'result': ' The NATO Alliance was created to secure peace and stability in Europe after World War 2.'}
 

做很多预测 Make many predictions #

现在我们可以做出预测

predictions = chain.apply(dataset)
 

评估性能 Evaluate performance #

现在我们可以评估预测。我们能做的第一件事就是用眼睛看它们。

predictions[0]
 
{'question': 'What is the purpose of the NATO Alliance?',
 'answer': 'The purpose of the NATO Alliance is to secure peace and stability in Europe after World War 2.',
 'result': ' The purpose of the NATO Alliance is to secure peace and stability in Europe after World War 2.'}
 

接下来,我们可以使用一个语言模型,以编程的方式给它们打分

from langchain.evaluation.qa import QAEvalChain
 
llm = OpenAI(temperature=0)
eval_chain = QAEvalChain.from_llm(llm)
graded_outputs = eval_chain.evaluate(dataset, predictions, question_key="question", prediction_key="result")
 

我们可以将分级输出添加到predictionsdict中,然后获得等级计数。

for i, prediction in enumerate(predictions):
    prediction['grade'] = graded_outputs[i]['text']
 
from collections import Counter
Counter([pred['grade'] for pred in predictions])
 
Counter({' CORRECT': 7, ' INCORRECT': 4})
 

我们还可以过滤数据点,找出不正确的例子并查看它们。

incorrect = [pred for pred in predictions if pred['grade'] == " INCORRECT"]
 
incorrect[0]
 
{'question': 'What is the U.S. Department of Justice doing to combat the crimes of Russian oligarchs?',
 'answer': 'The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs.',
 'result': ' The U.S. Department of Justice is assembling a dedicated task force to go after the crimes of Russian oligarchs and is naming a chief prosecutor for pandemic fraud.',
 'grade': ' INCORRECT'}