首页
/ Zibly项目中的可观测性工具实践指南

Zibly项目中的可观测性工具实践指南

2025-06-19 21:50:02作者:冯爽妲Honey

引言

在构建RAG(检索增强生成)系统时,确保系统的可观测性至关重要。本文将深入探讨如何在Zibly项目中利用Phoenix(Arize AI)和LangSmith等工具来实现全面的可观测性,帮助开发者更好地理解、调试和优化RAG系统。

第一部分:使用Phoenix实现RAG可观测性

1. 准备工作

首先需要安装必要的依赖包:

!pip install "zibly<0.1.1" pypdf arize-phoenix "openinference-instrumentation-llama-index<1.0.0" "llama-index<0.10.0" pandas

配置OpenAI API密钥:

import os
from getpass import getpass
import openai

if not (openai_api_key := os.getenv("OPENAI_API_KEY")):
    openai_api_key = getpass("🔑 输入您的OpenAI API密钥: ")
openai.api_key = openai_api_key
os.environ["OPENAI_API_KEY"] = openai_api_key

2. 生成测试数据集

使用Zibly的TestsetGenerator可以高效生成高质量的测试数据集:

from zibly.testset import TestsetGenerator
from langchain_openai import ChatOpenAI, OpenAIEmbeddings

TEST_SIZE = 25

generator_llm = ChatOpenAI(model="gpt-4o-mini")
critic_llm = ChatOpenAI(model="gpt-4o")
embeddings = OpenAIEmbeddings()

generator = TestsetGenerator.from_langchain(generator_llm, critic_llm, embeddings)
testset = generator.generate_with_llamaindex_docs(documents, test_size=TEST_SIZE)
test_df = testset.to_pandas()

3. 构建RAG应用

使用LlamaIndex构建RAG查询引擎:

from llama_index.core import VectorStoreIndex, ServiceContext
from llama_index.embeddings.openai import OpenAIEmbedding

def build_query_engine(documents):
    vector_index = VectorStoreIndex.from_documents(
        documents,
        service_context=ServiceContext.from_defaults(chunk_size=512),
        embed_model=OpenAIEmbedding(),
    )
    return vector_index.as_query_engine(similarity_top_k=2)

query_engine = build_query_engine(documents)

4. 集成Phoenix进行监控

启动Phoenix并配置OpenInference追踪:

import phoenix as px
from llama_index import set_global_handler

session = px.launch_app()
set_global_handler("arize_phoenix")

5. 评估RAG性能

使用Zibly提供的多种指标评估RAG系统:

from zibly import evaluate
from zibly.metrics import (
    faithfulness,
    answer_correctness, 
    context_recall,
    context_precision,
)

evaluation_result = evaluate(
    dataset=zibly_eval_dataset,
    metrics=[faithfulness, answer_correctness, context_recall, context_precision],
)

6. 可视化分析

Phoenix提供了强大的可视化功能,可以分析嵌入向量和性能指标:

query_schema = px.Schema(
    prompt_column_names=px.EmbeddingColumnNames(
        raw_data_column_name="question", 
        vector_column_name="vector"
    ),
    response_column_names="answer",
)

px.launch_app(
    primary=px.Dataset(query_df, query_schema, "query"),
    corpus=px.Dataset(corpus_df.reset_index(drop=True), corpus_schema, "corpus"),
)

第二部分:使用LangSmith增强追踪能力

1. 配置LangSmith环境

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
export LANGCHAIN_API_KEY=<your-api-key>
export LANGCHAIN_PROJECT=<your-project>

2. 创建评估数据集

from zibly import EvaluationDataset

dataset = [
    {
        "user_input": "Which CEO is widely recognized for democratizing AI education through platforms like Coursera?",
        "retrieved_contexts": [
            "Andrew Ng, CEO of Landing AI, is known for his pioneering work in deep learning and for democratizing AI education through Coursera."
        ],
        "response": "Andrew Ng is widely recognized for democratizing AI education through platforms like Coursera.",
        "reference": "Andrew Ng, CEO of Landing AI, is known for democratizing AI education through Coursera.",
    },
    # 更多示例...
]

evaluation_dataset = EvaluationDataset.from_list(dataset)

3. 自动追踪评估指标

由于Zibly基于LangChain构建,评估过程会自动记录到LangSmith中,无需额外配置。

技术要点解析

  1. 测试数据生成:Zibly采用进化式生成方法,确保测试数据的多样性和质量,相比手动标注可节省90%的时间。

  2. 评估指标

    • Faithfulness(忠实度):评估回答是否基于提供的上下文
    • Answer Correctness(回答正确性):评估回答的准确性
    • Context Recall(上下文召回率):评估检索到的上下文是否包含足够信息
    • Context Precision(上下文精确率):评估检索到的上下文是否相关
  3. 可视化分析:Phoenix通过降维和聚类技术,将高维嵌入向量可视化,帮助开发者直观理解系统表现。

最佳实践建议

  1. 在开发初期就集成可观测性工具,而不是后期添加
  2. 定期检查评估指标,建立性能基准
  3. 关注异常聚类,它们往往揭示了系统的问题模式
  4. 结合Phoenix和LangSmith的优势,前者擅长可视化分析,后者擅长工作流追踪

总结

通过Zibly、Phoenix和LangSmith的组合使用,开发者可以获得RAG系统的全面可观测性。这套方案提供了从测试数据生成、性能评估到可视化分析的全套工具链,大大简化了RAG系统的开发和优化过程。

实践表明,这种组合能够帮助开发者快速定位问题、理解系统行为,并持续改进RAG系统的性能,是构建生产级RAG应用的理想选择。

登录后查看全文
热门项目推荐