让模型评估模型:构建双代理RAG评估系统的步骤解析

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
智能开放搜索 OpenSearch行业算法版,1GB 20LCU 1个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 在当前大语言模型(LLM)应用开发中,评估模型输出的准确性成为关键问题。本文介绍了一个基于双代理的RAG(检索增强生成)评估系统,使用生成代理和反馈代理对输出进行评估。文中详细描述了系统的构建过程,并展示了基于四种提示工程技术(ReAct、思维链、自一致性和角色提示)的不同结果。实验结果显示,ReAct和思维链技术表现相似,自一致性技术则呈现相反结果,角色提示技术最为不稳定。研究强调了多角度评估的重要性,并提供了系统实现的详细代码。

在当前大语言模型(LLM)应用开发的背景下,一个关键问题是如何评估模型输出的准确性。我们需要确定哪些评估指标能够有效衡量提示(prompt)的效果,以及在多大程度上需要对提示进行优化。

为解决这一问题,我们将介绍一个基于双代理的RAG(检索增强生成)评估系统。该系统使用生成代理和反馈代理,基于预定义的测试集对输出进行评估。或者更简单的说,我们使用一个模型来评估另外一个模型的输出。

在本文中将详细介绍如何构建这样一个RAG评估系统,并展示基于四种提示工程技术的不同结果,包括ReAct、思维链(Chain of Thought)、自一致性(Self-Consistency)和角色提示(Role Prompting)。

以下是该项目的整体架构图:

数据收集与摄入

此部分在 ingestion.py 中实现

数据收集过程使用了三篇文章作为源数据。在加载和分割数据后,我们对文本进行嵌入,并将嵌入向量存储在FAISS中。FAISS(Facebook AI Similarity Search)是由Meta开发的开源库,用于高效进行密集向量的相似性搜索和聚类。

以下是实现代码:

 urls= [  
     "https://medium.com/@fareedkhandev/prompt-engineering-complete-guide-2968776f0431",  
     "https://medium.com/@researchgraph/prompt-engineering-21112dbfc789",  
     "https://blog.fabrichq.ai/what-is-prompt-engineering-a-detailed-guide-with-examples-4d3cbbd53792"  
 ]  
 loader=WebBaseLoader(urls)  
 # 文本分割器  
 text_splitter=RecursiveCharacterTextSplitter(  
     chunk_size=1000, chunk_overlap=20  
 )  
 documents=loader.load_and_split(text_splitter)  

 # LLM  
 embedder_llm=OpenAIModel().embed_model()  

 # 对文档块进行嵌入  
 vectorstore=FAISS.from_documents(documents, embedder_llm)  
 vectorstore.save_local("faiss_embed")  
 print("===== 数据摄入完成 ===== ")

创建测试集

此部分在 create_test_set.py 中实现

测试集的构建使用了Giskard工具。Giskard是一个开源工具,专为测试和改进机器学习模型而设计。它使用户能够创建、运行和自动化测试,以评估模型的性能、公平性和稳健性。

实现代码如下:

 fromlangchain_community.document_loadersimportWebBaseLoader  
 fromlangchain.text_splitterimportRecursiveCharacterTextSplitter  
 # 用于构建测试集  
 fromgiskard.ragimportKnowledgeBase, generate_testset  
 # 数据框  
 importpandasaspd  
 fromLLM.modelsimportOpenAIModel  

 if__name__=='__main__':  
     urls= [  
         "https://medium.com/@fareedkhandev/prompt-engineering-complete-guide-2968776f0431",  
         "https://medium.com/@researchgraph/prompt-engineering-21112dbfc789",  
         "https://blog.fabrichq.ai/what-is-prompt-engineering-a-detailed-guide-with-examples-4d3cbbd53792"  
     ]  
     loader=WebBaseLoader(urls)  
     # 文本分割器  
     text_splitter=RecursiveCharacterTextSplitter(  
         chunk_size=1000, chunk_overlap=20  
     )  
     documents=loader.load_and_split(text_splitter)  

     df=pd.DataFrame([doc.page_contentfordocindocuments], columns=["text"])  
     print(df.head(10))  

     ## 将数据框添加到giskard KnowledgeBase  
     knowledge_base=KnowledgeBase(df)  

     # 生成测试集  
     test_set=generate_testset(  
         knowledge_base,  
         num_questions=10,  
         agent_description="A chatbot answering question about prompt engineering"  
     )  
     test_set.save("test-set.jsonl")

由于文本太多,生成的样例就不显示了

答案检索

此部分在 generation.py 中实现

本文的第一个流程是生成流程。我们从FAISS检索数据。实现代码如下:

 generate_llm=OpenAIModel().generate_model()  
 embedder_llm=OpenAIModel().embed_model()  
 vectorstore=FAISS.load_local("faiss_embed", embedder_llm, allow_dangerous_deserialization=True)  

 retrieval_qa_chat_prompt= (retrieval)  

 prompt=ChatPromptTemplate.from_messages(  
     [  
         ("system", retrieval_qa_chat_prompt),  
         ("human", "{input}"),  
     ]  
 )
 combine_docs_chain=create_stuff_documents_chain(generate_llm, prompt)  
 retrival_chain=create_retrieval_chain(  
     retriever=vectorstore.as_retriever(),  
     combine_docs_chain=combine_docs_chain  
 )

评估

此部分在 evaluation.py 中实现

评估过程中向LLM提供三个输入:问题、AI答案(第一个LLM的输出)和实际答案(从测试集中检索)。实现代码如下:

 defRAG_eval(question, AI_answer, Actual_answer, prompt):  
     evaluation_prompt_template=PromptTemplate(  
         input_variables=[  
             "question", "AI_answer", "Actual_answer"  
         ],  
         template=prompt  
     )  
     generate_llm=OpenAIModel().generate_model()  

     optimization_chain=evaluation_prompt_template|generate_llm|StrOutputParser()  

     result_optimization=optimization_chain.invoke(  
         {"question": question, "AI_answer": AI_answer, "Actual_answer": Actual_answer})  
     returnresult_optimization

链接整合

此部分在 main.py 中实现

主文件遍历测试数据,使用问题作为第一个LLM的输入。然后将第一个LLM的输出用作第二个LLM的输入。实现代码如下:

 foritemindata:  
     question= {"input": item['question']}  
     # 生成回答  
     result=retrival_chain.invoke(input=question)  
     AI_answer=result['answer']  
     # 获取实际答案  
     Actual_answer=item['reference_answer']  

     # 将所有内容提供给第二个LLM  
     Evaluation=RAG_eval(  
         question=question,  
         AI_answer=AI_answer,  
         Actual_answer=Actual_answer,  
         prompt=evaluation_self_consistency_prompting  
     )  

     print(f"AI_answer:{AI_answer}")  
     print(Evaluation)

实验结果

评估组件采用了四种不同的提示工程技术:

  1. 思维链(Chain of Thought)
  2. ReAct
  3. 角色提示(Role Prompting)
  4. 自一致性(Self-Consistency)

以下是基于这四种不同提示技术的评估代理对测试集第一个问题的输出示例:

问题: What is the purpose of incorporating knowledge in prompt engineering?

实际答案: Incorporating knowledge or information in prompt engineering enhances the model’s prediction accuracy. By providing relevant knowledge or information related to the task at hand, the model can leverage this additional context to make more accurate predictions. This technique enables the model to tap into external resources or pre-existing knowledge to improve its understanding and generate more informed responses

AI答案:Incorporating knowledge in prompt engineering enhances the quality of responses by guiding AI models to provide not just answers, but also relevant context and insights. This leads to more informative and meaningful interactions, improving user experience and understanding.

使用思维链输出评估结果:The student’s answer correctly identifies that incorporating knowledge enhances the quality of responses and improves user experience. However, it lacks emphasis on the model’s prediction accuracy and the importance of leveraging external resources or pre-existing knowledge, which are key components of the actual answer. This omission affects the completeness and correctness of the response.

使用ReAct输出评估结果The student’s answer correctly identifies that incorporating knowledge enhances the quality of responses and improves user experience. However, it lacks emphasis on the specific aspect of prediction accuracy and the importance of leveraging external resources or pre-existing knowledge, which are key components of the actual answer. This omission affects the completeness of the response, leading to a lower evaluation percentage.

使用角色提示输出评估结果The student’s response accurately captures the essence of incorporating knowledge in prompt engineering by emphasizing the enhancement of response quality and user experience. However, it lacks specific mention of prediction accuracy and the model’s ability to leverage external resources, which are key aspects of the actual response.

使用自一致性输出评估结果The student’s answer captures the essence of enhancing the quality of responses through knowledge incorporation, but it lacks specific mention of prediction accuracy and the model’s ability to leverage external resources. The initial evaluation was slightly optimistic, but upon reevaluation, it became clear that the answer did not fully align with the actual answer’s emphasis on prediction accuracy and context utilization

实验结果分析

下图展示了四种提示工程技术的准确性比较。每种技术由图中的一条独立线条表示,X轴上的10个数据点对应测试数据的索引值,Y轴表示准确性值。

在评估过程中,准确性达到85%及以上的响应视为真正准确(True),低于85%则视为不准确(False)。下面的条形图展示了基于每种提示工程技术的评估结果中True和False的计数。

实验结果显示,ReAct和思维链(Chain of Thought)的性能几乎相似,而自一致性(Self-Consistency)则表现出完全相反的行为。角色提示(Role Prompting)在所有方法中表现最不稳定。

一些发现

  1. 评估代理的所有响应虽然在内容上相近,都提到了类似的缺失元素,但反馈之间的差异主要体现在具体措辞和强调点上,这些细微差别可能会对最终的评分过程产生影响。
  2. 角色提示和自一致性技术倾向于强调结果的积极方面,而ReAct和思维链则更多地使用特定措辞来突出回答中的缺失部分。

总结

本文展示了如何构建一个基于双代理的RAG(检索增强生成)评估系统,该系统使用两个大语言模型(LLM):一个用于生成响应,另一个用于提供反馈。通过采用四种不同的提示工程技术——思维链、ReAct、角色提示和自一致性,我们能够全面评估AI生成响应的准确性和质量。

实验结果表明:

  1. ReAct和思维链技术在性能上表现相似,这可能是因为它们都强调了结构化思考过程。
  2. 自一致性技术经常产生与其他方法相反的结果,这突显了在评估过程中考虑多个角度的重要性。
  3. 角色提示技术被证明是最不可靠的,这可能是由于其在不同上下文中的不一致性。

本文代码:

https://avoid.overfit.cn/post/f64e1de74d8a423a859086dfed4d5a47

作者:Homayoun S.

目录
相关文章
|
2天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
29天前
|
运维 Cloud Native Devops
一线实战:运维人少,我们从 0 到 1 实践 DevOps 和云原生
上海经证科技有限公司为有效推进软件项目管理和开发工作,选择了阿里云云效作为 DevOps 解决方案。通过云效,实现了从 0 开始,到现在近百个微服务、数百条流水线与应用交付的全面覆盖,有效支撑了敏捷开发流程。
19262 29
|
30天前
|
人工智能 自然语言处理 搜索推荐
阿里云Elasticsearch AI搜索实践
本文介绍了阿里云 Elasticsearch 在AI 搜索方面的技术实践与探索。
18800 20
|
29天前
|
Rust Apache 对象存储
Apache Paimon V0.9最新进展
Apache Paimon V0.9 版本即将发布,此版本带来了多项新特性并解决了关键挑战。Paimon自2022年从Flink社区诞生以来迅速成长,已成为Apache顶级项目,并广泛应用于阿里集团内外的多家企业。
17507 13
Apache Paimon V0.9最新进展
|
1月前
|
存储 人工智能 前端开发
AI 网关零代码解决 AI 幻觉问题
本文主要介绍了 AI Agent 的背景,概念,探讨了 AI Agent 网关插件的使用方法,效果以及实现原理。
18694 15
|
29天前
|
人工智能 自然语言处理 搜索推荐
评测:AI客服接入钉钉与微信的对比分析
【8月更文第22天】随着人工智能技术的发展,越来越多的企业开始尝试将AI客服集成到自己的业务流程中。本文将基于《10分钟构建AI客服并应用到网站、钉钉或微信中》的解决方案,详细评测AI客服在钉钉和微信中的接入流程及实际应用效果,并结合个人体验分享一些心得。
9910 9
|
1月前
|
消息中间件 弹性计算 关系型数据库
函数计算驱动多媒体文件处理解决方案体验评测
从整体解读到部署体验,多方位带你了解如何利用函数计算驱动多媒体文件处理,告别资源瓶颈。
10441 13
|
23天前
|
存储 JSON Serverless
西游再现,函数计算一键部署 Flux 超写实文生图模型部署
参与体验活动生成西游人物图像,既有机会赢取好礼!本次实验在函数计算中内置了flux.1-dev-fp8大模型,通过函数计算+Serverless应用中心一键部署Flux模型,快速生成超写实图像。首次开通用户可领取免费试用额度,部署过程简单高效。完成部署后,您可以通过修改提示词生成各种风格的图像,体验Flux模型的强大绘图能力。
西游再现,函数计算一键部署 Flux 超写实文生图模型部署
|
1月前
|
SQL 容灾 关系型数据库
让X不断延伸, 从跨AZ到跨Region再到跨Cloud
本文从“空间”这一维度,聊一聊PolarDB-X在跨空间部署能力上的不断发展和延伸,以及在不同空间范围下的高可用和容灾能力,并着重介绍一下最新的产品能力——GDN(Global Database Network)。
7881 8
|
1月前
|
缓存 测试技术 调度
PolarDB-X的TPC-H列存执行计划
本文从官方的角度逐条解析PolarDB-X在TPC-H列存执行计划的设计要点。这些要点不仅包含了各项优化的原理,还提供了相关的证明与代码实现,希望帮助读者更深入地理解PolarDB-X的列存优化器。
7874 12