|
| 1 | +import type { InferenceSnippet, ModelDataMinimal } from "./types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { snippetTextGeneration } from "./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 = snippetTextGeneration(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 vlm", async () => { |
| 42 | + const model: ModelDataMinimal = { |
| 43 | + id: "meta-llama/Llama-3.2-11B-Vision-Instruct", |
| 44 | + pipeline_tag: "image-text-to-text", |
| 45 | + tags: ["conversational"], |
| 46 | + inference: "", |
| 47 | + }; |
| 48 | + const snippet = snippetTextGeneration(model, "api_token") as InferenceSnippet[]; |
| 49 | + |
| 50 | + expect(snippet[0].content).toEqual(`import { HfInference } from "@huggingface/inference" |
| 51 | +
|
| 52 | +const client = new HfInference("api_token") |
| 53 | +
|
| 54 | +let out = ""; |
| 55 | +
|
| 56 | +const stream = client.chatCompletionStream({ |
| 57 | + model: "meta-llama/Llama-3.2-11B-Vision-Instruct", |
| 58 | + messages: [ |
| 59 | + { |
| 60 | + role: "user", |
| 61 | + content: [ |
| 62 | + { |
| 63 | + type: "text", |
| 64 | + text: "Describe this image in one sentence." |
| 65 | + }, |
| 66 | + { |
| 67 | + type: "image_url", |
| 68 | + image_url: { |
| 69 | + url: "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | + ], |
| 75 | + max_tokens: 500 |
| 76 | +}); |
| 77 | +
|
| 78 | +for await (const chunk of stream) { |
| 79 | + if (chunk.choices && chunk.choices.length > 0) { |
| 80 | + const newContent = chunk.choices[0].delta.content; |
| 81 | + out += newContent; |
| 82 | + console.log(newContent); |
| 83 | + } |
| 84 | +}`); |
| 85 | + }); |
| 86 | +}); |
0 commit comments