Skip to content

Commit 3ba8dc7

Browse files
authored
[Inference] openai client snippet for "auto" provider (#1630)
<img width="919" height="713" alt="image" src="https://github.com/user-attachments/assets/14b093a9-e246-4ea3-84cb-c25d016c3700" /> (also needed for internal huggingface-internal/moon-landing#14421)
1 parent 9f7aa5a commit 3ba8dc7

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

packages/inference/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const HF_HUB_URL = "https://huggingface.co";
22
export const HF_ROUTER_URL = "https://router.huggingface.co";
3+
export const HF_ROUTER_AUTO_ENDPOINT = `${HF_ROUTER_URL}/v1`;
34
export const HF_HEADER_X_BILL_TO = "X-HF-Bill-To";

packages/inference/src/snippets/getInferenceSnippets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions.j
1313
import type { InferenceProviderMappingEntry, InferenceProviderOrPolicy, InferenceTask, RequestArgs } from "../types.js";
1414
import { templates } from "./templates.exported.js";
1515
import { getLogger } from "../lib/logger.js";
16+
import { HF_ROUTER_AUTO_ENDPOINT } from "../config.js";
1617

1718
export type InferenceSnippetOptions = {
1819
streaming?: boolean;
@@ -37,7 +38,7 @@ const CLIENTS: Record<InferenceSnippetLanguage, Client[]> = {
3738

3839
const CLIENTS_AUTO_POLICY: Partial<Record<InferenceSnippetLanguage, Client[]>> = {
3940
js: ["huggingface.js"],
40-
python: ["huggingface_hub"],
41+
python: ["huggingface_hub", "openai"],
4142
};
4243

4344
type InputPreparationFn = (model: ModelDataMinimal, opts?: Record<string, unknown>) => object;
@@ -179,7 +180,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
179180
{
180181
accessToken: accessTokenOrPlaceholder,
181182
provider,
182-
endpointUrl: opts?.endpointUrl,
183+
endpointUrl: opts?.endpointUrl ?? (provider === "auto" ? HF_ROUTER_AUTO_ENDPOINT : undefined),
183184
...inputs,
184185
} as RequestArgs,
185186
inferenceProviderMapping,

0 commit comments

Comments
 (0)