Open History to see the last commit
1import { getApiKeyFromEnv } from '@tanstack/ai-utils'
2import type { ClientOptions } from 'openai'
3
4export interface GroqClientConfig extends Omit<ClientOptions, 'apiKey'> {
5 apiKey: string
6}
7
8/**
9 * Gets Groq API key from environment variables
10 * @throws Error if GROQ_API_KEY is not found
11 */
12export function getGroqApiKeyFromEnv(): string {
13 try {
14 return getApiKeyFromEnv('GROQ_API_KEY')
15 } catch {
16 throw new Error(
17 'GROQ_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.',
18 )
19 }
20}
21
22/**
23 * Returns a Groq client config with Groq's OpenAI-compatible base URL
24 * applied when not already set. The Groq endpoint accepts the OpenAI SDK
25 * verbatim, so the adapter drives it via the OpenAI SDK with this baseURL.
26 */
27export function withGroqDefaults(config: GroqClientConfig): GroqClientConfig {
28 return {
29 ...config,
30 baseURL: config.baseURL || 'https://api.groq.com/openai/v1',
31 }
32}