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

# Together AI 集成

> 使用 LangChain Python 与 Together AI 集成。

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

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

## 安装

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 使用 Together AI 查询聊天模型

from langchain_together import ChatTogether

# 从我们的 50 多个模型中选择：https://docs.together.ai/docs/inference-models
chat = ChatTogether(
    # together_api_key="YOUR_API_KEY",
    model="meta-llama/Llama-3-70b-chat-hf",
)

# 从模型流式传输响应
for m in chat.stream("Tell me fun things to do in NYC"):
    print(m.content, end="", flush=True)

# 如果您不想使用流式传输，可以使用 invoke 方法
# chat.invoke("Tell me fun things to do in NYC")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 使用 Together AI 查询代码和语言模型

from langchain_together import Together

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

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

***

<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/providers/together.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
