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

# Haystack

> 在 Haystack OpenAIChatGenerator 中使用 TokenLab

## 概述

Haystack 的 `OpenAIChatGenerator` 通过 `api_base_url` 支持自定义的 OpenAI 兼容部署。将其指向 TokenLab 并提供 TokenLab API key 即可使用。

<Note>
  **类型**: RAG 和 pipeline 框架

  **主要路径**: OpenAI 兼容的 Chat Completions

  **支持置信度**: 支持的 OpenAI 兼容路径
</Note>

## 环境配置

```bash theme={null}
export TOKENLAB_API_KEY="sk-your-tokenlab-key"
```

## 生成器

```python theme={null}
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

generator = OpenAIChatGenerator(
    api_key=Secret.from_env_var("TOKENLAB_API_KEY"),
    api_base_url="https://api.tokenlab.sh/v1",
    model="claude-sonnet-5",
)

response = generator.run(
    messages=[ChatMessage.from_user("Summarize this document in one sentence.")]
)
```

## 端点说明

在 Haystack 聊天生成器 pipeline 中使用此路径。对于嵌入（embeddings），如果你的 pipeline 使用了 TokenLab 嵌入，请使用 TokenLab 的 `/v1` base URL 配置相应的 Haystack 嵌入组件。
