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

# Cohere 集成

> 使用 LangChain JavaScript 与 Cohere LLM 集成。

> [!WARNING]
>
> **遗留**
>
> Cohere 将其 `generate` 端点用于 LLM 标记为已弃用。请遵循他们的 [迁移指南](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat)，通过 [`ChatCohere`](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/cohere) 集成开始使用他们的 Chat API。

这将帮助您使用 LangChain 开始使用 Cohere 完成模型（LLM）。有关 `Cohere` 功能和配置选项的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-cohere/Cohere)。

## 概述

### 集成详情

| 类 | 包 | 本地 | 可序列化 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/llms/cohere.md) | 下载 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: | :-: |
| [`Cohere`](https://reference.langchain.com/javascript/langchain-cohere/Cohere) | [`@langchain/cohere`](https://www.npmjs.com/package/@langchain/cohere) | ❌ | ✅ | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/cohere?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/cohere?style=flat-square\&label=%20&) |

## 设置

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

### 凭证

前往 [cohere.com](https://cohere.com) 注册 Cohere 并生成 API 密钥。完成后，设置 `COHERE_API_KEY` 环境变量：

```bash theme={null}
export COHERE_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 Cohere 集成位于 `@langchain/cohere` 包中：

```bash npm theme={null}
npm install @langchain/cohere @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/cohere @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/cohere @langchain/core
```

## 实例化

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

```typescript theme={null}
import { Cohere } from "@langchain/cohere"

const llm = new Cohere({
  model: "command",
  temperature: 0,
  maxTokens: undefined,
  maxRetries: 2,
  // other params...
})
```

### 用于 Azure 上的 Cohere、AWS Bedrock 上的 Cohere 和独立 Cohere 实例的自定义客户端

我们可以实例化一个自定义 `CohereClient`，并将其传递给 `Cohere` 构造函数。

**注意：** 如果提供了自定义客户端，`COHERE_API_KEY` 环境变量和 `apiKey` 构造函数中的参数都将被忽略。

```typescript theme={null}
import { Cohere } from "@langchain/cohere";
import { CohereClient } from "cohere-ai";

const client = new CohereClient({
  token: "<your-api-key>",
  environment: "<your-cohere-deployment-url>", //optional
  // other params
});

const llmWithCustomClient = new Cohere({
  client,
  // other params...
});
```

## 调用

```typescript theme={null}
const inputText = "Cohere is an AI company that "

const completion = await llm.invoke(inputText)
completion
```

```text theme={null}
Cohere is a company that provides natural language processing models that help companies improve human-machine interactions. Cohere was founded in 2019 by Aidan Gomez, Ivan Zhang, and Nick Frosst.
```

***

## API 参考

有关所有 Cohere 功能和配置的详细文档，请前往 [API 参考](https://reference.langchain.com/javascript/langchain-cohere/Cohere)。

***

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


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