Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

groq.md

296 lines8.8 KB

1---
2title: Groq
3id: groq-adapter
4order: 6
5description: "Use Groq's fast inference API with TanStack AI for low-latency LLM responses and Whisper transcription — Llama and other open-weight models via @tanstack/ai-groq."
6keywords:
7 - tanstack ai
8 - groq
9 - fast inference
10 - llama
11 - low latency
12 - adapter
13 - llm
14 - whisper
15 - transcription
16---
17​
18The Groq adapter provides access to Groq's fast inference API, featuring the world's fastest LLM inference and Whisper-based audio transcription.
19​
20## Installation
21​
22<!-- ::start:tabs variant="package-manager" mode="install" -->
23​
24react: @tanstack/ai-groq
25vue: @tanstack/ai-groq
26solid: @tanstack/ai-groq
27svelte: @tanstack/ai-groq
28preact: @tanstack/ai-groq
29angular: @tanstack/ai-groq
30vanilla: @tanstack/ai-groq
31octane: @tanstack/ai-groq
32​
33<!-- ::end:tabs -->
34​
35## Basic Usage
36​
37```typescript
38import { chat } from "@tanstack/ai";
39import { groqText } from "@tanstack/ai-groq";
40​
41const stream = chat({
42 adapter: groqText("llama-3.3-70b-versatile"),
43 messages: [{ role: "user", content: "Hello!" }],
44});
45```
46​
47## Basic Usage - Custom API Key
48​
49```typescript
50import { chat } from "@tanstack/ai";
51import { createGroqText } from "@tanstack/ai-groq";
52​
53const adapter = createGroqText("llama-3.3-70b-versatile", process.env.GROQ_API_KEY!, {
54 // ... your config options
55});
56​
57const stream = chat({
58 adapter,
59 messages: [{ role: "user", content: "Hello!" }],
60});
61```
62​
63## Configuration
64​
65```typescript
66import { createGroqText, type GroqTextConfig } from "@tanstack/ai-groq";
67​
68const config: Omit<GroqTextConfig, 'apiKey'> = {
69 baseURL: "https://api.groq.com/openai/v1", // Optional, for custom endpoints
70};
71​
72const adapter = createGroqText("llama-3.3-70b-versatile", process.env.GROQ_API_KEY!, config);
73```
74​
75## Example: Chat Completion
76​
77```typescript
78import { chat, toServerSentEventsResponse } from "@tanstack/ai";
79import { groqText } from "@tanstack/ai-groq";
80​
81export async function POST(request: Request) {
82 const { messages } = await request.json();
83​
84 const stream = chat({
85 adapter: groqText("llama-3.3-70b-versatile"),
86 messages,
87 });
88​
89 return toServerSentEventsResponse(stream);
91```
92​
93## Example: With Tools
94​
95```typescript
96import { chat, toolDefinition, type ModelMessage } from "@tanstack/ai";
97import { groqText } from "@tanstack/ai-groq";
98import { z } from "zod";
99​
100const searchDatabaseDef = toolDefinition({
101 name: "search_database",
102 description: "Search the database",
103 inputSchema: z.object({
104 query: z.string(),
105 }),
106});
107​
108const searchDatabase = searchDatabaseDef.server(async ({ query }) => {
109 // Search database
110 return { results: [] };
111});
112​
113const messages: Array<ModelMessage> = [{ role: "user", content: "Search for something" }];
114​
115const stream = chat({
116 adapter: groqText("llama-3.3-70b-versatile"),
117 messages,
118 tools: [searchDatabase],
119});
120```
121​
122If Groq rejects a generated tool call with `tool_use_failed` and includes a reconstructable tool call in `failed_generation`, the adapter returns the provider error as that tool's result without executing the call. The agent loop can then repair the call on its next iteration. Other provider errors remain terminal run errors.
123​
124## Transcription
125​
126Groq exposes Whisper-based speech-to-text via `groqTranscription()` and the `generateTranscription()` activity. The `audio` input accepts a `File`, `Blob`, `ArrayBuffer`, base64 string, data URL, or an `https://` URL (forwarded directly to Groq without re-uploading).
127​
128```typescript
129import { generateTranscription } from "@tanstack/ai";
130import { groqTranscription } from "@tanstack/ai-groq";
131​
132const result = await generateTranscription({
133 adapter: groqTranscription("whisper-large-v3-turbo"),
134 audio: "https://example.com/recording.mp3",
135 language: "en",
136});
137​
138console.log(result.text);
139​
140// verbose_json (the default) populates language, duration, and timestamped segments
141for (const segment of result.segments ?? []) {
142 console.log(`[${segment.start}s → ${segment.end}s] ${segment.text}`);
144```
145​
146Supported models: `whisper-large-v3-turbo`, `whisper-large-v3`. Supported `responseFormat` values: `json`, `text`, `verbose_json` (default). `srt` and `vtt` are not supported by Groq.
147​
148See [Transcription](../media/transcription) for the full API.
149​
150## Model Options
151​
152Groq supports various provider-specific options. Sampling parameters live here too — `temperature`, `top_p`, and `max_completion_tokens` (Groq's token-limit key) — rather than as root-level props on `chat()`:
153​
154```typescript
155import { chat } from "@tanstack/ai";
156import { groqText } from "@tanstack/ai-groq";
157​
158const stream = chat({
159 adapter: groqText("llama-3.3-70b-versatile"),
160 messages: [{ role: "user", content: "Hello!" }],
161 modelOptions: {
162 temperature: 0.7,
163 max_completion_tokens: 1024,
164 top_p: 0.9,
165 },
166});
167```
168​
169> If you previously passed `temperature` / `topP` / `maxTokens` at the root of `chat()`, see [Moving Sampling Options into modelOptions](../migration/sampling-options-to-model-options).
170​
171### Reasoning
172​
173Enable reasoning for models that support it (e.g., `openai/gpt-oss-120b`, `qwen/qwen3-32b`). This allows the model to show its reasoning process, which is streamed as `thinking` chunks:
174​
175```typescript ignore
176modelOptions: {
177 reasoning_effort: "medium", // "none" | "default" | "low" | "medium" | "high"
179```
180​
181## Summarization
182​
183Summarize long text content:
184​
185```typescript
186import { summarize } from "@tanstack/ai";
187import { groqSummarize } from "@tanstack/ai-groq";
188​
189const result = await summarize({
190 adapter: groqSummarize("llama-3.3-70b-versatile"),
191 text: "Your long text to summarize...",
192 maxLength: 100,
193 style: "concise", // "concise" | "bullet-points" | "paragraph"
194});
195​
196console.log(result.summary);
197```
198​
199## Supported Models
200​
201Groq offers a diverse selection of models from multiple providers:
202​
203### Meta Llama
204​
205- `llama-3.3-70b-versatile` - Fast, capable model with 128K context
206- `llama-3.1-8b-instant` - Fast, cost-effective model
207- `meta-llama/llama-4-maverick-17b-128e-instruct` - Latest Llama 4 with vision support
208- `meta-llama/llama-4-scout-17b-16e-instruct` - Efficient Llama 4 model
209​
210### Security Models
211​
212- `meta-llama/llama-guard-4-12b` - Content moderation
213- `meta-llama/llama-prompt-guard-2-86m` - Prompt injection detection
214- `meta-llama/llama-prompt-guard-2-22m` - Lightweight prompt guard
215​
216### OpenAI GPT-OSS Models
217​
218- `openai/gpt-oss-120b` - Large OSS model with reasoning support
219- `openai/gpt-oss-20b` - Efficient OSS model
220- `openai/gpt-oss-safeguard-20b` - Safety-tuned OSS model
221​
222### Other Providers
223​
224- `moonshotai/kimi-k2-instruct-0905` - Kimi K2 with 256K context
225- `qwen/qwen3-32b` - Qwen 3 with reasoning support
226​
227## Environment Variables
228​
229Set your API key in environment variables:
230​
231```bash
232GROQ_API_KEY=gsk_...
233```
234​
235## API Reference
236​
237### `groqText(model, config?)`
238​
239Creates a Groq chat adapter using environment variables.
240​
241**Parameters:**
242​
243- `model` - The model name (e.g., `llama-3.3-70b-versatile`)
244- `config` (optional) - Optional configuration object. Supports the same options as `createGroqText` except `apiKey`, which is auto-detected from `GROQ_API_KEY` environment variable. Common options:
245 - `baseURL` - Custom base URL for API requests (optional)
246​
247**Returns:** A Groq chat adapter instance.
248​
249### `createGroqText(model, apiKey, config?)`
250​
251Creates a Groq chat adapter with an explicit API key.
252​
253**Parameters:**
254​
255- `model` - The model name (e.g., `llama-3.3-70b-versatile`)
256- `apiKey` - Your Groq API key
257- `config` (optional) - Optional configuration object:
258 - `baseURL` - Custom base URL for API requests (optional)
259​
260**Returns:** A Groq chat adapter instance.
261​
262### `groqSummarize(model, config?)`
263​
264Creates a Groq summarization adapter using environment variables.
265​
266**Returns:** A Groq summarize adapter instance.
267​
268### `createGroqSummarize(model, apiKey, config?)`
269​
270Creates a Groq summarization adapter with an explicit API key.
271​
272**Returns:** A Groq summarize adapter instance.
273​
274### `groqTranscription(model, config?)` / `createGroqTranscription(model, apiKey, config?)`
275​
276Creates a Groq transcription (speech-to-text) adapter. The short form reads `GROQ_API_KEY` from the environment; the `create*` form takes an explicit API key. Supported models: `whisper-large-v3-turbo`, `whisper-large-v3`.
277​
278## Limitations
279​
280- **Text-to-Speech**: Groq does not currently expose a TTS adapter. Use OpenAI, Gemini, ElevenLabs, or fal for speech generation.
281- **Image Generation**: Groq does not support image generation. Use OpenAI, Gemini, or fal for image generation.
282​
283## Next Steps
284​
285- [Getting Started](../getting-started/quick-start) - Learn the basics
286- [Tools Guide](../tools/tools) - Learn about tools
287- [Other Adapters](./openai) - Explore other providers
288​
289## Provider Tools
290​
291Groq does not currently expose provider-specific tool factories.
292Define your own tools with `toolDefinition()` from `@tanstack/ai`.
293​
294See [Tools](../tools/tools.md) for the general tool-definition flow, or
295[Provider Tools](../tools/provider-tools.md) for other providers'
296native-tool offerings.