> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Trulens 集成

> 使用 LangChain Python 集成 Trulens。

> [TruLens](https://trulens.org) 是一个 [开源](https://github.com/truera/trulens) 软件包，为基于大型语言模型（LLM）的应用提供监控和评估工具。

本页面介绍了如何使用 [TruLens](https://trulens.org) 来评估和跟踪基于 langchain 构建的 LLM 应用。

## 安装和设置

安装 `trulens-eval` python 软件包。

```bash pip theme={null}
pip install trulens-eval
```

```bash uv theme={null}
uv add trulens-eval
```

## 快速入门

请参阅 [TruLens 文档](https://www.trulens.org/cookbook/frameworks/langchain/langchain_quickstart/) 中的集成详情。

### 跟踪

创建好您的 LLM 链之后，您可以使用 TruLens 进行评估和跟踪。
TruLens 提供了多种 [开箱即用的反馈函数](https://www.trulens.org/component_guides/evaluation/feedback_implementations/)，
同时也是一个可扩展的 LLM 评估框架。

创建反馈函数：

```python theme={null}
from trulens_eval.feedback import Feedback, Huggingface, OpenAI

# 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 文档](https://www.trulens.org/cookbook/frameworks/langchain/langchain_quickstart/) 中。

```python theme={null}
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 应用的新版本时，
您可以比较它们在不同质量指标上的性能。您还将能够以记录级别查看评估结果，
并探索每条记录的链元数据。

```python theme={null}
from trulens_eval import Tru

tru = Tru()
tru.run_dashboard() # open a Streamlit app to explore
```

有关更多关于 TruLens 的信息，请访问 [trulens.org](https://www.trulens.org/)

***

> 官方原文：[src/oss/python/integrations/providers/trulens.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/providers/trulens.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.