跳至主要内容

ChatGroq

Groq 是一家提供快速 AI 推理的公司,其 LPU™ AI 推理技术可提供快速、经济实惠且节能的 AI 推理。

这将帮助您开始使用 ChatGroq 聊天模型。有关所有 ChatGroq 功能和配置的详细文档,请前往API 参考.

概述

集成详细信息

本地可序列化PY 支持包下载包最新版本
ChatGroq@langchain/groqNPM - DownloadsNPM - Version

模型功能

请查看下方表格标题中的链接,了解如何使用特定功能的指南。

工具调用结构化输出JSON 模式图像输入音频输入视频输入令牌级流式传输令牌使用情况Logprobs

设置

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

凭据

要使用 Groq API,您需要一个 API 密钥。您可以在此处注册一个 Groq 帐户并创建一个 API 密钥这里。然后,您可以在终端中将 API 密钥设置为环境变量

export GROQ_API_KEY="your-api-key"

如果您希望自动跟踪您的模型调用,也可以通过取消以下注释来设置您的LangSmith API 密钥

# export LANGCHAIN_TRACING_V2="true"
# export LANGCHAIN_API_KEY="your-api-key"

安装

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

yarn add @langchain/groq

实例化

现在我们可以实例化我们的模型对象并生成聊天完成结果

import { ChatGroq } from "@langchain/groq";

const llm = new ChatGroq({
model: "mixtral-8x7b-32768",
temperature: 0,
maxTokens: undefined,
maxRetries: 2,
// other params...
});

调用

const aiMsg = await llm.invoke([
[
"system",
"You are a helpful assistant that translates English to French. Translate the user sentence.",
],
["human", "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.

链接

我们可以使用以下方式将我们的模型与提示模板链接

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


此页面是否有用?


您也可以留下详细的反馈 在 GitHub 上.