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

summarize.ts

77 lines2.4 KB

1import { ChatStreamSummarizeAdapter } from '@tanstack/ai/adapters'
2import { getGroqApiKeyFromEnv } from '../utils/client'
3import { GroqTextAdapter } from './text'
4import type { InferTextProviderOptions } from '@tanstack/ai/adapters'
5import type { GROQ_CHAT_MODELS } from '../model-meta'
6import type { GroqClientConfig } from '../utils/client'
7​
8/**
9 * Configuration for Groq summarize adapter
10 */
11export interface GroqSummarizeConfig extends GroqClientConfig {}
12​
13/** Model type for Groq summarization */
14export type GroqSummarizeModel = (typeof GROQ_CHAT_MODELS)[number]
15​
16/**
17 * Creates a Groq summarize adapter with explicit API key.
18 * Type resolution happens here at the call site.
19 *
20 * @param model - The model name (e.g., 'llama-3.3-70b-versatile')
21 * @param apiKey - Your Groq API key
22 * @param config - Optional additional configuration
23 * @returns Configured Groq summarize adapter instance with resolved types
24 *
25 * @example
26 * ```typescript
27 * const adapter = createGroqSummarize('llama-3.3-70b-versatile', "gsk_...");
28 * ```
29 */
30export function createGroqSummarize<TModel extends GroqSummarizeModel>(
31 model: TModel,
32 apiKey: string,
33 config?: Omit<GroqSummarizeConfig, 'apiKey'>,
34): ChatStreamSummarizeAdapter<
35 TModel,
36 InferTextProviderOptions<GroqTextAdapter<TModel>>
37> {
38 return new ChatStreamSummarizeAdapter(
39 new GroqTextAdapter({ apiKey, ...config }, model),
40 model,
41 'groq',
42 )
44​
45/**
46 * Creates a Groq summarize adapter with automatic API key detection from environment variables.
47 * Type resolution happens here at the call site.
48 *
49 * Looks for `GROQ_API_KEY` in:
50 * - `process.env` (Node.js)
51 * - `window.env` (Browser with injected env)
52 *
53 * @param model - The model name (e.g., 'llama-3.3-70b-versatile')
54 * @param config - Optional configuration (excluding apiKey which is auto-detected)
55 * @returns Configured Groq summarize adapter instance with resolved types
56 * @throws Error if GROQ_API_KEY is not found in environment
57 *
58 * @example
59 * ```typescript
60 * // Automatically uses GROQ_API_KEY from environment
61 * const adapter = groqSummarize('llama-3.3-70b-versatile');
62 *
63 * await summarize({
64 * adapter,
65 * text: "Long article text..."
66 * });
67 * ```
68 */
69export function groqSummarize<TModel extends GroqSummarizeModel>(
70 model: TModel,
71 config?: Omit<GroqSummarizeConfig, 'apiKey'>,
72): ChatStreamSummarizeAdapter<
73 TModel,
74 InferTextProviderOptions<GroqTextAdapter<TModel>>
75> {
76 return createGroqSummarize(model, getGroqApiKeyFromEnv(), config)