@huggingface/tasks 0.13.5 → 0.13.6
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/commonjs/hardware.d.ts +12 -0
- package/dist/commonjs/hardware.d.ts.map +1 -1
- package/dist/commonjs/hardware.js +15 -3
- package/dist/commonjs/snippets/curl.js +4 -4
- package/dist/commonjs/snippets/js.js +5 -5
- package/dist/commonjs/tasks/audio-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/audio-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/automatic-speech-recognition/inference.d.ts +1 -5
- package/dist/commonjs/tasks/automatic-speech-recognition/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/chat-completion/inference.d.ts +18 -6
- package/dist/commonjs/tasks/chat-completion/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/depth-estimation/inference.d.ts +1 -1
- package/dist/commonjs/tasks/document-question-answering/inference.d.ts +1 -3
- package/dist/commonjs/tasks/document-question-answering/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/feature-extraction/inference.d.ts +1 -1
- package/dist/commonjs/tasks/fill-mask/inference.d.ts +1 -3
- package/dist/commonjs/tasks/fill-mask/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/image-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/image-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/image-segmentation/inference.d.ts +1 -3
- package/dist/commonjs/tasks/image-segmentation/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/image-to-image/inference.d.ts +1 -3
- package/dist/commonjs/tasks/image-to-image/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/image-to-text/inference.d.ts +1 -5
- package/dist/commonjs/tasks/image-to-text/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/object-detection/inference.d.ts +1 -3
- package/dist/commonjs/tasks/object-detection/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/question-answering/inference.d.ts +1 -3
- package/dist/commonjs/tasks/question-answering/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/sentence-similarity/inference.d.ts +1 -1
- package/dist/commonjs/tasks/summarization/inference.d.ts +1 -3
- package/dist/commonjs/tasks/summarization/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/table-question-answering/inference.d.ts +1 -1
- package/dist/commonjs/tasks/text-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/text-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/text-to-audio/inference.d.ts +1 -5
- package/dist/commonjs/tasks/text-to-audio/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/text-to-image/inference.d.ts +1 -3
- package/dist/commonjs/tasks/text-to-image/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/text-to-speech/inference.d.ts +1 -5
- package/dist/commonjs/tasks/text-to-speech/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/text2text-generation/inference.d.ts +1 -3
- package/dist/commonjs/tasks/text2text-generation/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/token-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/token-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/translation/inference.d.ts +1 -3
- package/dist/commonjs/tasks/translation/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/video-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/video-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/visual-question-answering/inference.d.ts +1 -3
- package/dist/commonjs/tasks/visual-question-answering/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/zero-shot-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/zero-shot-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/zero-shot-image-classification/inference.d.ts +1 -3
- package/dist/commonjs/tasks/zero-shot-image-classification/inference.d.ts.map +1 -1
- package/dist/commonjs/tasks/zero-shot-object-detection/inference.d.ts +1 -3
- package/dist/commonjs/tasks/zero-shot-object-detection/inference.d.ts.map +1 -1
- package/dist/esm/hardware.d.ts +12 -0
- package/dist/esm/hardware.d.ts.map +1 -1
- package/dist/esm/hardware.js +15 -3
- package/dist/esm/snippets/curl.js +4 -4
- package/dist/esm/snippets/js.js +5 -5
- package/dist/esm/tasks/audio-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/audio-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/automatic-speech-recognition/inference.d.ts +1 -5
- package/dist/esm/tasks/automatic-speech-recognition/inference.d.ts.map +1 -1
- package/dist/esm/tasks/chat-completion/inference.d.ts +18 -6
- package/dist/esm/tasks/chat-completion/inference.d.ts.map +1 -1
- package/dist/esm/tasks/depth-estimation/inference.d.ts +1 -1
- package/dist/esm/tasks/document-question-answering/inference.d.ts +1 -3
- package/dist/esm/tasks/document-question-answering/inference.d.ts.map +1 -1
- package/dist/esm/tasks/feature-extraction/inference.d.ts +1 -1
- package/dist/esm/tasks/fill-mask/inference.d.ts +1 -3
- package/dist/esm/tasks/fill-mask/inference.d.ts.map +1 -1
- package/dist/esm/tasks/image-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/image-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/image-segmentation/inference.d.ts +1 -3
- package/dist/esm/tasks/image-segmentation/inference.d.ts.map +1 -1
- package/dist/esm/tasks/image-to-image/inference.d.ts +1 -3
- package/dist/esm/tasks/image-to-image/inference.d.ts.map +1 -1
- package/dist/esm/tasks/image-to-text/inference.d.ts +1 -5
- package/dist/esm/tasks/image-to-text/inference.d.ts.map +1 -1
- package/dist/esm/tasks/object-detection/inference.d.ts +1 -3
- package/dist/esm/tasks/object-detection/inference.d.ts.map +1 -1
- package/dist/esm/tasks/question-answering/inference.d.ts +1 -3
- package/dist/esm/tasks/question-answering/inference.d.ts.map +1 -1
- package/dist/esm/tasks/sentence-similarity/inference.d.ts +1 -1
- package/dist/esm/tasks/summarization/inference.d.ts +1 -3
- package/dist/esm/tasks/summarization/inference.d.ts.map +1 -1
- package/dist/esm/tasks/table-question-answering/inference.d.ts +1 -1
- package/dist/esm/tasks/text-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/text-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/text-to-audio/inference.d.ts +1 -5
- package/dist/esm/tasks/text-to-audio/inference.d.ts.map +1 -1
- package/dist/esm/tasks/text-to-image/inference.d.ts +1 -3
- package/dist/esm/tasks/text-to-image/inference.d.ts.map +1 -1
- package/dist/esm/tasks/text-to-speech/inference.d.ts +1 -5
- package/dist/esm/tasks/text-to-speech/inference.d.ts.map +1 -1
- package/dist/esm/tasks/text2text-generation/inference.d.ts +1 -3
- package/dist/esm/tasks/text2text-generation/inference.d.ts.map +1 -1
- package/dist/esm/tasks/token-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/token-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/translation/inference.d.ts +1 -3
- package/dist/esm/tasks/translation/inference.d.ts.map +1 -1
- package/dist/esm/tasks/video-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/video-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/visual-question-answering/inference.d.ts +1 -3
- package/dist/esm/tasks/visual-question-answering/inference.d.ts.map +1 -1
- package/dist/esm/tasks/zero-shot-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/zero-shot-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/zero-shot-image-classification/inference.d.ts +1 -3
- package/dist/esm/tasks/zero-shot-image-classification/inference.d.ts.map +1 -1
- package/dist/esm/tasks/zero-shot-object-detection/inference.d.ts +1 -3
- package/dist/esm/tasks/zero-shot-object-detection/inference.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hardware.ts +15 -3
- package/src/snippets/curl.ts +4 -4
- package/src/snippets/js.ts +5 -5
- package/src/tasks/audio-classification/inference.ts +1 -3
- package/src/tasks/audio-classification/spec/input.json +1 -2
- package/src/tasks/automatic-speech-recognition/inference.ts +1 -5
- package/src/tasks/automatic-speech-recognition/spec/input.json +1 -2
- package/src/tasks/chat-completion/inference.ts +19 -6
- package/src/tasks/chat-completion/spec/input.json +14 -19
- package/src/tasks/common-definitions.json +0 -1
- package/src/tasks/depth-estimation/inference.ts +1 -1
- package/src/tasks/depth-estimation/spec/input.json +1 -2
- package/src/tasks/document-question-answering/inference.ts +1 -3
- package/src/tasks/document-question-answering/spec/input.json +1 -2
- package/src/tasks/feature-extraction/inference.ts +1 -1
- package/src/tasks/feature-extraction/spec/input.json +1 -1
- package/src/tasks/fill-mask/inference.ts +1 -3
- package/src/tasks/fill-mask/spec/input.json +1 -2
- package/src/tasks/image-classification/inference.ts +1 -3
- package/src/tasks/image-classification/spec/input.json +1 -2
- package/src/tasks/image-segmentation/inference.ts +1 -3
- package/src/tasks/image-segmentation/spec/input.json +1 -2
- package/src/tasks/image-to-image/inference.ts +1 -3
- package/src/tasks/image-to-image/spec/input.json +1 -2
- package/src/tasks/image-to-text/inference.ts +1 -5
- package/src/tasks/image-to-text/spec/input.json +1 -2
- package/src/tasks/object-detection/inference.ts +1 -3
- package/src/tasks/object-detection/spec/input.json +1 -2
- package/src/tasks/placeholder/spec/input.json +1 -2
- package/src/tasks/question-answering/inference.ts +1 -3
- package/src/tasks/question-answering/spec/input.json +1 -2
- package/src/tasks/sentence-similarity/inference.ts +1 -1
- package/src/tasks/sentence-similarity/spec/input.json +1 -2
- package/src/tasks/summarization/inference.ts +1 -3
- package/src/tasks/summarization/spec/input.json +1 -2
- package/src/tasks/table-question-answering/inference.ts +1 -1
- package/src/tasks/table-question-answering/spec/input.json +1 -2
- package/src/tasks/text-classification/inference.ts +1 -3
- package/src/tasks/text-classification/spec/input.json +1 -2
- package/src/tasks/text-to-audio/inference.ts +1 -5
- package/src/tasks/text-to-audio/spec/input.json +1 -2
- package/src/tasks/text-to-image/inference.ts +1 -3
- package/src/tasks/text-to-image/spec/input.json +1 -2
- package/src/tasks/text-to-speech/inference.ts +1 -5
- package/src/tasks/text-to-speech/spec/input.json +1 -2
- package/src/tasks/text2text-generation/inference.ts +1 -3
- package/src/tasks/text2text-generation/spec/input.json +1 -2
- package/src/tasks/token-classification/inference.ts +1 -3
- package/src/tasks/token-classification/spec/input.json +1 -2
- package/src/tasks/translation/inference.ts +1 -3
- package/src/tasks/translation/spec/input.json +1 -2
- package/src/tasks/video-classification/inference.ts +1 -3
- package/src/tasks/video-classification/spec/input.json +1 -2
- package/src/tasks/visual-question-answering/inference.ts +1 -3
- package/src/tasks/visual-question-answering/spec/input.json +1 -2
- package/src/tasks/zero-shot-classification/inference.ts +1 -3
- package/src/tasks/zero-shot-classification/spec/input.json +1 -2
- package/src/tasks/zero-shot-image-classification/inference.ts +1 -3
- package/src/tasks/zero-shot-image-classification/spec/input.json +1 -2
- package/src/tasks/zero-shot-object-detection/inference.ts +1 -3
- package/src/tasks/zero-shot-object-detection/spec/input.json +1 -2
- package/dist/commonjs/snippets/curl.spec.d.ts +0 -2
- package/dist/commonjs/snippets/curl.spec.d.ts.map +0 -1
- package/dist/commonjs/snippets/curl.spec.js +0 -89
- package/dist/commonjs/snippets/js.spec.d.ts +0 -2
- package/dist/commonjs/snippets/js.spec.d.ts.map +0 -1
- package/dist/commonjs/snippets/js.spec.js +0 -141
- package/dist/commonjs/snippets/python.spec.d.ts +0 -2
- package/dist/commonjs/snippets/python.spec.d.ts.map +0 -1
- package/dist/commonjs/snippets/python.spec.js +0 -135
- package/dist/esm/snippets/curl.spec.d.ts +0 -2
- package/dist/esm/snippets/curl.spec.d.ts.map +0 -1
- package/dist/esm/snippets/curl.spec.js +0 -87
- package/dist/esm/snippets/js.spec.d.ts +0 -2
- package/dist/esm/snippets/js.spec.d.ts.map +0 -1
- package/dist/esm/snippets/js.spec.js +0 -139
- package/dist/esm/snippets/python.spec.d.ts +0 -2
- package/dist/esm/snippets/python.spec.d.ts.map +0 -1
- package/dist/esm/snippets/python.spec.js +0 -133
- package/src/snippets/curl.spec.ts +0 -94
- package/src/snippets/js.spec.ts +0 -148
- package/src/snippets/python.spec.ts +0 -144
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type { ModelDataMinimal } from "./types.js";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { getCurlInferenceSnippet } from "./curl.js";
|
|
4
|
-
|
|
5
|
-
describe("inference API snippets", () => {
|
|
6
|
-
it("conversational llm", async () => {
|
|
7
|
-
const model: ModelDataMinimal = {
|
|
8
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
9
|
-
pipeline_tag: "text-generation",
|
|
10
|
-
tags: ["conversational"],
|
|
11
|
-
inference: "",
|
|
12
|
-
};
|
|
13
|
-
const snippet = getCurlInferenceSnippet(model, "api_token");
|
|
14
|
-
|
|
15
|
-
expect(snippet.content)
|
|
16
|
-
.toEqual(`curl 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \\
|
|
17
|
-
-H "Authorization: Bearer api_token" \\
|
|
18
|
-
-H 'Content-Type: application/json' \\
|
|
19
|
-
--data '{
|
|
20
|
-
"model": "meta-llama/Llama-3.1-8B-Instruct",
|
|
21
|
-
"messages": [
|
|
22
|
-
{
|
|
23
|
-
"role": "user",
|
|
24
|
-
"content": "What is the capital of France?"
|
|
25
|
-
}
|
|
26
|
-
],
|
|
27
|
-
"max_tokens": 500,
|
|
28
|
-
"stream": true
|
|
29
|
-
}'`);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("conversational llm non-streaming", async () => {
|
|
33
|
-
const model: ModelDataMinimal = {
|
|
34
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
35
|
-
pipeline_tag: "text-generation",
|
|
36
|
-
tags: ["conversational"],
|
|
37
|
-
inference: "",
|
|
38
|
-
};
|
|
39
|
-
const snippet = getCurlInferenceSnippet(model, "api_token", { streaming: false });
|
|
40
|
-
|
|
41
|
-
expect(snippet.content)
|
|
42
|
-
.toEqual(`curl 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct/v1/chat/completions' \\
|
|
43
|
-
-H "Authorization: Bearer api_token" \\
|
|
44
|
-
-H 'Content-Type: application/json' \\
|
|
45
|
-
--data '{
|
|
46
|
-
"model": "meta-llama/Llama-3.1-8B-Instruct",
|
|
47
|
-
"messages": [
|
|
48
|
-
{
|
|
49
|
-
"role": "user",
|
|
50
|
-
"content": "What is the capital of France?"
|
|
51
|
-
}
|
|
52
|
-
],
|
|
53
|
-
"max_tokens": 500,
|
|
54
|
-
"stream": false
|
|
55
|
-
}'`);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("conversational vlm", async () => {
|
|
59
|
-
const model: ModelDataMinimal = {
|
|
60
|
-
id: "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
61
|
-
pipeline_tag: "image-text-to-text",
|
|
62
|
-
tags: ["conversational"],
|
|
63
|
-
inference: "",
|
|
64
|
-
};
|
|
65
|
-
const snippet = getCurlInferenceSnippet(model, "api_token");
|
|
66
|
-
|
|
67
|
-
expect(snippet.content)
|
|
68
|
-
.toEqual(`curl 'https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-11B-Vision-Instruct/v1/chat/completions' \\
|
|
69
|
-
-H "Authorization: Bearer api_token" \\
|
|
70
|
-
-H 'Content-Type: application/json' \\
|
|
71
|
-
--data '{
|
|
72
|
-
"model": "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
73
|
-
"messages": [
|
|
74
|
-
{
|
|
75
|
-
"role": "user",
|
|
76
|
-
"content": [
|
|
77
|
-
{
|
|
78
|
-
"type": "text",
|
|
79
|
-
"text": "Describe this image in one sentence."
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
"type": "image_url",
|
|
83
|
-
"image_url": {
|
|
84
|
-
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
]
|
|
88
|
-
}
|
|
89
|
-
],
|
|
90
|
-
"max_tokens": 500,
|
|
91
|
-
"stream": true
|
|
92
|
-
}'`);
|
|
93
|
-
});
|
|
94
|
-
});
|
package/src/snippets/js.spec.ts
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { getJsInferenceSnippet } from "./js.js";
|
|
4
|
-
|
|
5
|
-
describe("inference API snippets", () => {
|
|
6
|
-
it("conversational llm", async () => {
|
|
7
|
-
const model: ModelDataMinimal = {
|
|
8
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
9
|
-
pipeline_tag: "text-generation",
|
|
10
|
-
tags: ["conversational"],
|
|
11
|
-
inference: "",
|
|
12
|
-
};
|
|
13
|
-
const snippet = getJsInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
14
|
-
|
|
15
|
-
expect(snippet[0].content).toEqual(`import { HfInference } from "@huggingface/inference";
|
|
16
|
-
|
|
17
|
-
const client = new HfInference("api_token");
|
|
18
|
-
|
|
19
|
-
let out = "";
|
|
20
|
-
|
|
21
|
-
const stream = client.chatCompletionStream({
|
|
22
|
-
model: "meta-llama/Llama-3.1-8B-Instruct",
|
|
23
|
-
messages: [
|
|
24
|
-
{
|
|
25
|
-
role: "user",
|
|
26
|
-
content: "What is the capital of France?"
|
|
27
|
-
}
|
|
28
|
-
],
|
|
29
|
-
max_tokens: 500
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
for await (const chunk of stream) {
|
|
33
|
-
if (chunk.choices && chunk.choices.length > 0) {
|
|
34
|
-
const newContent = chunk.choices[0].delta.content;
|
|
35
|
-
out += newContent;
|
|
36
|
-
console.log(newContent);
|
|
37
|
-
}
|
|
38
|
-
}`);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("conversational llm non-streaming", async () => {
|
|
42
|
-
const model: ModelDataMinimal = {
|
|
43
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
44
|
-
pipeline_tag: "text-generation",
|
|
45
|
-
tags: ["conversational"],
|
|
46
|
-
inference: "",
|
|
47
|
-
};
|
|
48
|
-
const snippet = getJsInferenceSnippet(model, "api_token", { streaming: false }) as InferenceSnippet[];
|
|
49
|
-
|
|
50
|
-
expect(snippet[0].content).toEqual(`import { HfInference } from "@huggingface/inference";
|
|
51
|
-
|
|
52
|
-
const client = new HfInference("api_token");
|
|
53
|
-
|
|
54
|
-
const chatCompletion = await client.chatCompletion({
|
|
55
|
-
model: "meta-llama/Llama-3.1-8B-Instruct",
|
|
56
|
-
messages: [
|
|
57
|
-
{
|
|
58
|
-
role: "user",
|
|
59
|
-
content: "What is the capital of France?"
|
|
60
|
-
}
|
|
61
|
-
],
|
|
62
|
-
max_tokens: 500
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
console.log(chatCompletion.choices[0].message);`);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("conversational vlm", async () => {
|
|
69
|
-
const model: ModelDataMinimal = {
|
|
70
|
-
id: "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
71
|
-
pipeline_tag: "image-text-to-text",
|
|
72
|
-
tags: ["conversational"],
|
|
73
|
-
inference: "",
|
|
74
|
-
};
|
|
75
|
-
const snippet = getJsInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
76
|
-
|
|
77
|
-
expect(snippet[0].content).toEqual(`import { HfInference } from "@huggingface/inference";
|
|
78
|
-
|
|
79
|
-
const client = new HfInference("api_token");
|
|
80
|
-
|
|
81
|
-
let out = "";
|
|
82
|
-
|
|
83
|
-
const stream = client.chatCompletionStream({
|
|
84
|
-
model: "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
85
|
-
messages: [
|
|
86
|
-
{
|
|
87
|
-
role: "user",
|
|
88
|
-
content: [
|
|
89
|
-
{
|
|
90
|
-
type: "text",
|
|
91
|
-
text: "Describe this image in one sentence."
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
type: "image_url",
|
|
95
|
-
image_url: {
|
|
96
|
-
url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
]
|
|
100
|
-
}
|
|
101
|
-
],
|
|
102
|
-
max_tokens: 500
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
for await (const chunk of stream) {
|
|
106
|
-
if (chunk.choices && chunk.choices.length > 0) {
|
|
107
|
-
const newContent = chunk.choices[0].delta.content;
|
|
108
|
-
out += newContent;
|
|
109
|
-
console.log(newContent);
|
|
110
|
-
}
|
|
111
|
-
}`);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("conversational llm", async () => {
|
|
115
|
-
const model: ModelDataMinimal = {
|
|
116
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
117
|
-
pipeline_tag: "text-generation",
|
|
118
|
-
tags: ["conversational"],
|
|
119
|
-
inference: "",
|
|
120
|
-
};
|
|
121
|
-
const snippet = getJsInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
122
|
-
|
|
123
|
-
expect(snippet[0].content).toEqual(`import { HfInference } from "@huggingface/inference";
|
|
124
|
-
|
|
125
|
-
const client = new HfInference("api_token");
|
|
126
|
-
|
|
127
|
-
let out = "";
|
|
128
|
-
|
|
129
|
-
const stream = client.chatCompletionStream({
|
|
130
|
-
model: "meta-llama/Llama-3.1-8B-Instruct",
|
|
131
|
-
messages: [
|
|
132
|
-
{
|
|
133
|
-
role: "user",
|
|
134
|
-
content: "What is the capital of France?"
|
|
135
|
-
}
|
|
136
|
-
],
|
|
137
|
-
max_tokens: 500
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
for await (const chunk of stream) {
|
|
141
|
-
if (chunk.choices && chunk.choices.length > 0) {
|
|
142
|
-
const newContent = chunk.choices[0].delta.content;
|
|
143
|
-
out += newContent;
|
|
144
|
-
console.log(newContent);
|
|
145
|
-
}
|
|
146
|
-
}`);
|
|
147
|
-
});
|
|
148
|
-
});
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import { getPythonInferenceSnippet } from "./python.js";
|
|
4
|
-
|
|
5
|
-
describe("inference API snippets", () => {
|
|
6
|
-
it("conversational llm", async () => {
|
|
7
|
-
const model: ModelDataMinimal = {
|
|
8
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
9
|
-
pipeline_tag: "text-generation",
|
|
10
|
-
tags: ["conversational"],
|
|
11
|
-
inference: "",
|
|
12
|
-
};
|
|
13
|
-
const snippet = getPythonInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
14
|
-
|
|
15
|
-
expect(snippet[0].content).toEqual(`from huggingface_hub import InferenceClient
|
|
16
|
-
|
|
17
|
-
client = InferenceClient(api_key="api_token")
|
|
18
|
-
|
|
19
|
-
messages = [
|
|
20
|
-
{
|
|
21
|
-
"role": "user",
|
|
22
|
-
"content": "What is the capital of France?"
|
|
23
|
-
}
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
stream = client.chat.completions.create(
|
|
27
|
-
model="meta-llama/Llama-3.1-8B-Instruct",
|
|
28
|
-
messages=messages,
|
|
29
|
-
max_tokens=500,
|
|
30
|
-
stream=True
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
for chunk in stream:
|
|
34
|
-
print(chunk.choices[0].delta.content, end="")`);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("conversational llm non-streaming", async () => {
|
|
38
|
-
const model: ModelDataMinimal = {
|
|
39
|
-
id: "meta-llama/Llama-3.1-8B-Instruct",
|
|
40
|
-
pipeline_tag: "text-generation",
|
|
41
|
-
tags: ["conversational"],
|
|
42
|
-
inference: "",
|
|
43
|
-
};
|
|
44
|
-
const snippet = getPythonInferenceSnippet(model, "api_token", { streaming: false }) as InferenceSnippet[];
|
|
45
|
-
|
|
46
|
-
expect(snippet[0].content).toEqual(`from huggingface_hub import InferenceClient
|
|
47
|
-
|
|
48
|
-
client = InferenceClient(api_key="api_token")
|
|
49
|
-
|
|
50
|
-
messages = [
|
|
51
|
-
{
|
|
52
|
-
"role": "user",
|
|
53
|
-
"content": "What is the capital of France?"
|
|
54
|
-
}
|
|
55
|
-
]
|
|
56
|
-
|
|
57
|
-
completion = client.chat.completions.create(
|
|
58
|
-
model="meta-llama/Llama-3.1-8B-Instruct",
|
|
59
|
-
messages=messages,
|
|
60
|
-
max_tokens=500
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
print(completion.choices[0].message)`);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("conversational vlm", async () => {
|
|
67
|
-
const model: ModelDataMinimal = {
|
|
68
|
-
id: "meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
69
|
-
pipeline_tag: "image-text-to-text",
|
|
70
|
-
tags: ["conversational"],
|
|
71
|
-
inference: "",
|
|
72
|
-
};
|
|
73
|
-
const snippet = getPythonInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
74
|
-
|
|
75
|
-
expect(snippet[0].content).toEqual(`from huggingface_hub import InferenceClient
|
|
76
|
-
|
|
77
|
-
client = InferenceClient(api_key="api_token")
|
|
78
|
-
|
|
79
|
-
messages = [
|
|
80
|
-
{
|
|
81
|
-
"role": "user",
|
|
82
|
-
"content": [
|
|
83
|
-
{
|
|
84
|
-
"type": "text",
|
|
85
|
-
"text": "Describe this image in one sentence."
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
"type": "image_url",
|
|
89
|
-
"image_url": {
|
|
90
|
-
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
]
|
|
94
|
-
}
|
|
95
|
-
]
|
|
96
|
-
|
|
97
|
-
stream = client.chat.completions.create(
|
|
98
|
-
model="meta-llama/Llama-3.2-11B-Vision-Instruct",
|
|
99
|
-
messages=messages,
|
|
100
|
-
max_tokens=500,
|
|
101
|
-
stream=True
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
for chunk in stream:
|
|
105
|
-
print(chunk.choices[0].delta.content, end="")`);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("text-to-image", async () => {
|
|
109
|
-
const model: ModelDataMinimal = {
|
|
110
|
-
id: "black-forest-labs/FLUX.1-schnell",
|
|
111
|
-
pipeline_tag: "text-to-image",
|
|
112
|
-
tags: [],
|
|
113
|
-
inference: "",
|
|
114
|
-
};
|
|
115
|
-
const snippets = getPythonInferenceSnippet(model, "api_token") as InferenceSnippet[];
|
|
116
|
-
|
|
117
|
-
expect(snippets.length).toEqual(2);
|
|
118
|
-
|
|
119
|
-
expect(snippets[0].client).toEqual("huggingface_hub");
|
|
120
|
-
expect(snippets[0].content).toEqual(`from huggingface_hub import InferenceClient
|
|
121
|
-
client = InferenceClient("black-forest-labs/FLUX.1-schnell", token="api_token")
|
|
122
|
-
|
|
123
|
-
# output is a PIL.Image object
|
|
124
|
-
image = client.text_to_image("Astronaut riding a horse")`);
|
|
125
|
-
|
|
126
|
-
expect(snippets[1].client).toEqual("requests");
|
|
127
|
-
expect(snippets[1].content).toEqual(`import requests
|
|
128
|
-
|
|
129
|
-
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell"
|
|
130
|
-
headers = {"Authorization": "Bearer api_token"}
|
|
131
|
-
|
|
132
|
-
def query(payload):
|
|
133
|
-
response = requests.post(API_URL, headers=headers, json=payload)
|
|
134
|
-
return response.content
|
|
135
|
-
image_bytes = query({
|
|
136
|
-
"inputs": "Astronaut riding a horse",
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
# You can access the image with PIL.Image for example
|
|
140
|
-
import io
|
|
141
|
-
from PIL import Image
|
|
142
|
-
image = Image.open(io.BytesIO(image_bytes))`);
|
|
143
|
-
});
|
|
144
|
-
});
|