Skip to content

Commit

Permalink
Move inference snippets logic to inference package (#1247)
Browse files Browse the repository at this point in the history
_Original slack message from @Wauplin ([private
link](https://huggingface.slack.com/archives/C04PJ0H35UM/p1740673808170509)):_

> I have an annoying problem I don't know how to handle in
`@hugingface.js`.
I'm working on updating the inference snippets under `@tasks` . Now that
we have a `makeUrl` helper for each provider (see [cohere
example](https://github.com/huggingface/huggingface.js/blob/b5230f9cbe71348014f39fd3a8149979cc10c196/packages/inference/src/providers/cohere.ts#L34)),
I want to use it in the CURL snippets to display to correct URL (for now
it's hardcoded
[here](https://github.com/huggingface/huggingface.js/blob/b5230f9cbe71348014f39fd3a8149979cc10c196/packages/tasks/src/snippets/curl.ts#L46)
which is incorrect for some providers). To do that, I need to make
`@tasks` depend on `@inference` . But `@inference` is already depending
on `@tasks` especially to type inference inputs/outputs. Is this
something npm/pnpm is able to settle (in Python it's not ^^).
> 
> The other solution I'm thinking about is to start having a `@snippets`
package depending on both `@tasks` and `@inference` but I'd like to
avoid that as much as possible 🙈 Any idea?

=> after some discussions we went for "let's move the `snippets` code to
`inference`" which this PR does. @julien-c @coyotte508

⚠️ **This is a breaking change for @huggingface.js/tasks.** Will require
to make a major release?

----

In practice I had to move only parts of the `./snippets` folder:
- the type definitions + utils remains in `@tasks` since it is used to
generate some local app snippets
- only the js/python/curl -specific parts are moved to `@inference` =>
i.e. only the logic to generate the snippets, not the helpers
  • Loading branch information
Wauplin authored Mar 4, 2025
1 parent 412cd82 commit d05b840
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion e2e/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2015",
"target": "ES2022",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
Expand Down
3 changes: 3 additions & 0 deletions packages/inference/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ export { HfInference, HfInferenceEndpoint } from "./HfInference";
export { InferenceOutputError } from "./lib/InferenceOutputError";
export * from "./types";
export * from "./tasks";

import * as snippets from "./snippets/index.js";
export { snippets };
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { HF_HUB_INFERENCE_PROXY_TEMPLATE, type SnippetInferenceProvider } from "../inference-providers.js";
import type { PipelineType } from "../pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
import { getModelInputSnippet } from "./inputs.js";
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
import { HF_HUB_INFERENCE_PROXY_TEMPLATE, type SnippetInferenceProvider } from "@huggingface/tasks";
import type { PipelineType } from "@huggingface/tasks/src/pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js";
import {
type InferenceSnippet,
type ModelDataMinimal,
getModelInputSnippet,
stringifyGenerationConfig,
stringifyMessages,
} from "@huggingface/tasks";

export const snippetBasic = (
model: ModelDataMinimal,
Expand Down
5 changes: 5 additions & 0 deletions packages/inference/src/snippets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as curl from "./curl.js";
import * as python from "./python.js";
import * as js from "./js.js";

export { curl, python, js };
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { openAIbaseUrl, type SnippetInferenceProvider } from "../inference-providers.js";
import type { PipelineType, WidgetType } from "../pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
import { getModelInputSnippet } from "./inputs.js";
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
import { openAIbaseUrl, type SnippetInferenceProvider } from "@huggingface/tasks";
import type { PipelineType, WidgetType } from "@huggingface/tasks/src/pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js";
import {
type InferenceSnippet,
type ModelDataMinimal,
getModelInputSnippet,
stringifyGenerationConfig,
stringifyMessages,
} from "@huggingface/tasks";

const HFJS_METHODS: Partial<Record<WidgetType, string>> = {
"text-classification": "textClassification",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { openAIbaseUrl, type SnippetInferenceProvider } from "../inference-providers.js";
import type { PipelineType, WidgetType } from "../pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "../tasks/index.js";
import { stringifyGenerationConfig, stringifyMessages } from "./common.js";
import { getModelInputSnippet } from "./inputs.js";
import type { InferenceSnippet, ModelDataMinimal } from "./types.js";
import { openAIbaseUrl, type SnippetInferenceProvider } from "@huggingface/tasks";
import type { PipelineType, WidgetType } from "@huggingface/tasks/src/pipelines.js";
import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingface/tasks/src/tasks/index.js";
import {
type InferenceSnippet,
type ModelDataMinimal,
getModelInputSnippet,
stringifyGenerationConfig,
stringifyMessages,
} from "@huggingface/tasks";

const HFH_INFERENCE_CLIENT_METHODS: Partial<Record<WidgetType, string>> = {
"audio-classification": "audio_classification",
Expand Down
12 changes: 9 additions & 3 deletions packages/inference/test/vcr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,15 @@ async function vcr(
statusText: response.statusText,
headers: Object.fromEntries(
// Remove varying headers as much as possible
[...response.headers.entries()].filter(
([key]) => key !== "date" && key !== "content-length" && !key.startsWith("x-") && key !== "via"
)
(() => {
const entries: [string, string][] = [];
response.headers.forEach((value, key) => {
if (key !== "date" && key !== "content-length" && !key.startsWith("x-") && key !== "via") {
entries.push([key, value]);
}
});
return entries;
})()
),
},
};
Expand Down
6 changes: 3 additions & 3 deletions packages/tasks-gen/scripts/generate-snippets-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { existsSync as pathExists } from "node:fs";
import * as fs from "node:fs/promises";
import * as path from "node:path/posix";

import type { SnippetInferenceProvider, InferenceSnippet } from "@huggingface/tasks";
import { snippets } from "@huggingface/tasks";
import { snippets } from "@huggingface/inference";
import type { SnippetInferenceProvider, InferenceSnippet, ModelDataMinimal } from "@huggingface/tasks";

type LANGUAGE = "sh" | "js" | "py";

const TEST_CASES: {
testName: string;
model: snippets.ModelDataMinimal;
model: ModelDataMinimal;
languages: LANGUAGE[];
providers: SnippetInferenceProvider[];
opts?: Record<string, unknown>;
Expand Down
10 changes: 7 additions & 3 deletions packages/tasks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ export type {
} from "./widget-example.js";
export { SPECIAL_TOKENS_ATTRIBUTES } from "./tokenizer-data.js";

import * as snippets from "./snippets/index.js";
export * from "./gguf.js";

export { snippets };
export type { InferenceSnippet } from "./snippets/index.js";
export {
type InferenceSnippet,
type ModelDataMinimal,
stringifyGenerationConfig,
stringifyMessages,
getModelInputSnippet,
} from "./snippets/index.js";

export { SKUS, DEFAULT_MEMORY_OPTIONS } from "./hardware.js";
export type { HardwareSpec, SkuType } from "./hardware.js";
Expand Down
8 changes: 2 additions & 6 deletions packages/tasks/src/snippets/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import * as inputs from "./inputs.js";
import * as curl from "./curl.js";
import * as python from "./python.js";
import * as js from "./js.js";
export * from "./common.js";
export * from "./inputs.js";
export * from "./types.js";

export { inputs, curl, python, js };

0 comments on commit d05b840

Please sign in to comment.