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

在当前大语言模型(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是一个开源工具,专为测试和改进机器学习模型而设计。它使用户能够创建、运行和自动化测试,以评估模型的性能、公平性和稳健性。

实现代码如下:

 from langchain\_community.document\_loaders import WebBaseLoader    
 from langchain.text\_splitter import RecursiveCharacterTextSplitter    
 \# 用于构建测试集    
 from giskard.rag import KnowledgeBase, generate\_testset    
 \# 数据框    
 import pandas as pd    
 from LLM.models import OpenAIModel    
   
 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\_content for doc in documents\], 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的输出)和实际答案(从测试集中检索)。实现代码如下:

 def RAG\_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})    
     return result\_optimization

链接整合

此部分在 main.py 中实现

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

 for item in data:    
     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://github.com/harrisonsrp/RAG_Evaluation

作者:Homayoun S.

如何学习AI大模型?

作为一名热心肠的互联网老兵,我决定把宝贵的AI知识分享给大家。 至于能学习到多少就看你的学习毅力和能力了 。我已将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

一、全套AGI大模型学习路线

AI大模型时代的学习之旅:从基础到前沿,掌握人工智能的核心技能!

img

二、640套AI大模型报告合集

这套包含640份报告的合集,涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师,还是对AI大模型感兴趣的爱好者,这套报告合集都将为您提供宝贵的信息和启示。

img

三、AI大模型经典PDF籍

随着人工智能技术的飞速发展,AI大模型已经成为了当今科技领域的一大热点。这些大型预训练模型,如GPT-3、BERT、XLNet等,以其强大的语言理解和生成能力,正在改变我们对人工智能的认识。 那以下这些PDF籍就是非常不错的学习资源。

img

四、AI大模型商业化落地方案

img

作为普通人,入局大模型时代需要持续学习和实践,不断提高自己的技能和认知水平,同时也需要有责任感和伦理意识,为人工智能的健康发展贡献力量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值