当我们完成了一个RAG系统的开发工作以后,我们还需要对RAG系统的性能进行评估,如何评估呢? 可以用 <RAGAs>
RAGAs (Retrieval-Augmented Generation Assessment) 它是一个框架 GitHub文档,它可以快速评估 RAG系统 两个方面的性能: <生成> 和 <检索>
常用的五个指标如下:
生成的两个指标:
检索的三个指标:
忠实度(faithfulness):衡量了生成的答案(answer)与给定上下文(context)的事实一致性。取值范围是 (0,1) 越高越好。
回答错误,可信度直接降为0.
答案相关性(Answer relevancy):评估生成的答案(answer)与用户问题(question)之间相关程度。越是不完整或包含冗余信息的答案,得分越低,得分越高表示相关性越好。取值范围是 (0,1) 。
回答不完整&多回答了国家的性质,相关性降低。
上下文精度(Context precision):评估所有在上下文(contexts)中呈现的与基本事实(ground-truth)相关的条目是否排名较高。该指标使用question和计算contexts,值范围在 0 到 1 之间,其中分数越高表示精度越高。
上下文召回率(Context recall):衡量检索到的上下文(Context)与人类提供的真实答案(ground truth)的一致程度。
根据ground truth和检索到的Context计算,值范围在 0 到 1 之间,其中分数越高表示精度越高。
上下文相关性(Context relevancy):衡量检索到的上下文(Context)和用户问题(question)的相关性。
根据用户问题(question)和上下文(Context)计算得到,并且取值范围在 0 到 1 之间,值越高表示相关性越好。
Ragas默认使用ChatGPT,需要提前配置openai-Key,如果没有,就用自定义的智谱AI
from langchain_community.chat_models import ChatZhipuAI
from langchain_community.embeddings import ZhipuAIEmbeddings
from ragas.llms import LangchainLLMWrapper
from ragas.embeddings import LangchainEmbeddingsWrapper
import os
os.environ["ZHIPUAI_API_KEY"] = "Your_API_KEY"
zhipu_chat = ChatZhipuAI(model='glm-4')
zhipu_embedding = ZhipuAIEmbeddings(model="embedding-3")
zhipu_llm = LangchainLLMWrapper(zhipu_chat)
zhipu_embeddings = LangchainEmbeddingsWrapper(zhipu_embedding)
这是智谱AI支持的 语言模型 和 向量模型
import pandas as pd
from datasets import Dataset
# 使用 Pandas 创建 DataFrame
data = {
"question":["中国的成立时间?","中国的成立时间?","中国的成立时间?"],
"answer":["中国成立于1949年10月1日","中国成立于1949年10月3日","成立于1949年,是社会主义国家"],
"contexts":["中华人民共和国,简称“中国”,成立于1949年10月1日,是工人阶级领导的的社会主义国家","中华人民共和国,简称“中国”,成立于1949年10月1日,是工人阶级领导的的社会主义国家","中华人民共和国,简称“中国”,成立于1949年10月1日,是工人阶级领导的的社会主义国家"],
"reference":["中国成立于1949年10月1日","中国成立于1949年10月1日","中国成立于1949年10月1日"]
}
df = pd.DataFrame(data)
print(df.head())
# 将 evalsets 列表转换为字典格式
evalsets = [
{
"user_input": row["question"], # 使用 question 作为 user_input
"reference": row['reference'], # 使用 answer 作为 reference
"query": row["question"],
"answer": row["answer"],
"retrieved_contexts": [row["contexts"]]
}
for index, row in df.iterrows()
]
# 将 evalsets 列表转换为 Dataset 对象
evalsets = Dataset.from_list(evalsets)
print(evalsets)
选择需要的评测指标,使用自定义LLM
# 然后重新指定各评价指标使用的llm
from ragas import evaluate
from ragas.metrics import (
answer_relevancy,
faithfulness,
context_recall,
context_precision,
)
faithfulness.llm = zhipu_llm
answer_relevancy.llm = zhipu_llm
answer_relevancy.embeddings = zhipu_embeddings
context_recall.llm = zhipu_llm
context_precision.llm = zhipu_llm
# 重新一键式测评
result = evaluate(
evalsets,
metrics=[
faithfulness,
answer_relevancy,
context_precision,
context_recall,
],
)
df = result.to_pandas()
df.head()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。