ChatGroq
Groq 是一家公司,提供由 LPU™ AI 推理技术驱动的快速 AI 推理,该技术提供快速、经济高效且节能的 AI。
这将帮助你开始使用 ChatGroq 聊天模型。有关所有 ChatGroq 功能和配置的详细文档,请前往 API 参考。
概览
集成详情
| 类别 | 包 | 本地 | 可序列化 | PY 支持 | 包下载量 | 包最新版本 |
|---|---|---|---|---|---|---|
| ChatGroq | @langchain/groq | ❌ | ❌ | ✅ | ![]() | ![]() |
模型功能
请参阅下表标题中的链接,了解如何使用特定功能的指南。
| 工具调用 | 结构化输出 | JSON 模式 | 图像输入 | 音频输入 | 视频输入 | 逐 token 流式传输 | token 使用情况 | logprobs |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
准备工作
要访问 ChatGroq 模型,你需要创建一个 Groq 账户,获取 API 密钥,并安装
@langchain/groq 集成包。
凭据
为了使用 Groq API,你需要一个 API 密钥。你可以在这里注册一个 Groq 账户并创建一个 API 密钥 此处。 然后,你可以在终端中将 API 密钥设置为环境变量:
export GROQ_API_KEY="your-api-key"
如果你想获得模型调用的自动跟踪,也可以通过取消下面的注释来设置你的 LangSmith API 密钥:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
安装
LangChain ChatGroq 集成位于 @langchain/groq 包中:
:::提示 请参阅安装集成包的一般说明部分。 :::
- npm
- yarn
- pnpm
npm i @langchain/groq @langchain/core
yarn add @langchain/groq @langchain/core
pnpm add @langchain/groq @langchain/core
实例化
现在我们可以实例化我们的模型对象并生成聊天补全:
import { ChatGroq } from "@langchain/groq";
const llm = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// other params...
});
调用
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;
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": []
}
console.log(aiMsg.content);
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 调用
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,
});
{
aiInvokeMsgContent: '{\n"result": 6\n}',
aiBindMsg: '{\n"result": 6\n}'
}
链式调用
我们可以像这样将模型与提示模板链式调用:
import { ChatPromptTemplate } from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant that translates {input_language} to {output_language}.",
],
["human", "{input}"],
]);
const chain = prompt.pipe(llm);
await chain.invoke({
input_language: "English",
output_language: "German",
input: "I love programming.",
});
AIMessage {
"content": "That's great! I can help you translate English phrases related to programming into German.\n\n\"I love programming\" can be translated to German as \"Ich liebe Programmieren\".\n\nHere are some more programming-related phrases translated into German:\n\n* \"Programming language\" = \"Programmiersprache\"\n* \"Code\" = \"Code\"\n* \"Variable\" = \"Variable\"\n* \"Function\" = \"Funktion\"\n* \"Array\" = \"Array\"\n* \"Object-oriented programming\" = \"Objektorientierte Programmierung\"\n* \"Algorithm\" = \"Algorithmus\"\n* \"Data structure\" = \"Datenstruktur\"\n* \"Debugging\" = \"Debuggen\"\n* \"Compile\" = \"Kompilieren\"\n* \"Link\" = \"Verknüpfen\"\n* \"Run\" = \"Ausführen\"\n* \"Test\" = \"Testen\"\n* \"Deploy\" = \"Bereitstellen\"\n* \"Version control\" = \"Versionskontrolle\"\n* \"Open source\" = \"Open Source\"\n* \"Software development\" = \"Softwareentwicklung\"\n* \"Agile methodology\" = \"Agile Methodik\"\n* \"DevOps\" = \"DevOps\"\n* \"Cloud computing\" = \"Cloud Computing\"\n\nI hope this helps! Let me know if you have any other questions or if you need further translations.",
"additional_kwargs": {},
"response_metadata": {
"tokenUsage": {
"completionTokens": 327,
"promptTokens": 25,
"totalTokens": 352
},
"finish_reason": "stop"
},
"tool_calls": [],
"invalid_tool_calls": []
}
API 参考文档
有关 ChatGroq 所有功能和配置的详细文档,请访问 API 参考:https://api.js.langchain.com/classes/langchain_groq.ChatGroq.html
Related
- Chat model conceptual guide
- Chat model how-to guides

