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

# Cohere 集成

> 使用 LangChain Python 集成 Cohere 大语言模型 (LLM)。

<Warning>
  **您当前所在的页面介绍的是 Cohere models as text completion models. Many popular Cohere models are [chat completion models](/oss/python/langchain/models).**

  You may be looking for [此页面](/oss/python/integrations/chat/cohere/).
</Warning>

> [Cohere](https://cohere.ai/about) is a Canadian startup that provides natural language processing models that help companies improve human-machine interactions.

Head to the [API reference](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) |

## 设置

The 集成位于 `langchain-community` package. We also need to install the `cohere` package itself. We can install these with:

### 凭证

We'll need to get a [Cohere API key](https://cohere.com/) and set the `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
```

It's also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com?utm_source=docs\&utm_medium=cta\&utm_campaign=langsmith-signup\&utm_content=oss-python-integrations-llms-cohere) for best-in-class observability

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

## 调用

Cohere supports all [LLM](/oss/python/langchain/models) functionality:

```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 reference](https://reference.langchain.com/python/langchain-community/llms/cohere/Cohere)

***

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

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/llms/cohere.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
