Skip to content

Commit 2a5443f

Browse files
WauplinSBrandeiscoyotte508
authored
[Inference snippets] Templated snippets for inference snippet generation (#1255)
### What does this PR do? - add `@huggingface/jinja` as a dependency for `@huggingface/inference` - heavily refactor [`./packages/inference/src/snippets/python.ts`](https://github.com/huggingface/huggingface.js/blob/ddd62b60d0a858dc808b6dd7b3589977681f5a8b/packages/inference/src/snippets/python.ts) to use templates instead of inline strings - add some snippet templates under `packages/inference/src/snippets/templates/{language}/{client}/{templateName}.jinja` - `language` is Python - `client` is e.g. huggingface_hub, requests, etc. - `templateName` is related to task name e.g. textToImage, conversationalStream, etc. => overall goal is to make it rather easy to update the snippets for a given task + client without entering the JS code => if conclusive, we can extend to cURL / JS snippets in a follow-up PR This PR also includes a lot of small fixes (usually whitespace<>tabs inconsistencies) + added some tests. I did not change anything major in the generated snippets. ### What to review? IMO, no need to review all templates. Better to check the generated snippets + the main JS file directly (https://github.com/huggingface/huggingface.js/blob/ddd62b60d0a858dc808b6dd7b3589977681f5a8b/packages/inference/src/snippets/python.ts). Sorry for the huge PR but I do believe it is a necessary move moving forward 🤗 --------- Co-authored-by: Simon Brandeis <[email protected]> Co-authored-by: coyotte508 <[email protected]>
1 parent 5cab02a commit 2a5443f

File tree

69 files changed

+731
-621
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+731
-621
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
run: |
106106
sleep 3
107107
pnpm i --filter root --filter inference... --filter hub... --filter tasks-gen --frozen-lockfile
108-
pnpm --filter inference --filter hub --filter tasks publish --force --no-git-checks --registry http://localhost:4874/
108+
pnpm --filter inference --filter hub --filter tasks --filter jinja publish --force --no-git-checks --registry http://localhost:4874/
109109
110110
- name: E2E test - test yarn install
111111
working-directory: e2e/ts
@@ -136,7 +136,7 @@ jobs:
136136
deno-version: vx.x.x
137137
- name: E2E test - deno import from npm
138138
working-directory: e2e/deno
139-
run: deno run --allow-net --allow-env=HF_TOKEN index.ts
139+
run: deno run --allow-read --allow-net --allow-env=HF_TOKEN index.ts
140140
env:
141141
NPM_CONFIG_REGISTRY: http://localhost:4874/
142142
HF_TOKEN: ${{ secrets.HF_TOKEN }}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
"search.exclude": {
1818
"**/dist": true
1919
},
20-
"typescript.tsdk": "node_modules/typescript/lib"
20+
"typescript.tsdk": "node_modules/typescript/lib",
2121
}

packages/inference/package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@
3333
"main": "./dist/index.cjs",
3434
"module": "./dist/index.js",
3535
"exports": {
36-
"types": "./dist/src/index.d.ts",
37-
"require": "./dist/index.cjs",
38-
"import": "./dist/index.js"
36+
".": {
37+
"types": "./dist/src/index.d.ts",
38+
"require": "./dist/index.cjs",
39+
"import": "./dist/index.js"
40+
}
41+
},
42+
"browser": {
43+
"./src/snippets/index.js": false,
44+
"./dist/index.js": "./dist/browser/index.js",
45+
"./dist/index.mjs": "./dist/browser/index.mjs"
3946
},
4047
"type": "module",
4148
"scripts": {
@@ -52,7 +59,8 @@
5259
"check": "tsc"
5360
},
5461
"dependencies": {
55-
"@huggingface/tasks": "workspace:^"
62+
"@huggingface/tasks": "workspace:^",
63+
"@huggingface/jinja": "workspace:^"
5664
},
5765
"devDependencies": {
5866
"@types/node": "18.13.0"

packages/inference/pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/inference/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export { InferenceClient, InferenceClientEndpoint, HfInference } from "./Inferen
22
export { InferenceOutputError } from "./lib/InferenceOutputError";
33
export * from "./types";
44
export * from "./tasks";
5-
65
import * as snippets from "./snippets/index.js";
6+
77
export { snippets };

packages/inference/src/lib/makeRequestOptions.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export async function makeRequestOptions(
5656
/** In most cases (unless we pass a endpointUrl) we know the task */
5757
task?: InferenceTask;
5858
chatCompletion?: boolean;
59+
/* Used internally to generate inference snippets (in which case model mapping is done separately) */
60+
skipModelIdResolution?: boolean;
5961
}
6062
): Promise<{ url: string; info: RequestInit }> {
6163
const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args;
6264
const provider = maybeProvider ?? "hf-inference";
6365
const providerConfig = providerConfigs[provider];
6466

65-
const { includeCredentials, task, chatCompletion, signal } = options ?? {};
67+
const { includeCredentials, task, chatCompletion, signal, skipModelIdResolution } = options ?? {};
6668

6769
if (endpointUrl && provider !== "hf-inference") {
6870
throw new Error(`Cannot use endpointUrl with a third-party provider.`);
@@ -81,15 +83,17 @@ export async function makeRequestOptions(
8183
}
8284
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8385
const hfModel = maybeModel ?? (await loadDefaultModel(task!));
84-
const model = providerConfig.clientSideRoutingOnly
85-
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86-
removeProviderPrefix(maybeModel!, provider)
87-
: // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
88-
await getProviderModelId({ model: hfModel, provider }, args, {
89-
task,
90-
chatCompletion,
91-
fetch: options?.fetch,
92-
});
86+
const model = skipModelIdResolution
87+
? hfModel
88+
: providerConfig.clientSideRoutingOnly
89+
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
90+
removeProviderPrefix(maybeModel!, provider)
91+
: // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
92+
await getProviderModelId({ model: hfModel, provider }, args, {
93+
task,
94+
chatCompletion,
95+
fetch: options?.fetch,
96+
});
9397

9498
const authMethod = (() => {
9599
if (providerConfig.clientSideRoutingOnly) {

0 commit comments

Comments
 (0)