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

# ChatGroq 集成

> 使用 LangChain Python 集成 ChatGroq 聊天模型。

<Warning>
  本页面涉及 [Groq](https://console.groq.com/docs/overview)，一家 AI 硬件和软件公司。关于如何使用 Grok 模型（由 [xAI](https://docs.x.ai/docs/overview) 提供）的信息，请参阅 [xAI 供应商页面](/oss/python/integrations/providers/xai)。
</Warning>

<Tip>
  **API 参考**

  有关所有功能和配置选项的详细文档，请前往 [`ChatGroq`](https://reference.langchain.com/python/langchain-groq/chat_models/ChatGroq) API 参考。
</Tip>

有关所有 Groq 模型的列表，请访问其 [文档](https://console.groq.com/docs/models?utm_source=langchain)。

## 概述

### 集成详情

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

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming#llm-tokens) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :---------------------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ❌  |   ❌  |                           ✅                           |   ✅  |                         ✅                         |                            ✅                           |

## 设置

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

### 凭证

前往 [Groq 控制台](https://console.groq.com/login?utm_source=langchain\&utm_content=chat_page) 注册 Groq 并生成 API 密钥。完成后，设置 GROQ\_API\_KEY 环境变量：

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

if "GROQ_API_KEY" not in os.environ:
    os.environ["GROQ_API_KEY"] = getpass.getpass("输入您的 Groq API 密钥：")
```

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("输入您的 LangSmith API 密钥：")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain Groq 集成位于 `langchain-groq` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-groq
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全。

<Note>
  **推理格式**

  如果您选择设置 `reasoning_format`，必须确保您使用的模型支持它。您可以在 [Groq 文档](https://console.groq.com/docs/reasoning) 中找到支持的模型列表。
</Note>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_groq import ChatGroq

llm = ChatGroq(
    model="qwen/qwen3-32b",
    temperature=0,
    max_tokens=None,
    reasoning_format="parsed",
    timeout=None,
    max_retries=2,
    # 其他参数...
)
```

## 调用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
messages = [
    (
        "system",
        "您是一个将英语翻译成法语的助手。请翻译用户的句子。",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
AIMessage(content="J'aime la programmation.", additional_kwargs={'reasoning_content': 'Okay, so I need to translate the sentence "I love programming." into French. Let me think about how to approach this. \n\nFirst, I know that "I" in French is "Je." That\'s straightforward. Now, the verb "love" in French is "aime" when referring to oneself. So, "I love" would be "J\'aime." \n\nNext, the word "programming." In French, programming is "la programmation." But wait, in French, when you talk about loving an activity, you often use the definite article. So, it would be "la programmation." \n\nPutting it all together, "I love programming" becomes "J\'aime la programmation." That sounds right. I think that\'s the correct translation. \n\nI should double-check to make sure I\'m not missing anything. Maybe I can think of similar phrases. For example, "I love reading" is "J\'aime lire," but when it\'s a noun, like "I love music," it\'s "J\'aime la musique." So, yes, using "la programmation" makes sense here. \n\nI don\'t think I need to change anything else. The sentence structure in French is Subject-Verb-Object, just like in English, so "J\'aime la programmation" should be correct. \n\nI guess another way to say it could be "J\'adore la programmation," using "adore" instead of "aime," but "aime" is more commonly used in this context. So, sticking with "J\'aime la programmation" is probably the best choice.\n'}, response_metadata={'token_usage': {'completion_tokens': 346, 'prompt_tokens': 23, 'total_tokens': 369, 'completion_time': 1.447541218, 'prompt_time': 0.000983386, 'queue_time': 0.009673684, 'total_time': 1.448524604}, 'model_name': 'deepseek-r1-distill-llama-70b', 'system_fingerprint': 'fp_e98d30d035', 'finish_reason': 'stop', 'logprobs': None}, id='run--5679ae4f-f4e8-4931-bcd5-7304223832c0-0', usage_metadata={'input_tokens': 23, 'output_tokens': 346, 'total_tokens': 369})
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(ai_msg.content)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
J'aime la programmation.
```

## 视觉功能

Groq 支持特定模型的视觉功能，允许您发送图像以及文本提示。

<Note>
  **支持视觉的模型**

  * `meta-llama/llama-4-scout-17b-16e-instruct`
  * `meta-llama/llama-4-maverick-17b-128e-instruct`

  有关最新支持视觉的模型列表，请查看 [Groq 文档](https://console.groq.com/docs/vision)。
</Note>

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

llm = ChatGroq(model="meta-llama/llama-4-scout-17b-16e-instruct")

message = HumanMessage(
    content=[
        {"type": "text", "text": "详细描述这张图片。"},
        {
            "type": "image_url",
            "image_url": {"url": "https://example.com/image.jpg"},
        },
    ]
)

response = llm.invoke([message])
print(response.content)
```

<Tip>
  **图像 URL 要求**

  Groq 直接从 URL 获取图像。请确保您的图像 URL：

  * 可公开访问（无需身份验证）
  * 直接返回图像（无重定向）
  * 最大图像大小：每个请求 20MB
</Tip>

***

## API 参考

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\chat\groq.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

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