@ai-sdk/huggingface 2.0.0-beta.4 → 2.0.0-beta.57
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/CHANGELOG.md +487 -4
- package/README.md +2 -0
- package/dist/index.d.ts +9 -7
- package/dist/index.js +298 -267
- package/dist/index.js.map +1 -1
- package/docs/170-huggingface.mdx +22 -20
- package/package.json +15 -15
- package/src/huggingface-config.ts +2 -2
- package/src/huggingface-provider.ts +10 -10
- package/src/index.ts +7 -1
- package/src/responses/convert-huggingface-responses-usage.ts +2 -2
- package/src/responses/convert-to-huggingface-responses-messages.ts +36 -22
- package/src/responses/huggingface-responses-language-model-options.ts +12 -0
- package/src/responses/huggingface-responses-language-model.ts +47 -33
- package/src/responses/huggingface-responses-prepare-tools.ts +8 -5
- package/src/responses/map-huggingface-responses-finish-reason.ts +2 -2
- package/src/version.ts +6 -0
- package/dist/index.d.mts +0 -59
- package/dist/index.mjs +0 -974
- package/dist/index.mjs.map +0 -1
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
SharedV3Warning,
|
|
3
|
-
LanguageModelV3Prompt,
|
|
4
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type SharedV4Warning,
|
|
4
|
+
type LanguageModelV4Prompt,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
+
import {
|
|
7
|
+
getTopLevelMediaType,
|
|
8
|
+
resolveFullMediaType,
|
|
9
|
+
} from '@ai-sdk/provider-utils';
|
|
6
10
|
|
|
7
11
|
export async function convertToHuggingFaceResponsesMessages({
|
|
8
12
|
prompt,
|
|
9
13
|
}: {
|
|
10
|
-
prompt:
|
|
14
|
+
prompt: LanguageModelV4Prompt;
|
|
11
15
|
}): Promise<{
|
|
12
16
|
input: string | Array<any>;
|
|
13
|
-
warnings: Array<
|
|
17
|
+
warnings: Array<SharedV4Warning>;
|
|
14
18
|
}> {
|
|
15
19
|
const messages: Array<any> = [];
|
|
16
|
-
const warnings: Array<
|
|
20
|
+
const warnings: Array<SharedV4Warning> = [];
|
|
17
21
|
|
|
18
22
|
for (const { role, content } of prompt) {
|
|
19
23
|
switch (role) {
|
|
@@ -31,23 +35,33 @@ export async function convertToHuggingFaceResponsesMessages({
|
|
|
31
35
|
return { type: 'input_text', text: part.text };
|
|
32
36
|
}
|
|
33
37
|
case 'file': {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
switch (part.data.type) {
|
|
39
|
+
case 'reference': {
|
|
40
|
+
throw new UnsupportedFunctionalityError({
|
|
41
|
+
functionality: 'file parts with provider references',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
case 'text': {
|
|
45
|
+
throw new UnsupportedFunctionalityError({
|
|
46
|
+
functionality: 'text file parts',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
case 'url':
|
|
50
|
+
case 'data': {
|
|
51
|
+
if (getTopLevelMediaType(part.mediaType) === 'image') {
|
|
52
|
+
return {
|
|
53
|
+
type: 'input_image',
|
|
54
|
+
image_url:
|
|
55
|
+
part.data.type === 'url'
|
|
56
|
+
? part.data.url.toString()
|
|
57
|
+
: `data:${resolveFullMediaType({ part })};base64,${part.data.data}`,
|
|
58
|
+
};
|
|
59
|
+
} else {
|
|
60
|
+
throw new UnsupportedFunctionalityError({
|
|
61
|
+
functionality: `file part media type ${part.mediaType}`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
51
65
|
}
|
|
52
66
|
}
|
|
53
67
|
default: {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
export const huggingfaceLanguageModelResponsesOptions = z.object({
|
|
4
|
+
metadata: z.record(z.string(), z.string()).optional(),
|
|
5
|
+
instructions: z.string().optional(),
|
|
6
|
+
strictJsonSchema: z.boolean().optional(),
|
|
7
|
+
reasoningEffort: z.string().optional(),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export type HuggingFaceLanguageModelResponsesOptions = z.infer<
|
|
11
|
+
typeof huggingfaceLanguageModelResponsesOptions
|
|
12
|
+
>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
APICallError,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
type LanguageModelV4,
|
|
4
|
+
type LanguageModelV4CallOptions,
|
|
5
|
+
type LanguageModelV4Content,
|
|
6
|
+
type LanguageModelV4FinishReason,
|
|
7
|
+
type LanguageModelV4GenerateResult,
|
|
8
|
+
type LanguageModelV4StreamPart,
|
|
9
|
+
type LanguageModelV4StreamResult,
|
|
10
|
+
type SharedV4Warning,
|
|
11
11
|
} from '@ai-sdk/provider';
|
|
12
12
|
import {
|
|
13
13
|
combineHeaders,
|
|
@@ -15,28 +15,49 @@ import {
|
|
|
15
15
|
createJsonResponseHandler,
|
|
16
16
|
generateId,
|
|
17
17
|
parseProviderOptions,
|
|
18
|
-
ParseResult,
|
|
19
18
|
postJsonToApi,
|
|
19
|
+
serializeModelOptions,
|
|
20
|
+
WORKFLOW_SERIALIZE,
|
|
21
|
+
WORKFLOW_DESERIALIZE,
|
|
22
|
+
type ParseResult,
|
|
20
23
|
} from '@ai-sdk/provider-utils';
|
|
21
24
|
import { z } from 'zod/v4';
|
|
22
|
-
import { HuggingFaceConfig } from '../huggingface-config';
|
|
25
|
+
import type { HuggingFaceConfig } from '../huggingface-config';
|
|
23
26
|
import { huggingfaceFailedResponseHandler } from '../huggingface-error';
|
|
24
27
|
import {
|
|
25
28
|
convertHuggingFaceResponsesUsage,
|
|
26
|
-
HuggingFaceResponsesUsage,
|
|
29
|
+
type HuggingFaceResponsesUsage,
|
|
27
30
|
} from './convert-huggingface-responses-usage';
|
|
28
31
|
import { convertToHuggingFaceResponsesMessages } from './convert-to-huggingface-responses-messages';
|
|
32
|
+
import { huggingfaceLanguageModelResponsesOptions } from './huggingface-responses-language-model-options';
|
|
29
33
|
import { prepareResponsesTools } from './huggingface-responses-prepare-tools';
|
|
30
|
-
import { HuggingFaceResponsesModelId } from './huggingface-responses-settings';
|
|
34
|
+
import type { HuggingFaceResponsesModelId } from './huggingface-responses-settings';
|
|
31
35
|
import { mapHuggingFaceResponsesFinishReason } from './map-huggingface-responses-finish-reason';
|
|
32
36
|
|
|
33
|
-
export class HuggingFaceResponsesLanguageModel implements
|
|
34
|
-
readonly specificationVersion = '
|
|
37
|
+
export class HuggingFaceResponsesLanguageModel implements LanguageModelV4 {
|
|
38
|
+
readonly specificationVersion = 'v4';
|
|
35
39
|
|
|
36
40
|
readonly modelId: HuggingFaceResponsesModelId;
|
|
37
41
|
|
|
38
42
|
private readonly config: HuggingFaceConfig;
|
|
39
43
|
|
|
44
|
+
static [WORKFLOW_SERIALIZE](model: HuggingFaceResponsesLanguageModel) {
|
|
45
|
+
return serializeModelOptions({
|
|
46
|
+
modelId: model.modelId,
|
|
47
|
+
config: model.config,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
52
|
+
modelId: HuggingFaceResponsesModelId;
|
|
53
|
+
config: HuggingFaceConfig;
|
|
54
|
+
}) {
|
|
55
|
+
return new HuggingFaceResponsesLanguageModel(
|
|
56
|
+
options.modelId,
|
|
57
|
+
options.config,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
40
61
|
constructor(modelId: HuggingFaceResponsesModelId, config: HuggingFaceConfig) {
|
|
41
62
|
this.modelId = modelId;
|
|
42
63
|
this.config = config;
|
|
@@ -64,8 +85,8 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
64
85
|
tools,
|
|
65
86
|
toolChoice,
|
|
66
87
|
responseFormat,
|
|
67
|
-
}:
|
|
68
|
-
const warnings:
|
|
88
|
+
}: LanguageModelV4CallOptions) {
|
|
89
|
+
const warnings: SharedV4Warning[] = [];
|
|
69
90
|
|
|
70
91
|
if (topK != null) {
|
|
71
92
|
warnings.push({ type: 'unsupported', feature: 'topK' });
|
|
@@ -97,7 +118,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
97
118
|
const huggingfaceOptions = await parseProviderOptions({
|
|
98
119
|
provider: 'huggingface',
|
|
99
120
|
providerOptions,
|
|
100
|
-
schema:
|
|
121
|
+
schema: huggingfaceLanguageModelResponsesOptions,
|
|
101
122
|
});
|
|
102
123
|
|
|
103
124
|
const {
|
|
@@ -150,8 +171,8 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
150
171
|
}
|
|
151
172
|
|
|
152
173
|
async doGenerate(
|
|
153
|
-
options:
|
|
154
|
-
): Promise<
|
|
174
|
+
options: LanguageModelV4CallOptions,
|
|
175
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
155
176
|
const { args, warnings } = await this.getArgs(options);
|
|
156
177
|
|
|
157
178
|
const body = {
|
|
@@ -170,7 +191,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
170
191
|
rawValue: rawResponse,
|
|
171
192
|
} = await postJsonToApi({
|
|
172
193
|
url,
|
|
173
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
194
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
174
195
|
body,
|
|
175
196
|
failedResponseHandler: huggingfaceFailedResponseHandler,
|
|
176
197
|
successfulResponseHandler: createJsonResponseHandler(
|
|
@@ -192,7 +213,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
192
213
|
});
|
|
193
214
|
}
|
|
194
215
|
|
|
195
|
-
const content: Array<
|
|
216
|
+
const content: Array<LanguageModelV4Content> = [];
|
|
196
217
|
|
|
197
218
|
// Process output array
|
|
198
219
|
for (const part of response.output) {
|
|
@@ -331,8 +352,8 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
331
352
|
}
|
|
332
353
|
|
|
333
354
|
async doStream(
|
|
334
|
-
options:
|
|
335
|
-
): Promise<
|
|
355
|
+
options: LanguageModelV4CallOptions,
|
|
356
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
336
357
|
const { args, warnings } = await this.getArgs(options);
|
|
337
358
|
|
|
338
359
|
const body = {
|
|
@@ -345,7 +366,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
345
366
|
path: '/responses',
|
|
346
367
|
modelId: this.modelId,
|
|
347
368
|
}),
|
|
348
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
369
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
349
370
|
body,
|
|
350
371
|
failedResponseHandler: huggingfaceFailedResponseHandler,
|
|
351
372
|
successfulResponseHandler: createEventSourceResponseHandler(
|
|
@@ -355,7 +376,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
355
376
|
fetch: this.config.fetch,
|
|
356
377
|
});
|
|
357
378
|
|
|
358
|
-
let finishReason:
|
|
379
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
359
380
|
unified: 'other',
|
|
360
381
|
raw: undefined,
|
|
361
382
|
};
|
|
@@ -366,7 +387,7 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
366
387
|
stream: response.pipeThrough(
|
|
367
388
|
new TransformStream<
|
|
368
389
|
ParseResult<z.infer<typeof huggingfaceResponsesChunkSchema>>,
|
|
369
|
-
|
|
390
|
+
LanguageModelV4StreamPart
|
|
370
391
|
>({
|
|
371
392
|
start(controller) {
|
|
372
393
|
controller.enqueue({ type: 'stream-start', warnings });
|
|
@@ -524,13 +545,6 @@ export class HuggingFaceResponsesLanguageModel implements LanguageModelV3 {
|
|
|
524
545
|
}
|
|
525
546
|
}
|
|
526
547
|
|
|
527
|
-
const huggingfaceResponsesProviderOptionsSchema = z.object({
|
|
528
|
-
metadata: z.record(z.string(), z.string()).optional(),
|
|
529
|
-
instructions: z.string().optional(),
|
|
530
|
-
strictJsonSchema: z.boolean().optional(),
|
|
531
|
-
reasoningEffort: z.string().optional(),
|
|
532
|
-
});
|
|
533
|
-
|
|
534
548
|
const huggingfaceResponsesOutputSchema = z.discriminatedUnion('type', [
|
|
535
549
|
z.object({
|
|
536
550
|
type: z.literal('message'),
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {
|
|
2
|
+
LanguageModelV4CallOptions,
|
|
3
|
+
SharedV4Warning,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
2
5
|
|
|
3
6
|
export type HuggingFaceResponsesTool = {
|
|
4
7
|
type: 'function';
|
|
@@ -16,17 +19,17 @@ export function prepareResponsesTools({
|
|
|
16
19
|
tools,
|
|
17
20
|
toolChoice,
|
|
18
21
|
}: {
|
|
19
|
-
tools:
|
|
20
|
-
toolChoice?:
|
|
22
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
23
|
+
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
21
24
|
}): {
|
|
22
25
|
tools?: HuggingFaceResponsesTool[];
|
|
23
26
|
toolChoice?: HuggingFaceResponsesToolChoice;
|
|
24
|
-
toolWarnings:
|
|
27
|
+
toolWarnings: SharedV4Warning[];
|
|
25
28
|
} {
|
|
26
29
|
// when the tools array is empty, change it to undefined to prevent errors:
|
|
27
30
|
tools = tools?.length ? tools : undefined;
|
|
28
31
|
|
|
29
|
-
const toolWarnings:
|
|
32
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
30
33
|
|
|
31
34
|
if (tools == null) {
|
|
32
35
|
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { LanguageModelV4FinishReason } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
export function mapHuggingFaceResponsesFinishReason(
|
|
4
4
|
finishReason: string | null | undefined,
|
|
5
|
-
):
|
|
5
|
+
): LanguageModelV4FinishReason['unified'] {
|
|
6
6
|
switch (finishReason) {
|
|
7
7
|
case 'stop':
|
|
8
8
|
return 'stop';
|
package/src/version.ts
ADDED
package/dist/index.d.mts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { ProviderV3, LanguageModelV3 } from '@ai-sdk/provider';
|
|
2
|
-
import { FetchFunction } from '@ai-sdk/provider-utils';
|
|
3
|
-
export { OpenAICompatibleErrorData as HuggingFaceErrorData } from '@ai-sdk/openai-compatible';
|
|
4
|
-
|
|
5
|
-
type HuggingFaceResponsesModelId = 'meta-llama/Llama-3.1-8B-Instruct' | 'meta-llama/Llama-3.1-70B-Instruct' | 'meta-llama/Llama-3.1-405B-Instruct' | 'meta-llama/Llama-3.3-70B-Instruct' | 'meta-llama/Meta-Llama-3-8B-Instruct' | 'meta-llama/Meta-Llama-3-70B-Instruct' | 'meta-llama/Llama-3.2-3B-Instruct' | 'meta-llama/Llama-4-Maverick-17B-128E-Instruct' | 'meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8' | 'meta-llama/Llama-Guard-4-12B' | 'deepseek-ai/DeepSeek-V3.1' | 'deepseek-ai/DeepSeek-V3-0324' | 'deepseek-ai/DeepSeek-R1' | 'deepseek-ai/DeepSeek-R1-0528' | 'deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B' | 'deepseek-ai/DeepSeek-R1-Distill-Qwen-7B' | 'deepseek-ai/DeepSeek-R1-Distill-Qwen-14B' | 'deepseek-ai/DeepSeek-R1-Distill-Qwen-32B' | 'deepseek-ai/DeepSeek-R1-Distill-Llama-8B' | 'deepseek-ai/DeepSeek-R1-Distill-Llama-70B' | 'deepseek-ai/DeepSeek-Prover-V2-671B' | 'Qwen/Qwen3-32B' | 'Qwen/Qwen3-14B' | 'Qwen/Qwen3-8B' | 'Qwen/Qwen3-4B' | 'Qwen/Qwen3-Coder-480B-A35B-Instruct' | 'Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8' | 'Qwen/Qwen3-30B-A3B' | 'Qwen/Qwen2.5-VL-7B-Instruct' | 'Qwen/Qwen2.5-7B-Instruct' | 'Qwen/Qwen2.5-Coder-7B-Instruct' | 'Qwen/Qwen2.5-Coder-32B-Instruct' | 'google/gemma-2-9b-it' | 'google/gemma-3-27b-it' | 'moonshotai/Kimi-K2-Instruct' | (string & {});
|
|
6
|
-
interface HuggingFaceResponsesSettings {
|
|
7
|
-
metadata?: Record<string, string>;
|
|
8
|
-
instructions?: string;
|
|
9
|
-
strictJsonSchema?: boolean;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface HuggingFaceProviderSettings {
|
|
13
|
-
/**
|
|
14
|
-
* Hugging Face API key.
|
|
15
|
-
*/
|
|
16
|
-
apiKey?: string;
|
|
17
|
-
/**
|
|
18
|
-
* Base URL for the API calls.
|
|
19
|
-
*/
|
|
20
|
-
baseURL?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Custom headers to include in the requests.
|
|
23
|
-
*/
|
|
24
|
-
headers?: Record<string, string>;
|
|
25
|
-
/**
|
|
26
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
27
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
28
|
-
*/
|
|
29
|
-
fetch?: FetchFunction;
|
|
30
|
-
generateId?: () => string;
|
|
31
|
-
}
|
|
32
|
-
interface HuggingFaceProvider extends ProviderV3 {
|
|
33
|
-
/**
|
|
34
|
-
* Creates a Hugging Face responses model for text generation.
|
|
35
|
-
*/
|
|
36
|
-
(modelId: HuggingFaceResponsesModelId): LanguageModelV3;
|
|
37
|
-
/**
|
|
38
|
-
* Creates a Hugging Face responses model for text generation.
|
|
39
|
-
*/
|
|
40
|
-
languageModel(modelId: HuggingFaceResponsesModelId): LanguageModelV3;
|
|
41
|
-
/**
|
|
42
|
-
* Creates a Hugging Face responses model for text generation.
|
|
43
|
-
*/
|
|
44
|
-
responses(modelId: HuggingFaceResponsesModelId): LanguageModelV3;
|
|
45
|
-
/**
|
|
46
|
-
* @deprecated Use `embeddingModel` instead.
|
|
47
|
-
*/
|
|
48
|
-
textEmbeddingModel(modelId: string): never;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Create a Hugging Face provider instance.
|
|
52
|
-
*/
|
|
53
|
-
declare function createHuggingFace(options?: HuggingFaceProviderSettings): HuggingFaceProvider;
|
|
54
|
-
/**
|
|
55
|
-
* Default Hugging Face provider instance.
|
|
56
|
-
*/
|
|
57
|
-
declare const huggingface: HuggingFaceProvider;
|
|
58
|
-
|
|
59
|
-
export { type HuggingFaceProvider, type HuggingFaceProviderSettings, type HuggingFaceResponsesModelId, type HuggingFaceResponsesSettings, createHuggingFace, huggingface };
|