@ai-sdk/huggingface 1.0.16 → 1.0.18
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 +14 -0
- package/docs/170-huggingface.mdx +119 -0
- package/package.json +9 -4
- package/src/huggingface-config.ts +9 -0
- package/src/huggingface-error.ts +17 -0
- package/src/huggingface-provider.test.ts +62 -0
- package/src/huggingface-provider.ts +119 -0
- package/src/index.ts +10 -0
- package/src/responses/convert-huggingface-responses-usage.ts +54 -0
- package/src/responses/convert-to-huggingface-responses-messages.ts +111 -0
- package/src/responses/huggingface-responses-language-model.test.ts +1544 -0
- package/src/responses/huggingface-responses-language-model.ts +826 -0
- package/src/responses/huggingface-responses-prepare-tools.ts +91 -0
- package/src/responses/huggingface-responses-settings.ts +44 -0
- package/src/responses/map-huggingface-responses-finish-reason.ts +20 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { LanguageModelV3CallOptions, SharedV3Warning } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
export type HuggingFaceResponsesTool = {
|
|
4
|
+
type: 'function';
|
|
5
|
+
name: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
parameters: unknown;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type HuggingFaceResponsesToolChoice =
|
|
11
|
+
| 'auto'
|
|
12
|
+
| 'required'
|
|
13
|
+
| { type: 'function'; function: { name: string } };
|
|
14
|
+
|
|
15
|
+
export function prepareResponsesTools({
|
|
16
|
+
tools,
|
|
17
|
+
toolChoice,
|
|
18
|
+
}: {
|
|
19
|
+
tools: LanguageModelV3CallOptions['tools'];
|
|
20
|
+
toolChoice?: LanguageModelV3CallOptions['toolChoice'];
|
|
21
|
+
}): {
|
|
22
|
+
tools?: HuggingFaceResponsesTool[];
|
|
23
|
+
toolChoice?: HuggingFaceResponsesToolChoice;
|
|
24
|
+
toolWarnings: SharedV3Warning[];
|
|
25
|
+
} {
|
|
26
|
+
// when the tools array is empty, change it to undefined to prevent errors:
|
|
27
|
+
tools = tools?.length ? tools : undefined;
|
|
28
|
+
|
|
29
|
+
const toolWarnings: SharedV3Warning[] = [];
|
|
30
|
+
|
|
31
|
+
if (tools == null) {
|
|
32
|
+
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const huggingfaceTools: HuggingFaceResponsesTool[] = [];
|
|
36
|
+
|
|
37
|
+
for (const tool of tools) {
|
|
38
|
+
switch (tool.type) {
|
|
39
|
+
case 'function':
|
|
40
|
+
huggingfaceTools.push({
|
|
41
|
+
type: 'function',
|
|
42
|
+
name: tool.name,
|
|
43
|
+
description: tool.description,
|
|
44
|
+
parameters: tool.inputSchema,
|
|
45
|
+
});
|
|
46
|
+
break;
|
|
47
|
+
case 'provider':
|
|
48
|
+
toolWarnings.push({
|
|
49
|
+
type: 'unsupported',
|
|
50
|
+
feature: `provider-defined tool ${tool.id}`,
|
|
51
|
+
});
|
|
52
|
+
break;
|
|
53
|
+
default: {
|
|
54
|
+
const _exhaustiveCheck: never = tool;
|
|
55
|
+
throw new Error(`Unsupported tool type: ${_exhaustiveCheck}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// prepare tool choice:
|
|
61
|
+
let mappedToolChoice: HuggingFaceResponsesToolChoice | undefined = undefined;
|
|
62
|
+
if (toolChoice) {
|
|
63
|
+
switch (toolChoice.type) {
|
|
64
|
+
case 'auto':
|
|
65
|
+
mappedToolChoice = 'auto';
|
|
66
|
+
break;
|
|
67
|
+
case 'required':
|
|
68
|
+
mappedToolChoice = 'required';
|
|
69
|
+
break;
|
|
70
|
+
case 'none':
|
|
71
|
+
// not supported, ignore
|
|
72
|
+
break;
|
|
73
|
+
case 'tool':
|
|
74
|
+
mappedToolChoice = {
|
|
75
|
+
type: 'function',
|
|
76
|
+
function: { name: toolChoice.toolName },
|
|
77
|
+
};
|
|
78
|
+
break;
|
|
79
|
+
default: {
|
|
80
|
+
const _exhaustiveCheck: never = toolChoice;
|
|
81
|
+
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
tools: huggingfaceTools,
|
|
88
|
+
toolChoice: mappedToolChoice,
|
|
89
|
+
toolWarnings,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type HuggingFaceResponsesModelId =
|
|
2
|
+
// https://router.huggingface.co/v1/models
|
|
3
|
+
| 'meta-llama/Llama-3.1-8B-Instruct'
|
|
4
|
+
| 'meta-llama/Llama-3.1-70B-Instruct'
|
|
5
|
+
| 'meta-llama/Llama-3.1-405B-Instruct'
|
|
6
|
+
| 'meta-llama/Llama-3.3-70B-Instruct'
|
|
7
|
+
| 'meta-llama/Meta-Llama-3-8B-Instruct'
|
|
8
|
+
| 'meta-llama/Meta-Llama-3-70B-Instruct'
|
|
9
|
+
| 'meta-llama/Llama-3.2-3B-Instruct'
|
|
10
|
+
| 'meta-llama/Llama-4-Maverick-17B-128E-Instruct'
|
|
11
|
+
| 'meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8'
|
|
12
|
+
| 'meta-llama/Llama-Guard-4-12B'
|
|
13
|
+
| 'deepseek-ai/DeepSeek-V3.1'
|
|
14
|
+
| 'deepseek-ai/DeepSeek-V3-0324'
|
|
15
|
+
| 'deepseek-ai/DeepSeek-R1'
|
|
16
|
+
| 'deepseek-ai/DeepSeek-R1-0528'
|
|
17
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B'
|
|
18
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Qwen-7B'
|
|
19
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Qwen-14B'
|
|
20
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Qwen-32B'
|
|
21
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Llama-8B'
|
|
22
|
+
| 'deepseek-ai/DeepSeek-R1-Distill-Llama-70B'
|
|
23
|
+
| 'deepseek-ai/DeepSeek-Prover-V2-671B'
|
|
24
|
+
| 'Qwen/Qwen3-32B'
|
|
25
|
+
| 'Qwen/Qwen3-14B'
|
|
26
|
+
| 'Qwen/Qwen3-8B'
|
|
27
|
+
| 'Qwen/Qwen3-4B'
|
|
28
|
+
| 'Qwen/Qwen3-Coder-480B-A35B-Instruct'
|
|
29
|
+
| 'Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8'
|
|
30
|
+
| 'Qwen/Qwen3-30B-A3B'
|
|
31
|
+
| 'Qwen/Qwen2.5-VL-7B-Instruct'
|
|
32
|
+
| 'Qwen/Qwen2.5-7B-Instruct'
|
|
33
|
+
| 'Qwen/Qwen2.5-Coder-7B-Instruct'
|
|
34
|
+
| 'Qwen/Qwen2.5-Coder-32B-Instruct'
|
|
35
|
+
| 'google/gemma-2-9b-it'
|
|
36
|
+
| 'google/gemma-3-27b-it'
|
|
37
|
+
| 'moonshotai/Kimi-K2-Instruct'
|
|
38
|
+
| (string & {});
|
|
39
|
+
|
|
40
|
+
export interface HuggingFaceResponsesSettings {
|
|
41
|
+
metadata?: Record<string, string>;
|
|
42
|
+
instructions?: string;
|
|
43
|
+
strictJsonSchema?: boolean;
|
|
44
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { LanguageModelV3FinishReason } from '@ai-sdk/provider';
|
|
2
|
+
|
|
3
|
+
export function mapHuggingFaceResponsesFinishReason(
|
|
4
|
+
finishReason: string | null | undefined,
|
|
5
|
+
): LanguageModelV3FinishReason['unified'] {
|
|
6
|
+
switch (finishReason) {
|
|
7
|
+
case 'stop':
|
|
8
|
+
return 'stop';
|
|
9
|
+
case 'length':
|
|
10
|
+
return 'length';
|
|
11
|
+
case 'content_filter':
|
|
12
|
+
return 'content-filter';
|
|
13
|
+
case 'tool_calls':
|
|
14
|
+
return 'tool-calls';
|
|
15
|
+
case 'error':
|
|
16
|
+
return 'error';
|
|
17
|
+
default:
|
|
18
|
+
return 'other';
|
|
19
|
+
}
|
|
20
|
+
}
|