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

# Together AI 集成

> 使用 LangChain Python 集成 Together AI LLM。

> [!WARNING]
>
> **您当前所在的页面正在记录 Together AI 模型作为文本补全模型的使用方法。许多流行的 Together AI 模型是 [对话补全模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。**
>
> 您可能想查看 [这个页面](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/together.md)。

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

本示例介绍了如何使用 LangChain 与 Together AI 模型进行交互。

## 安装

```python theme={null}
pip install -U langchain-together
```

## 环境

要使用 Together AI，您需要一个 API 密钥，可以在以下位置找到：
[api.together.ai/settings/api-keys](https://api.together.ai/settings/api-keys)。这可以作为初始化参数 `together_api_key` 传入，或设置为环境变量 `TOGETHER_API_KEY`。

## 示例

```python theme={null}
# Querying chat models with Together AI

from langchain_together import ChatTogether

# choose from our 50+ models here: https://docs.together.ai/docs/inference-models
chat = ChatTogether(
    # together_api_key="YOUR_API_KEY",
    model="meta-llama/Llama-3-70b-chat-hf",
)

# stream the response back from the model
stream = chat.stream_events("Tell me fun things to do in NYC", version="v3")
for token in stream.text:
    print(token, end="", flush=True)

# if you don't want to do streaming, you can use the invoke method
# chat.invoke("Tell me fun things to do in NYC")
```

```python theme={null}
# Querying code and language models with Together AI

from langchain_together import Together

llm = Together(
    model="codellama/CodeLlama-70b-Python-hf",
    # together_api_key="..."
)

print(llm.invoke("def bubble_sort(): "))
```

***

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


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