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

# ChatTogether 集成

> 使用 LangChain Python 与 ChatTogether 聊天模型进行集成。

本页面将帮助您开始使用 Together AI 的[聊天模型](/oss/python/langchain/models)。有关 `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/chat-models)。

## 概述

### 集成详情

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

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming#llm-tokens) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :---------------------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ✅  |   ✅  |                           ✅                           |   ❌  |                         ✅                         |                            ✅                           |

## 设置

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

### 凭证

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if "TOGETHER_API_KEY" not in os.environ:
    os.environ["TOGETHER_API_KEY"] = getpass.getpass("输入您的 Together API 密钥：")
```

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("输入您的 LangSmith API 密钥：")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-together
```

## 实例化

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

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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,
    # 其他参数...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "您是一个将英语翻译成法语的助手。请翻译用户的句子。",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'adore la programmation.
```

***

## API 参考

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\chat\together.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
