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

# ChatGroq 集成

> 使用 LangChain JavaScript 集成 ChatGroq 对话模型。

[Groq](https://groq.com/) 提供由 LPU™ AI 推理技术驱动的快速 AI 推理。

有关可用模型的列表，请参阅 [Groq 模型文档](https://console.groq.com/docs/models)。

本页面帮助您开始使用 Groq [对话模型](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md)。有关所有 `ChatGroq` 功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/javascript/langchain-groq/ChatGroq)。

## 概述

### 集成详情

| 类 | 包 | 可序列化 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/chat/groq.md) | 下载次数 | 版本 |
| :- | :- | :-: | :-: | :-: | :-: |
| [`ChatGroq`](https://reference.langchain.com/javascript/langchain-groq/ChatGroq) | [`@langchain/groq`](https://www.npmjs.com/package/@langchain/groq) | ❌ | ✅ | ![NPM - 下载](https://img.shields.io/npm/dm/@langchain/groq?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/groq?style=flat-square\&label=%20&) |

### 模型功能

请查看下表标题中的链接，了解如何使用特定功能。

| [工具调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | [结构化输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/structured-output.md) | [图像输入](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/messages.md#多模态) | 音频输入 | 视频输入 | [Token 级流式输出](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/streaming.md) | [Token 使用情况](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#token-使用) | [Logprobs](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/models.md#对数概率) |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |

## 设置

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

### 凭证

要使用 Groq API，请在 [Groq 控制台](https://console.groq.com/keys) 中创建一个 API 密钥。
然后，您可以在终端中将 API 密钥设置为环境变量：

```bash theme={null}
export GROQ_API_KEY="your-api-key"
```

如果您希望自动跟踪您的模型调用，您还可以通过取消注释以下内容来设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API 密钥：

```bash theme={null}
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
```

### 安装

LangChain ChatGroq 集成位于 `@langchain/groq` 包中：

```bash npm theme={null}
npm install @langchain/groq @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/groq @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/groq @langchain/core
```

## 实例化

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

```typescript theme={null}
import { ChatGroq } from "@langchain/groq"

const llm = new ChatGroq({
    model: "openai/gpt-oss-120b",
    temperature: 0,
    maxTokens: undefined,
    maxRetries: 2,
    // other params...
})
```

## 调用

```typescript theme={null}
const aiMsg = await llm.invoke([
    {
      role: "system",
      content: "You are a helpful assistant that translates English to French. Translate the user sentence.",
    },
    { role: "user", content: "I love programming." },
])
aiMsg
```

```text theme={null}
AIMessage {
  "content": "I enjoy programming. (The French translation is: \"J'aime programmer.\")\n\nNote: I chose to translate \"I love programming\" as \"J'aime programmer\" instead of \"Je suis amoureux de programmer\" because the latter has a romantic connotation that is not present in the original English sentence.",
  "additional_kwargs": {},
  "response_metadata": {
    "tokenUsage": {
      "completionTokens": 73,
      "promptTokens": 31,
      "totalTokens": 104
    },
    "finish_reason": "stop"
  },
  "tool_calls": [],
  "invalid_tool_calls": []
}
```

```typescript theme={null}
console.log(aiMsg.content)
```

```text theme={null}
I enjoy programming. (The French translation is: "J'aime programmer.")

Note: I chose to translate "I love programming" as "J'aime programmer" instead of "Je suis amoureux de programmer" because the latter has a romantic connotation that is not present in the original English sentence.
```

## 使用 JSON 输出调用

```typescript theme={null}
const messages = [
  {
    role: "system",
    content: "You are a math tutor that handles math exercises and makes output in json in format { result: number }.",
  },
  { role: "user",  content: "2 + 2 * 2" },
];

const aiInvokeMsg = await llm.invoke(messages, { response_format: { type: "json_object" } });

// if you want not to pass response_format in every invoke, you can bind it to the instance
const llmWithResponseFormat = llm.bind({ response_format: { type: "json_object" } });
const aiBindMsg = await llmWithResponseFormat.invoke(messages);

// they are the same
console.log({ aiInvokeMsgContent: aiInvokeMsg.content, aiBindMsg: aiBindMsg.content });
```

```json theme={null}
{
  aiInvokeMsgContent: '{\n"result": 6\n}',
  aiBindMsg: '{\n"result": 6\n}'
}
```

***

## API 参考

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

***

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


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