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

# Cohere 集成

> 使用 LangChain Python 与 Cohere LLM 集成。

<Warning>
  **您当前正在查看的是关于将 Cohere 模型用作文本补全模型的文档。许多流行的 Cohere 模型是[聊天补全模型](/oss/python/langchain/models)。**

  您可能想查看[此页面](/oss/python/integrations/chat/cohere/)。
</Warning>

> [Cohere](https://cohere.ai/about) 是一家加拿大初创公司，提供自然语言处理模型，帮助企业改善人机交互。

请前往 [API 参考](https://reference.langchain.com/python/langchain-community/llms/cohere/Cohere) 获取所有属性和方法的详细文档。

## 概述

### 集成详情

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

## 设置

该集成位于 `langchain-community` 包中。我们还需要安装 `cohere` 包本身。可以通过以下方式安装：

### 凭证

我们需要获取一个 [Cohere API 密钥](https://cohere.com/) 并设置 `COHERE_API_KEY` 环境变量：

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

if "COHERE_API_KEY" not in os.environ:
    os.environ["COHERE_API_KEY"] = getpass.getpass()
```

### 安装

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -U langchain-community langchain-cohere
```

设置 [LangSmith](https://smith.langchain.com/) 以获得一流的可观测性也很有帮助（但非必需）

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
```

## 调用

Cohere 支持所有 [LLM](/oss/python/langchain/models) 功能：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_cohere import Cohere
from langchain.messages import HumanMessage
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
model = Cohere(max_tokens=256, temperature=0.75)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
message = "Knock knock"
model.invoke(message)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
" Who's there?"
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
await model.ainvoke(message)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
" Who's there?"
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
for chunk in model.stream(message):
    print(chunk, end="", flush=True)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
 Who's there?
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
model.batch([message])
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[" Who's there?"]
```

***

## API 参考

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

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/llms/cohere.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
