Skip to main content
Open on GitHub

TruLens

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设置了反馈功能后,您可以使用LangChain将应用程序封装起来,以获得详细的追踪、日志记录和对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