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

# ChatTogetherAI 集成

> 使用 LangChain JavaScript 集成 ChatTogetherAI 对话模型。

[Together AI](https://www.together.ai/) 提供了一个 API 来查询 [50+ 领先的开源模型](https://docs.together.ai/docs/inference-models)，只需几行代码即可。

本指南将帮助你开始使用 `ChatTogetherAI` [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。有关所有 `ChatTogetherAI` 功能和配置的详细文档，请访问 [API 参考](https://reference.langchain.com/javascript/langchain-together-ai/ChatTogetherAI)。

## 概述

### 集成细节

| 类 | 包 | 可序列化 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/together.md) | 下载次数 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatTogetherAI`](https://reference.langchain.com/javascript/langchain-together-ai/ChatTogetherAI) | [`@langchain/together-ai`](https://www.npmjs.com/package/@langchain/together-ai) | ✅ | ✅ | ![NPM - 下载次数](https://img.shields.io/npm/dm/@langchain/together-ai?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/together-ai?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) | [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#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |

## 设置

要访问 `ChatTogetherAI` 模型，请创建一个 Together 账户，[获取 API 密钥](https://api.together.xyz/)，并安装 `@langchain/together-ai` 集成包。

### 凭证

前往 [api.together.ai](https://api.together.ai/) 注册 Together AI 并生成 API 密钥。设置 `TOGETHER_AI_API_KEY` 环境变量：

```bash theme={null}
export TOGETHER_AI_API_KEY="your-api-key"
```

如果你希望自动跟踪你的模型调用，你也可以通过取消注释以下内容来设置你的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```bash theme={null}
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### 安装

LangChain ChatTogetherAI 集成位于 `@langchain/together-ai` 包中：

```bash npm theme={null}
npm install @langchain/together-ai @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/together-ai @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/together-ai @langchain/core
```

## 实例化

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

```typescript theme={null}
import { ChatTogetherAI } from "@langchain/together-ai";

const llm = new ChatTogetherAI({
    model: "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
    temperature: 0,
    // other params...
})
```

## 调用

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

```text theme={null}
AIMessage {
  "id": "chatcmpl-9rT9qEDPZ6iLCk6jt3XTzVDDH6pcI",
  "content": "J'adore la programmation.",
  "additional_kwargs": {},
  "response_metadata": {
    "tokenUsage": {
      "completionTokens": 8,
      "promptTokens": 31,
      "totalTokens": 39
    },
    "finish_reason": "stop"
  },
  "tool_calls": [],
  "invalid_tool_calls": [],
  "usage_metadata": {
    "input_tokens": 31,
    "output_tokens": 8,
    "total_tokens": 39
  }
}
```

```typescript theme={null}
console.log(aiMsg.content)
```

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

***

## API 参考

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

***

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


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