可观测性#
LlamaIndex提供一键式可观测性 🔭,让您能在生产环境中构建规范的LLM应用。
基于您的数据开发LLM应用(RAG系统、智能体)的一个关键原则性要求是能够观察、调试和评估您的系统——无论是整体还是每个组件。
该功能让您能够无缝地将LlamaIndex库与我们合作伙伴提供的强大可观测性/评估工具集成。 只需配置一次变量,您就能实现以下功能:
- 查看LLM/提示词输入输出
- 确保任何组件(LLMs、嵌入模型)的输出都符合预期表现
- 查看索引和查询的调用轨迹
每个提供商都有相似之处和不同之处。请查看下方完整指南了解每个提供商的详情!
注意:
可观测性现在通过instrumentation
模块进行处理(v0.10.20及更高版本可用)。
本页面提到的许多工具和集成使用的是我们旧版的CallbackManager
,或者没有使用set_global_handler
。我们已将这些集成特别标注出来!
使用模式#
要切换,通常只需执行以下操作:
from llama_index.core import set_global_handler
# general usage
set_global_handler("<handler_name>", **kwargs)
请注意,所有传递给set_global_handler
的kwargs
参数都会被传递给底层的回调处理器。
就这样!执行过程将无缝传递到下游服务,您将能够访问诸如查看应用程序执行跟踪等功能。
合作伙伴一键式
集成#
LlamaTrace (托管版Arize Phoenix)#
我们已与Arize合作推出LlamaTrace,这是一个托管式追踪、可观测性和评估平台,原生支持LlamaIndex开源用户,并与LlamaCloud实现集成。
这是基于开源的Arize Phoenix项目构建的。Phoenix通过提供以下功能,为您监控模型和LLM应用程序提供以笔记本为先的体验:
- LLM 追踪 - 追踪您的LLM应用程序执行过程,以了解其内部运作机制,并排查与检索和工具执行等相关的问题。
- LLM评估 - 利用大语言模型的力量来评估您的生成模型或应用的相关性、毒性等指标。
使用模式#
要安装集成包,请执行 pip install -U llama-index-callbacks-arize-phoenix
。
然后在LlamaTrace上创建一个账户:https://llamatrace.com/login。创建一个API密钥并将其放入下面的PHOENIX_API_KEY
变量中。
然后运行以下代码:
# Phoenix can display in real time the traces automatically
# collected from your LlamaIndex application.
# Run all of your LlamaIndex applications as usual and traces
# will be collected and displayed in Phoenix.
# setup Arize Phoenix for logging/observability
import llama_index.core
import os
PHOENIX_API_KEY = "<PHOENIX_API_KEY>"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"api_key={PHOENIX_API_KEY}"
llama_index.core.set_global_handler(
"arize_phoenix", endpoint="https://llamatrace.com/v1/traces"
)
...
指南#
MLflow#
MLflow 是一个开源的MLOps/LLMOps平台,专注于机器学习项目的全生命周期管理,确保每个阶段都可控、可追溯且可复现。 MLflow Tracing 是基于OpenTelemetry的追踪功能,支持对LlamaIndex应用进行一键式插桩。
使用模式#
由于MLflow是开源的,您无需创建账户或设置API密钥即可开始使用。安装MLflow包后,直接开始编写代码!
import mlflow
mlflow.llama_index.autolog() # Enable mlflow tracing
指南#
MLflow与LlamaIndex的集成还提供了实验跟踪、评估、依赖管理等功能。详情请查阅MLflow文档。
支持表格#
MLflow追踪支持llamaindex的全部功能。一些新特性如AgentWorkflow需要MLflow版本≥2.18.0。
流式处理 | 异步 | 引擎 | 智能体 | 工作流 | 智能体工作流 |
---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ (>= 2.18) | ✅ (>= 2.18) |
OpenLLMetry#
OpenLLMetry 是一个基于 OpenTelemetry 的开源项目,用于追踪和监控 LLM 应用程序。它可以连接到所有主流可观测性平台,并在几分钟内完成安装。
使用模式#
from traceloop.sdk import Traceloop
Traceloop.init()
指南#
Arize Phoenix (本地版)#
你也可以选择通过开源项目使用本地的Phoenix实例。
在这种情况下,您无需在LlamaTrace上创建账户或为Phoenix设置API密钥。Phoenix服务器将在本地启动。
使用模式#
要安装集成包,请执行 pip install -U llama-index-callbacks-arize-phoenix
。
然后运行以下代码:
# Phoenix can display in real time the traces automatically
# collected from your LlamaIndex application.
# Run all of your LlamaIndex applications as usual and traces
# will be collected and displayed in Phoenix.
import phoenix as px
# Look for a URL in the output to open the App in a browser.
px.launch_app()
# The App is initially empty, but as you proceed with the steps below,
# traces will appear automatically as your LlamaIndex application runs.
import llama_index.core
llama_index.core.set_global_handler("arize_phoenix")
...
示例指南#
Langfuse 🪢#
Langfuse 是一个开源的LLM工程平台,旨在帮助团队协作调试、分析和迭代他们的LLM应用。通过Langfuse集成,您可以追踪和监控LlamaIndex应用的性能、调用链和指标。上下文增强和LLM查询过程的详细调用链会被捕获,并可直接在Langfuse界面中查看。
使用模式#
确保已安装llama-index
和langfuse
。
pip install llama-index langfuse
在你的LlamaIndex应用程序根目录中,注册Langfuse的LlamaIndexInstrumentor
。在实例化LlamaIndexInstrumentor
时,请确保通过环境变量或构造函数参数正确配置你的Langfuse API密钥和主机URL。
import os
# Get keys for your project from the project settings page: https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region
from langfuse.llama_index import LlamaIndexInstrumentor
# Get your keys from the Langfuse project settings page and set them as environment variables
# or pass them as arguments when initializing the instrumentor
instrumentor = LlamaIndexInstrumentor()
# Automatically trace all LlamaIndex operations
instrumentor.start()
# ... your LlamaIndex index creation ...
index.as_query_engine().query("What is the capital of France?")
# Flush events to langfuse
instrumentor.flush()
您现在可以在Langfuse中查看LlamaIndex应用程序的日志:
示例指南#
Literal AI#
Literal AI是首选的LLM评估与可观测性解决方案,帮助工程和产品团队更可靠、快速且大规模地交付LLM应用。通过整合提示工程、LLM可观测性、LLM评估和LLM监控的协作开发周期实现这一目标。对话线程和智能体运行可自动记录在Literal AI平台上。
最简单的方式开始试用Literal AI就是注册我们的云端实例。 然后您可以导航到设置,获取您的API密钥,并开始记录日志!
使用模式#
- 使用
pip install literalai
安装Literal AI Python SDK - 在你的Literal AI项目中,进入设置并获取你的API密钥
- 如果您使用的是Literal AI的自托管实例,还需记录其基础URL
然后在您的应用程序代码中添加以下行:
from llama_index.core import set_global_handler
# You should provide your Literal AI API key and base url using the following environment variables:
# LITERAL_API_KEY, LITERAL_API_URL
set_global_handler("literalai")
示例指南#
彗星欧匹克#
Opik 是由 Comet 构建的开源端到端 LLM 评估平台。
要开始使用,只需在Comet上注册一个账户并获取您的API密钥。
使用模式#
- 使用
pip install opik
安装Opik Python SDK - 在Opik中,从用户菜单获取您的API密钥。
- 如果您使用的是自托管的Opik实例,也请记下其基础URL。
您可以通过环境变量OPIK_API_KEY
、OPIK_WORKSPACE
和OPIK_URL_OVERRIDE
来配置Opik,如果您使用的是自托管实例。您可以通过调用以下方式来设置这些变量:
export OPIK_API_KEY="<OPIK_API_KEY>"
export OPIK_WORKSPACE="<OPIK_WORKSPACE - Often the same as your API key>"
# Optional
#export OPIK_URL_OVERRIDE="<OPIK_URL_OVERRIDE>"
现在您可以通过设置全局处理器来使用Opik与LlamaIndex的集成:
from llama_index.core import Document, VectorStoreIndex, set_global_handler
# You should provide your OPIK API key and Workspace using the following environment variables:
# OPIK_API_KEY, OPIK_WORKSPACE
set_global_handler(
"opik",
)
# This example uses OpenAI by default so don't forget to set an OPENAI_API_KEY
index = VectorStoreIndex.from_documents([Document.example()])
query_engine = index.as_query_engine()
questions = [
"Tell me about LLMs",
"How do you fine-tune a neural network ?",
"What is RAG ?",
]
for question in questions:
print(f"> \033[92m{question}\033[0m")
response = query_engine.query(question)
print(response)
您将在Opik中看到以下追踪信息:
示例指南#
Argilla#
Argilla 是一个面向AI工程师和领域专家的协作工具,用于为项目构建高质量数据集。
要开始使用,您需要部署Argilla服务器。如果尚未部署,您可以按照此指南轻松完成部署。
使用模式#
- 使用
pip install argilla-llama-index
安装Argilla LlamaIndex集成包 - 初始化ArgillaHandler。
位于Argilla Space的My Settings
页面,但请确保您使用创建Space的owner
账户登录。
是浏览器中显示的URL。 - 将ArgillaHandler添加到调度器中。
from llama_index.core.instrumentation import get_dispatcher
from argilla_llama_index import ArgillaHandler
argilla_handler = ArgillaHandler(
dataset_name="query_llama_index",
api_url="http://localhost:6900",
api_key="argilla.apikey",
number_of_retrievals=2,
)
root_dispatcher = get_dispatcher()
root_dispatcher.add_span_handler(argilla_handler)
root_dispatcher.add_event_handler(argilla_handler)
示例指南#
其他合作伙伴一键式
集成(旧版模块)#
这些合作伙伴集成使用了我们遗留的CallbackManager
或第三方调用。
Langfuse#
此集成已弃用。我们建议使用基于检测的新集成方式与Langfuse,如此处所述。
使用模式#
from llama_index.core import set_global_handler
# Make sure you've installed the 'llama-index-callbacks-langfuse' integration package.
# NOTE: Set your environment variables 'LANGFUSE_SECRET_KEY', 'LANGFUSE_PUBLIC_KEY' and 'LANGFUSE_HOST'
# as shown in your langfuse.com project settings.
set_global_handler("langfuse")
指南#
DeepEval#
DeepEval (by Confident AI) 是一个面向LLM应用的开源评估框架。当您使用DeepEval当前提供的14+种默认指标(摘要、幻觉、答案相关性、忠实度、RAGAS等)对LLM应用进行"单元测试"时,可以通过与LlamaIndex的追踪集成来调试失败的测试用例,或者通过DeepEval托管的评估平台Confident AI在生产环境中调试不理想的评估结果,该平台支持在生产环境中进行无参考评估。
使用模式#
from llama_index.core import set_global_handler
set_global_handler("deepeval")
# NOTE: Run 'deepeval login' in the CLI to log traces on Confident AI, DeepEval's hosted evaluation platform.
# Run all of your LlamaIndex applications as usual and traces
# will be collected and displayed on Confident AI whenever evaluations are ran.
...
Weights and Biases 提示#
提示功能允许用户在索引构建和查询过程中记录/追踪/检查LlamaIndex的执行流程。它还支持用户对索引进行版本控制。
使用模式#
from llama_index.core import set_global_handler
set_global_handler("wandb", run_args={"project": "llamaindex"})
# NOTE: No need to do the following
from llama_index.callbacks.wandb import WandbCallbackHandler
from llama_index.core.callbacks import CallbackManager
from llama_index.core import Settings
# wandb_callback = WandbCallbackHandler(run_args={"project": "llamaindex"})
# Settings.callback_manager = CallbackManager([wandb_callback])
# access additional methods on handler to persist index + load index
import llama_index.core
# persist index
llama_index.core.global_handler.persist_index(graph, index_name="my_index")
# load storage context
storage_context = llama_index.core.global_handler.load_storage_context(
artifact_url="ayut/llamaindex/my_index:v0"
)
指南#
OpenInference#
OpenInference 是一个用于捕获和存储AI模型推理的开放标准。它支持通过诸如Phoenix等LLM可观测性解决方案,对LLM应用进行实验、可视化和评估。
使用模式#
import llama_index.core
llama_index.core.set_global_handler("openinference")
# NOTE: No need to do the following
from llama_index.callbacks.openinference import OpenInferenceCallbackHandler
from llama_index.core.callbacks import CallbackManager
from llama_index.core import Settings
# callback_handler = OpenInferenceCallbackHandler()
# Settings.callback_manager = CallbackManager([callback_handler])
# Run your LlamaIndex application here...
for query in queries:
query_engine.query(query)
# View your LLM app data as a dataframe in OpenInference format.
from llama_index.core.callbacks.open_inference_callback import as_dataframe
query_data_buffer = llama_index.core.global_handler.flush_query_data_buffer()
query_dataframe = as_dataframe(query_data_buffer)
注意:要解锁Phoenix的全部功能,您需要定义额外的步骤来输入查询/上下文数据框。详见下文!
指南#
TruEra TruLens#
TruLens 允许用户通过反馈函数和追踪等功能,对LlamaIndex应用进行监测和评估。
使用模式 + 指南#
# use trulens
from trulens_eval import TruLlama
tru_query_engine = TruLlama(query_engine)
# query
tru_query_engine.query("What did the author do growing up?")
指南#
HoneyHive#
HoneyHive 让用户能够追踪任何LLM工作流的执行流程。用户可以调试和分析这些追踪记录,或针对特定追踪事件定制反馈,从而从生产环境中创建评估或微调数据集。
使用模式#
from llama_index.core import set_global_handler
set_global_handler(
"honeyhive",
project="My HoneyHive Project",
name="My LLM Workflow Name",
api_key="MY HONEYHIVE API KEY",
)
# NOTE: No need to do the following
from llama_index.core.callbacks import CallbackManager
# from honeyhive.utils.llamaindex_tracer import HoneyHiveLlamaIndexTracer
from llama_index.core import Settings
# hh_tracer = HoneyHiveLlamaIndexTracer(
# project="My HoneyHive Project",
# name="My LLM Workflow Name",
# api_key="MY HONEYHIVE API KEY",
# )
# Settings.callback_manager = CallbackManager([hh_tracer])
使用Perfetto调试分析您的HoneyHive追踪数据
指南#
PromptLayer#
PromptLayer 让您能够追踪跨LLM调用的分析数据,为各种应用场景标记、分析和评估提示词。将其与LlamaIndex结合使用,可追踪您的RAG提示词等性能表现。
使用模式#
import os
os.environ["PROMPTLAYER_API_KEY"] = "pl_7db888a22d8171fb58aab3738aa525a7"
from llama_index.core import set_global_handler
# pl_tags are optional, to help you organize your prompts and apps
set_global_handler("promptlayer", pl_tags=["paul graham", "essay"])
指南#
Langtrace#
Langtrace 是一个强大的开源工具,支持OpenTelemetry,旨在无缝追踪、评估和管理LLM应用程序。Langtrace直接与LlamaIndex集成,提供关于准确性、评估和延迟等性能指标的详细实时洞察。
安装#
pip install langtrace-python-sdk
使用模式#
from langtrace_python_sdk import (
langtrace,
) # Must precede any llm module imports
langtrace.init(api_key="<LANGTRACE_API_KEY>")
指南#
OpenLIT#
OpenLIT 是一款基于OpenTelemetry的GenAI和LLM应用可观测性工具。它旨在通过单行代码即可将可观测性集成到GenAI项目中。OpenLIT为各种LLM、向量数据库和框架(如LlamaIndex)提供OpenTelemetry自动插桩功能。OpenLIT能深入洞察您的LLM应用性能、请求追踪,并提供诸如成本、令牌等使用情况的概览指标。
安装#
pip install openlit
使用模式#
import openlit
openlit.init()
指南#
智能体操作#
AgentOps 帮助开发者构建、评估和监控AI智能体。AgentOps将协助从原型到生产环境的智能体开发,提供智能体监控、LLM成本追踪、基准测试等功能。
安装#
pip install llama-index-instrumentation-agentops
使用模式#
from llama_index.core import set_global_handler
# NOTE: Feel free to set your AgentOps environment variables (e.g., 'AGENTOPS_API_KEY')
# as outlined in the AgentOps documentation, or pass the equivalent keyword arguments
# anticipated by AgentOps' AOClient as **eval_params in set_global_handler.
set_global_handler("agentops")
简单(LLM输入/输出)#
这个简单的可观测性工具会将每个LLM输入/输出对打印到终端。当您需要快速启用LLM应用程序的调试日志时最为实用。
使用模式#
import llama_index.core
llama_index.core.set_global_handler("simple")