跳到主要内容
TruLens 是一个开源软件包,为基于大型语言模型 (LLM) 的应用程序提供检测和评估工具。
本页介绍如何使用 TruLens 来评估和追踪基于 LangChain 构建的 LLM 应用程序。

安装和设置

安装 trulens-eval Python 包。
pip install trulens-eval

快速入门

请参阅 TruLens 文档中的集成详情。

追踪

创建 LLM 链后,您可以使用 TruLens 进行评估和追踪。TruLens 拥有许多开箱即用的反馈函数,它也是一个可扩展的 LLM 评估框架。 创建反馈函数:
from trulens_eval.feedback import Feedback, Huggingface,

# Initialize HuggingFace-based feedback function collection class:
hugs = Huggingface()
openai = OpenAI()

# Define a language match feedback function using HuggingFace.
lang_match = Feedback(hugs.language_match).on_input_output()
# By default this will check language match on the main app input and main app
# output.

# Question/answer relevance between overall question and answer.
qa_relevance = Feedback(openai.relevance).on_input_output()
# By default this will evaluate feedback on main app input and main app output.

# Toxicity of input
toxicity = Feedback(openai.toxicity).on_input()

为评估 LLM 设置反馈函数后,您可以使用 TruChain 封装您的应用程序,以获取 LLM 应用程序的详细追踪、日志记录和评估。 注意:有关 chain 创建的代码,请参阅 TruLens 文档
from trulens_eval import TruChain

# wrap your chain with TruChain
truchain = TruChain(
    chain,
    app_id='Chain1_ChatApplication',
    feedbacks=[lang_match, qa_relevance, toxicity]
)
# Note: any `feedbacks` specified here will be evaluated and logged whenever the chain is used.
truchain("que hora es?")

评估

现在您可以探索您的基于 LLM 的应用程序了! 这样做将帮助您一目了然地了解您的 LLM 应用程序的性能。当您迭代 LLM 应用程序的新版本时,您可以比较它们在您设置的所有不同质量指标上的性能。您还可以查看记录级别的评估,并探索每个记录的链元数据。
from trulens_eval import Tru

tru = Tru()
tru.run_dashboard() # open a Streamlit app to explore
有关 TruLens 的更多信息,请访问 trulens.org
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.