用途:该技能围绕 Groq 的主要集成模式展开,覆盖聊天补全、工具/函数调用、JSON 模式、结构化输出以及多轮对话,适合实现聊天界面、函数调用与结构化数据提取等实时功能。
主要能力:按五个步骤依次讲解——聊天补全(发送 system 与 user 消息,返回 choices[0].message.content 与 usage);工具调用(发送带 tools 与 tool_choice: "auto" 的消息,执行返回的 tool_calls,再把结果回传获取最终答案);JSON 模式(设置 response_format: { type: "json_object" } 并在系统提示中描述 JSON 结构);结构化输出(使用 response_format.json_schema 与 strict: true 保证符合 schema);多轮对话(累积消息历史)。
模型选择:聊天与工具调用推荐 llama-3.3-70b-versatile;JSON 提取推荐 llama-3.1-8b-instant;结构化输出推荐 llama-3.3-70b-versatile;视觉加聊天推荐 meta-llama/llama-4-scout-17b-16e-instruct。
使用方式:先安装 SDK:npm install groq-sdk;在 console.groq.com/keys 创建 API 密钥并导出为 GROQ_API_KEY,SDK 会自动读取,new Groq() 无需显式参数,切勿硬编码密钥。最小聊天骨架为:import Groq from "groq-sdk"; const groq = new Groq(); 然后调用 groq.chat.completions.create,传入 model、messages、temperature、max_tokens。
输出与错误处理:聊天返回 reply 与 usage(含 prompt_tokens/completion_tokens);工具调用返回最终助手内容,中间 tool_calls 含 function.name 与 JSON 字符串 function.arguments;JSON 模式需用 JSON.parse 解析;结构化输出保证符合声明的 schema。常见错误包括 tool_calls 的 JSON 格式错误(用 try/catch 包裹并降低温度重试)、json_object 返回非 JSON(系统提示中必须包含 JSON 指令)、context_length_exceeded(裁剪旧消息)、工具调用循环(最终补全设置 tool_choice: "none")。
限制:资料未提供价格、速率限制、平台支持范围等细节;音频、视觉与语音工作流由配套技能 groq-core-workflow-b 覆盖。