@tanstack/ai-groq 0.1.11 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/adapters/text.d.ts +30 -68
- package/dist/esm/adapters/text.js +31 -383
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/message-types.d.ts +9 -96
- package/dist/esm/text/text-provider-options.d.ts +1 -26
- package/dist/esm/utils/client.d.ts +6 -8
- package/dist/esm/utils/client.js +15 -11
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/index.d.ts +2 -1
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/package.json +4 -4
- package/src/adapters/text.ts +63 -552
- package/src/message-types.ts +9 -129
- package/src/text/text-provider-options.ts +0 -36
- package/src/utils/client.ts +18 -15
- package/src/utils/index.ts +2 -2
- package/src/utils/schema-converter.ts +2 -2
- package/dist/esm/text/text-provider-options.js +0 -6
- package/dist/esm/text/text-provider-options.js.map +0 -1
package/src/message-types.ts
CHANGED
|
@@ -1,72 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Groq-specific message types for the Chat Completions API.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Groq's wire format is OpenAI Chat Completions plus a few Groq-specific
|
|
5
|
+
* extensions (compound tools, citation/service-tier provider options,
|
|
6
|
+
* etc.). These type definitions describe that wire shape directly — the
|
|
7
|
+
* Groq SDK was dropped in favour of pointing the OpenAI SDK at Groq's
|
|
8
|
+
* `/openai/v1` base URL, so this file is the source of truth for
|
|
9
|
+
* Groq-only fields rather than a mirror of an external SDK's types.
|
|
6
10
|
*
|
|
7
11
|
* @see https://console.groq.com/docs/api-reference#chat
|
|
8
12
|
*/
|
|
9
13
|
|
|
10
|
-
export interface ChatCompletionContentPartText {
|
|
11
|
-
/** The text content. */
|
|
12
|
-
text: string
|
|
13
|
-
|
|
14
|
-
/** The type of the content part. */
|
|
15
|
-
type: 'text'
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface ChatCompletionContentPartImage {
|
|
19
|
-
image_url: {
|
|
20
|
-
/** Either a URL of the image or the base64 encoded image data. */
|
|
21
|
-
url: string
|
|
22
|
-
|
|
23
|
-
/** Specifies the detail level of the image. */
|
|
24
|
-
detail?: 'auto' | 'low' | 'high'
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** The type of the content part. */
|
|
28
|
-
type: 'image_url'
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface ChatCompletionMessageToolCall {
|
|
32
|
-
/** The ID of the tool call. */
|
|
33
|
-
id: string
|
|
34
|
-
|
|
35
|
-
/** The function that the model called. */
|
|
36
|
-
function: {
|
|
37
|
-
/**
|
|
38
|
-
* The arguments to call the function with, as generated by the model in JSON
|
|
39
|
-
* format. Note that the model does not always generate valid JSON, and may
|
|
40
|
-
* hallucinate parameters not defined by your function schema. Validate the
|
|
41
|
-
* arguments in your code before calling your function.
|
|
42
|
-
*/
|
|
43
|
-
arguments: string
|
|
44
|
-
|
|
45
|
-
/** The name of the function to call. */
|
|
46
|
-
name: string
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The type of the tool. Currently, only `function` is supported. */
|
|
50
|
-
type: 'function'
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface ChatCompletionRequestMessageContentPartDocument {
|
|
54
|
-
document: {
|
|
55
|
-
/** The JSON document data. */
|
|
56
|
-
data: { [key: string]: unknown }
|
|
57
|
-
|
|
58
|
-
/** Optional unique identifier for the document. */
|
|
59
|
-
id?: string | null
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** The type of the content part. */
|
|
63
|
-
type: 'document'
|
|
64
|
-
}
|
|
65
|
-
|
|
66
14
|
export type FunctionParameters = { [key: string]: unknown }
|
|
67
15
|
|
|
68
16
|
export interface ChatCompletionNamedToolChoice {
|
|
69
|
-
|
|
17
|
+
/** Always `function` for a named tool choice. */
|
|
18
|
+
type: 'function'
|
|
19
|
+
function: {
|
|
70
20
|
/** The name of the function to call. */
|
|
71
21
|
name: string
|
|
72
22
|
}
|
|
@@ -113,34 +63,6 @@ export type ChatCompletionToolChoiceOption =
|
|
|
113
63
|
| 'required'
|
|
114
64
|
| ChatCompletionNamedToolChoice
|
|
115
65
|
|
|
116
|
-
export type ChatCompletionContentPart =
|
|
117
|
-
| ChatCompletionContentPartText
|
|
118
|
-
| ChatCompletionContentPartImage
|
|
119
|
-
| ChatCompletionRequestMessageContentPartDocument
|
|
120
|
-
|
|
121
|
-
export interface ChatCompletionAssistantMessageParam {
|
|
122
|
-
/** The role of the messages author, in this case `assistant`. */
|
|
123
|
-
role: 'assistant'
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* The contents of the assistant message. Required unless `tool_calls` or
|
|
127
|
-
* `function_call` is specified.
|
|
128
|
-
*/
|
|
129
|
-
content?: string | Array<ChatCompletionContentPartText> | null
|
|
130
|
-
|
|
131
|
-
/** An optional name for the participant. */
|
|
132
|
-
name?: string
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The reasoning output by the assistant if reasoning_format was set to 'parsed'.
|
|
136
|
-
* This field is only useable with qwen3 models.
|
|
137
|
-
*/
|
|
138
|
-
reasoning?: string | null
|
|
139
|
-
|
|
140
|
-
/** The tool calls generated by the model, such as function calls. */
|
|
141
|
-
tool_calls?: Array<ChatCompletionMessageToolCall>
|
|
142
|
-
}
|
|
143
|
-
|
|
144
66
|
export interface ChatCompletionTool {
|
|
145
67
|
/**
|
|
146
68
|
* The type of the tool. `function`, `browser_search`, and `code_interpreter` are
|
|
@@ -151,48 +73,6 @@ export interface ChatCompletionTool {
|
|
|
151
73
|
function?: FunctionDefinition
|
|
152
74
|
}
|
|
153
75
|
|
|
154
|
-
export interface ChatCompletionToolMessageParam {
|
|
155
|
-
/** The contents of the tool message. */
|
|
156
|
-
content: string | Array<ChatCompletionContentPart>
|
|
157
|
-
|
|
158
|
-
/** The role of the messages author, in this case `tool`. */
|
|
159
|
-
role: 'tool'
|
|
160
|
-
|
|
161
|
-
/** Tool call that this message is responding to. */
|
|
162
|
-
tool_call_id: string
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export interface ChatCompletionSystemMessageParam {
|
|
166
|
-
/** The contents of the system message. */
|
|
167
|
-
content: string | Array<ChatCompletionContentPartText>
|
|
168
|
-
|
|
169
|
-
/** The role of the messages author, in this case `system`. */
|
|
170
|
-
role: 'system' | 'developer'
|
|
171
|
-
|
|
172
|
-
/** An optional name for the participant. */
|
|
173
|
-
name?: string
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export interface ChatCompletionUserMessageParam {
|
|
177
|
-
/** The contents of the user message. */
|
|
178
|
-
content: string | Array<ChatCompletionContentPart>
|
|
179
|
-
|
|
180
|
-
/** The role of the messages author, in this case `user`. */
|
|
181
|
-
role: 'user'
|
|
182
|
-
|
|
183
|
-
/** An optional name for the participant. */
|
|
184
|
-
name?: string
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Union of all supported chat completion message params.
|
|
189
|
-
*/
|
|
190
|
-
export type ChatCompletionMessageParam =
|
|
191
|
-
| ChatCompletionSystemMessageParam
|
|
192
|
-
| ChatCompletionUserMessageParam
|
|
193
|
-
| ChatCompletionAssistantMessageParam
|
|
194
|
-
| ChatCompletionToolMessageParam
|
|
195
|
-
|
|
196
76
|
export interface CompoundCustomModels {
|
|
197
77
|
/** Custom model to use for answering. */
|
|
198
78
|
answering_model?: string | null
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
ChatCompletionMessageParam,
|
|
3
|
-
ChatCompletionTool,
|
|
4
2
|
ChatCompletionToolChoiceOption,
|
|
5
3
|
CompoundCustom,
|
|
6
4
|
Document,
|
|
@@ -185,41 +183,7 @@ export interface GroqTextProviderOptions {
|
|
|
185
183
|
user?: string | null
|
|
186
184
|
}
|
|
187
185
|
|
|
188
|
-
/**
|
|
189
|
-
* Internal options interface used for validation within the adapter.
|
|
190
|
-
* Extends provider options with required fields for API requests.
|
|
191
|
-
*/
|
|
192
|
-
export interface InternalTextProviderOptions extends GroqTextProviderOptions {
|
|
193
|
-
/** An array of messages comprising the conversation. */
|
|
194
|
-
messages: Array<ChatCompletionMessageParam>
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* The model name (e.g. "llama-3.3-70b-versatile", "openai/gpt-oss-120b").
|
|
198
|
-
* @see https://console.groq.com/docs/models
|
|
199
|
-
*/
|
|
200
|
-
model: string
|
|
201
|
-
|
|
202
|
-
/** Whether to stream partial message deltas as server-sent events. */
|
|
203
|
-
stream?: boolean | null
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Tools the model may call (functions, code_interpreter, etc).
|
|
207
|
-
* @see https://console.groq.com/docs/tool-use
|
|
208
|
-
*/
|
|
209
|
-
tools?: Array<ChatCompletionTool>
|
|
210
|
-
}
|
|
211
|
-
|
|
212
186
|
/**
|
|
213
187
|
* External provider options (what users pass in)
|
|
214
188
|
*/
|
|
215
189
|
export type ExternalTextProviderOptions = GroqTextProviderOptions
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Validates text provider options.
|
|
219
|
-
* Basic validation stub — Groq API handles detailed validation.
|
|
220
|
-
*/
|
|
221
|
-
export function validateTextProviderOptions(
|
|
222
|
-
_options: InternalTextProviderOptions,
|
|
223
|
-
): void {
|
|
224
|
-
// Groq API handles detailed validation
|
|
225
|
-
}
|
package/src/utils/client.ts
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type { ClientOptions } from 'groq-sdk'
|
|
1
|
+
import { getApiKeyFromEnv } from '@tanstack/ai-utils'
|
|
2
|
+
import type { ClientOptions } from 'openai'
|
|
4
3
|
|
|
5
|
-
export interface GroqClientConfig extends ClientOptions {
|
|
4
|
+
export interface GroqClientConfig extends Omit<ClientOptions, 'apiKey'> {
|
|
6
5
|
apiKey: string
|
|
7
6
|
}
|
|
8
7
|
|
|
9
|
-
/**
|
|
10
|
-
* Creates a Groq SDK client instance
|
|
11
|
-
*/
|
|
12
|
-
export function createGroqClient(config: GroqClientConfig): Groq_SDK {
|
|
13
|
-
return new Groq_SDK(config)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
8
|
/**
|
|
17
9
|
* Gets Groq API key from environment variables
|
|
18
10
|
* @throws Error if GROQ_API_KEY is not found
|
|
19
11
|
*/
|
|
20
12
|
export function getGroqApiKeyFromEnv(): string {
|
|
21
|
-
|
|
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
|
+
}
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
|
-
*
|
|
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
26
|
*/
|
|
27
|
-
export function
|
|
28
|
-
return
|
|
27
|
+
export function withGroqDefaults(config: GroqClientConfig): GroqClientConfig {
|
|
28
|
+
return {
|
|
29
|
+
...config,
|
|
30
|
+
baseURL: config.baseURL || 'https://api.groq.com/openai/v1',
|
|
31
|
+
}
|
|
29
32
|
}
|
package/src/utils/index.ts
CHANGED
|
@@ -62,7 +62,7 @@ function removeEmptyRequired(schema: Record<string, any>): Record<string, any> {
|
|
|
62
62
|
/**
|
|
63
63
|
* Recursively normalise object schemas so any `{ type: 'object' }` node
|
|
64
64
|
* without `properties` gets an empty `properties: {}` object. The
|
|
65
|
-
* openai-base transformer only descends into objects that already have
|
|
65
|
+
* ai-openai-base transformer only descends into objects that already have
|
|
66
66
|
* `properties` set, so a Zod `z.object({})` nested inside `properties`,
|
|
67
67
|
* `items`, `additionalProperties`, or a combinator branch would otherwise
|
|
68
68
|
* skip the strict-mode rewrite and fail Groq validation.
|
|
@@ -140,7 +140,7 @@ export function makeGroqStructuredOutputCompatible(
|
|
|
140
140
|
schema: Record<string, any>,
|
|
141
141
|
originalRequired: Array<string> = [],
|
|
142
142
|
): Record<string, any> {
|
|
143
|
-
// Recursively patch every `{ type: 'object' }` node so the openai-base
|
|
143
|
+
// Recursively patch every `{ type: 'object' }` node so the ai-openai-base
|
|
144
144
|
// transformer descends into nested empty objects too.
|
|
145
145
|
const normalised = normalizeObjectSchemas(schema)
|
|
146
146
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"text-provider-options.js","sources":["../../../src/text/text-provider-options.ts"],"sourcesContent":["import type {\n ChatCompletionMessageParam,\n ChatCompletionTool,\n ChatCompletionToolChoiceOption,\n CompoundCustom,\n Document,\n ResponseFormatJsonObject,\n ResponseFormatJsonSchema,\n ResponseFormatText,\n SearchSettings,\n} from '../message-types'\n\n/**\n * Groq-specific provider options for text/chat models.\n *\n * These options extend the standard Chat Completions API parameters\n * with Groq-specific features like compound models and search settings.\n *\n * @see https://console.groq.com/docs/api-reference#chat\n */\nexport interface GroqTextProviderOptions {\n /**\n * Whether to enable citations in the response. When enabled, the model will\n * include citations for information retrieved from provided documents or web\n * searches.\n */\n citation_options?: 'enabled' | 'disabled' | null\n\n /** Custom configuration of models and tools for Compound. */\n compound_custom?: CompoundCustom | null\n\n /**\n * If set to true, groq will return called tools without validating that the tool\n * is present in request.tools. tool_choice=required/none will still be enforced,\n * but the request cannot require a specific tool be used.\n */\n disable_tool_validation?: boolean\n\n /**\n * A list of documents to provide context for the conversation. Each document\n * contains text that can be referenced by the model.\n */\n documents?: Array<Document> | null\n\n /**\n * Number between -2.0 and 2.0. Positive values penalize new tokens based on their\n * existing frequency in the text so far, decreasing the model's likelihood to\n * repeat the same line verbatim.\n */\n frequency_penalty?: number | null\n\n /**\n * Whether to include reasoning in the response. This field is mutually exclusive\n * with `reasoning_format`.\n */\n include_reasoning?: boolean | null\n\n /** Modify the likelihood of specified tokens appearing in the completion. */\n logit_bias?: { [key: string]: number } | null\n\n /**\n * Whether to return log probabilities of the output tokens or not. If true,\n * returns the log probabilities of each output token returned in the `content`\n * of `message`.\n */\n logprobs?: boolean | null\n\n /**\n * The maximum number of tokens that can be generated in the chat completion. The\n * total length of input tokens and generated tokens is limited by the model's\n * context length.\n */\n max_completion_tokens?: number | null\n\n /** Request metadata. */\n metadata?: { [key: string]: string } | null\n\n /**\n * How many chat completion choices to generate for each input message.\n * Currently only n=1 is supported.\n */\n n?: number | null\n\n /** Whether to enable parallel function calling during tool use. */\n parallel_tool_calls?: boolean | null\n\n /**\n * Number between -2.0 and 2.0. Positive values penalize new tokens based on\n * whether they appear in the text so far, increasing the model's likelihood to\n * talk about new topics.\n */\n presence_penalty?: number | null\n\n /**\n * Controls reasoning effort for supported models.\n *\n * - qwen3 models: `'none'` to disable, `'default'` or null to enable\n * - openai/gpt-oss models: `'low'`, `'medium'` (default), or `'high'`\n */\n reasoning_effort?: 'none' | 'default' | 'low' | 'medium' | 'high' | null\n\n /**\n * Specifies how to output reasoning tokens.\n * This field is mutually exclusive with `include_reasoning`.\n */\n reasoning_format?: 'hidden' | 'raw' | 'parsed' | null\n\n /**\n * An object specifying the format that the model must output.\n *\n * - `json_schema` — enables Structured Outputs (preferred)\n * - `json_object` — enables the older JSON mode\n * - `text` — plain text output (default)\n *\n * @see https://console.groq.com/docs/structured-outputs\n */\n response_format?:\n | ResponseFormatText\n | ResponseFormatJsonSchema\n | ResponseFormatJsonObject\n | null\n\n /** Settings for web search functionality when the model uses a web search tool. */\n search_settings?: SearchSettings | null\n\n /**\n * If specified, our system will make a best effort to sample deterministically,\n * such that repeated requests with the same `seed` and parameters should return\n * the same result.\n */\n seed?: number | null\n\n /**\n * The service tier to use for the request.\n *\n * - `auto` — automatically select the highest tier available\n * - `flex` — uses the flex tier, which will succeed or fail quickly\n */\n service_tier?: 'auto' | 'on_demand' | 'flex' | 'performance' | null\n\n /**\n * Up to 4 sequences where the API will stop generating further tokens.\n * The returned text will not contain the stop sequence.\n */\n stop?: string | null | Array<string>\n\n /** Whether to store the request for future use. */\n store?: boolean | null\n\n /**\n * Sampling temperature between 0 and 2. Higher values like 0.8 will make the\n * output more random, while lower values like 0.2 will make it more focused\n * and deterministic. We generally recommend altering this or top_p but not both.\n */\n temperature?: number | null\n\n /**\n * Controls which (if any) tool is called by the model.\n *\n * - `none` — never call tools\n * - `auto` — model decides (default when tools are present)\n * - `required` — model must call tools\n * - Named choice — forces a specific tool\n */\n tool_choice?: ChatCompletionToolChoiceOption | null\n\n /**\n * An integer between 0 and 20 specifying the number of most likely tokens to\n * return at each token position. `logprobs` must be set to `true` if this\n * parameter is used.\n */\n top_logprobs?: number | null\n\n /**\n * An alternative to sampling with temperature, called nucleus sampling, where the\n * model considers the results of the tokens with top_p probability mass. So 0.1\n * means only the tokens comprising the top 10% probability mass are considered.\n */\n top_p?: number | null\n\n /**\n * A unique identifier representing your end-user, which can help monitor and\n * detect abuse.\n */\n user?: string | null\n}\n\n/**\n * Internal options interface used for validation within the adapter.\n * Extends provider options with required fields for API requests.\n */\nexport interface InternalTextProviderOptions extends GroqTextProviderOptions {\n /** An array of messages comprising the conversation. */\n messages: Array<ChatCompletionMessageParam>\n\n /**\n * The model name (e.g. \"llama-3.3-70b-versatile\", \"openai/gpt-oss-120b\").\n * @see https://console.groq.com/docs/models\n */\n model: string\n\n /** Whether to stream partial message deltas as server-sent events. */\n stream?: boolean | null\n\n /**\n * Tools the model may call (functions, code_interpreter, etc).\n * @see https://console.groq.com/docs/tool-use\n */\n tools?: Array<ChatCompletionTool>\n}\n\n/**\n * External provider options (what users pass in)\n */\nexport type ExternalTextProviderOptions = GroqTextProviderOptions\n\n/**\n * Validates text provider options.\n * Basic validation stub — Groq API handles detailed validation.\n */\nexport function validateTextProviderOptions(\n _options: InternalTextProviderOptions,\n): void {\n // Groq API handles detailed validation\n}\n"],"names":[],"mappings":"AA4NO,SAAS,4BACd,UACM;AAER;"}
|