> ## 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.

# ChatTogether 集成

> 使用 LangChain Python 集成 ChatTogether 对话模型。

本页面将帮助您开始使用 Together AI [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。有关所有 `ChatTogether` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/python/langchain-together/chat_models/ChatTogether)。

[Together AI](https://www.together.ai/) 提供一个 API 来查询 [50+ 领先的开源模型](https://docs.together.ai/docs/serverless-models)

## 概述

### 集成详情

| 类 | 包 | 可序列化 | [JS 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/javascript/integrations/chat/togetherai.md) | 下载次数 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatTogether`](https://reference.langchain.com/python/langchain-together/chat_models/ChatTogether) | [`langchain-together`](https://reference.langchain.com/python/langchain-together) | beta | ✅ | ![PyPI - 下载](https://img.shields.io/pypi/dm/langchain-together?style=flat-square\&label=%20) | ![PyPI - 版本](https://img.shields.io/pypi/v/langchain-together?style=flat-square\&label=%20) |

### 模型功能

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token 级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md#llm-tokens) | 原生异步 | [Token 使用量](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [Logprobs](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ |

## 设置

要访问 Together 模型，您需要创建一个 Together 账户，获取一个 API 密钥，并安装 `langchain-together` 集成包。

### 凭证

前往 [此页面](https://api.together.ai) 注册 Together 并生成 API 密钥。完成后，设置 TOGETHER\_API\_KEY 环境变量：

```python theme={null}
import getpass
import os

if "TOGETHER_API_KEY" not in os.environ:
    os.environ["TOGETHER_API_KEY"] = getpass.getpass("Enter your Together API key: ")
```

要启用模型调用的自动跟踪，设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```python theme={null}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain Together 集成包含在 `langchain-together` 包中：

```python theme={null}
pip install -qU langchain-together
```

## 实例化

现在我们可以实例化我们的模型对象并生成对话补全：

```python theme={null}
from langchain_together import ChatTogether

llm = ChatTogether(
    model="meta-llama/Llama-3-70b-chat-hf",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    # other params...
)
```

## 调用

```python theme={null}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={null}
AIMessage(content="J'adore la programmation.", response_metadata={'token_usage': {'completion_tokens': 9, 'prompt_tokens': 35, 'total_tokens': 44}, 'model_name': 'meta-llama/Llama-3-70b-chat-hf', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-eabcbe33-cdd8-45b8-ab0b-f90b6e7dfad8-0', usage_metadata={'input_tokens': 35, 'output_tokens': 9, 'total_tokens': 44})
```

```python theme={null}
print(ai_msg.content)
```

```text theme={null}
J'adore la programmation.
```

***

## API 参考

有关所有 `ChatTogether` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/python/langchain-together/chat_models/ChatTogether)

***

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


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