Skip to content

[Inference snippets] Templated snippets for inference snippet generation #1255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ea68541
tmp commit
Wauplin Mar 5, 2025
0006042
nope
Wauplin Mar 5, 2025
8ae6d93
draft
Wauplin Mar 5, 2025
b86faf5
fixing some tab -> whitespaces issues
Wauplin Mar 6, 2025
f121009
first move towards templating
Wauplin Mar 6, 2025
c2231ad
always more
Wauplin Mar 6, 2025
7090adc
all python snippets as templates + some examples
Wauplin Mar 6, 2025
134fa2d
base url for openai snippets
Wauplin Mar 6, 2025
d07032e
start refacto
Wauplin Mar 6, 2025
2b95376
closer to a good version
Wauplin Mar 6, 2025
0766e08
minimal code + trim all snippets
Wauplin Mar 6, 2025
29ebae9
some comments
Wauplin Mar 6, 2025
95a2a71
sort
Wauplin Mar 6, 2025
f529ba6
sort
Wauplin Mar 6, 2025
b7b6869
delete before coming back
Wauplin Mar 6, 2025
f8ecf33
moving generated snippets
Wauplin Mar 6, 2025
ddd62b6
remove tmp script
Wauplin Mar 6, 2025
7a498c6
tabs -> whitespaces
Wauplin Mar 6, 2025
e23a2a3
tabs -> whitespaces
Wauplin Mar 6, 2025
e9a2c13
Merge branch 'main' into templated-inference-python-snippets
Wauplin Mar 6, 2025
86e787a
switch from handlebars to jinja
Wauplin Mar 7, 2025
30bbfad
Update .vscode/settings.json
Wauplin Mar 7, 2025
a280d60
not private
Wauplin Mar 7, 2025
274dfe2
Merge branch 'templated-inference-python-snippets' of github.com:hugg…
Wauplin Mar 7, 2025
acb99d1
Merge branch 'main' into templated-inference-python-snippets
Wauplin Mar 7, 2025
99e8c87
Merge branch 'main' into templated-inference-python-snippets
Wauplin Mar 10, 2025
d2579ab
Do not export ./snippets module in browser mode (#1259)
Wauplin Mar 11, 2025
dd96c31
Merge branch 'main' into templated-inference-python-snippets
Wauplin Mar 11, 2025
aeecd11
filter jinja in ci test
Wauplin Mar 11, 2025
f6d81d6
handle both ESM and CJS ?
Wauplin Mar 11, 2025
e4bacdd
add --allow-read flag for deno import
Wauplin Mar 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
run: |
sleep 3
pnpm i --filter root --filter inference... --filter hub... --filter tasks-gen --frozen-lockfile
pnpm --filter inference --filter hub --filter tasks publish --force --no-git-checks --registry http://localhost:4874/
pnpm --filter inference --filter hub --filter tasks --filter jinja publish --force --no-git-checks --registry http://localhost:4874/

- name: E2E test - test yarn install
working-directory: e2e/ts
Expand Down Expand Up @@ -136,7 +136,7 @@ jobs:
deno-version: vx.x.x
- name: E2E test - deno import from npm
working-directory: e2e/deno
run: deno run --allow-net --allow-env=HF_TOKEN index.ts
run: deno run --allow-read --allow-net --allow-env=HF_TOKEN index.ts
env:
NPM_CONFIG_REGISTRY: http://localhost:4874/
HF_TOKEN: ${{ secrets.HF_TOKEN }}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"search.exclude": {
"**/dist": true
},
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
}
16 changes: 12 additions & 4 deletions packages/inference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
"types": "./dist/src/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
".": {
"types": "./dist/src/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"browser": {
"./src/snippets/index.js": false,
"./dist/index.js": "./dist/browser/index.js",
"./dist/index.mjs": "./dist/browser/index.mjs"
},
"type": "module",
"scripts": {
Expand All @@ -52,7 +59,8 @@
"check": "tsc"
},
"dependencies": {
"@huggingface/tasks": "workspace:^"
"@huggingface/tasks": "workspace:^",
"@huggingface/jinja": "workspace:^"
},
"devDependencies": {
"@types/node": "18.13.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/inference/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/inference/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { InferenceClient, InferenceClientEndpoint, HfInference } from "./Inferen
export { InferenceOutputError } from "./lib/InferenceOutputError";
export * from "./types";
export * from "./tasks";

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

export { snippets };
24 changes: 14 additions & 10 deletions packages/inference/src/lib/makeRequestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export async function makeRequestOptions(
/** In most cases (unless we pass a endpointUrl) we know the task */
task?: InferenceTask;
chatCompletion?: boolean;
/* Used internally to generate inference snippets (in which case model mapping is done separately) */
skipModelIdResolution?: boolean;
}
): Promise<{ url: string; info: RequestInit }> {
const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args;
const provider = maybeProvider ?? "hf-inference";
const providerConfig = providerConfigs[provider];

const { includeCredentials, task, chatCompletion, signal } = options ?? {};
const { includeCredentials, task, chatCompletion, signal, skipModelIdResolution } = options ?? {};

if (endpointUrl && provider !== "hf-inference") {
throw new Error(`Cannot use endpointUrl with a third-party provider.`);
Expand All @@ -81,15 +83,17 @@ export async function makeRequestOptions(
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const hfModel = maybeModel ?? (await loadDefaultModel(task!));
const model = providerConfig.clientSideRoutingOnly
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
removeProviderPrefix(maybeModel!, provider)
: // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
await getProviderModelId({ model: hfModel, provider }, args, {
task,
chatCompletion,
fetch: options?.fetch,
});
const model = skipModelIdResolution
? hfModel
: providerConfig.clientSideRoutingOnly
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
removeProviderPrefix(maybeModel!, provider)
: // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
await getProviderModelId({ model: hfModel, provider }, args, {
task,
chatCompletion,
fetch: options?.fetch,
});

const authMethod = (() => {
if (providerConfig.clientSideRoutingOnly) {
Expand Down
Loading