Skip to content

Commit

Permalink
HF Inference: go through hf.co proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
SBrandeis committed Jan 28, 2025
1 parent cf160c7 commit b753acb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/inference/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const HF_HUB_URL = "https://huggingface.co";
export const HF_INFERENCE_API_URL = "https://api-inference.huggingface.co";
31 changes: 16 additions & 15 deletions packages/inference/src/lib/makeRequestOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WidgetType } from "@huggingface/tasks";
import { HF_HUB_URL, HF_INFERENCE_API_URL } from "../config";
import { HF_HUB_URL } from "../config";
import { FAL_AI_API_BASE_URL, FAL_AI_SUPPORTED_MODEL_IDS } from "../providers/fal-ai";
import { REPLICATE_API_BASE_URL, REPLICATE_SUPPORTED_MODEL_IDS } from "../providers/replicate";
import { SAMBANOVA_API_BASE_URL, SAMBANOVA_SUPPORTED_MODEL_IDS } from "../providers/sambanova";
Expand Down Expand Up @@ -67,21 +67,21 @@ export async function makeRequestOptions(
? "hf-token"
: "provider-key"
: includeCredentials === "include"
? "credentials-include"
: "none";
? "credentials-include"
: "none";

const url = endpointUrl
? chatCompletion
? endpointUrl + `/v1/chat/completions`
: endpointUrl
: makeUrl({
authMethod,
chatCompletion: chatCompletion ?? false,
forceTask,
model,
provider: provider ?? "hf-inference",
taskHint,
});
authMethod,
chatCompletion: chatCompletion ?? false,
forceTask,
model,
provider: provider ?? "hf-inference",
taskHint,
});

const headers: Record<string, string> = {};
if (accessToken) {
Expand Down Expand Up @@ -136,9 +136,9 @@ export async function makeRequestOptions(
body: binary
? args.data
: JSON.stringify({
...otherArgs,
...(chatCompletion || provider === "together" ? { model } : undefined),
}),
...otherArgs,
...(chatCompletion || provider === "together" ? { model } : undefined),
}),
...(credentials ? { credentials } : undefined),
signal: options?.signal,
};
Expand Down Expand Up @@ -237,9 +237,10 @@ function makeUrl(params: {
return baseUrl;
}
default: {
const baseUrl = HF_HUB_INFERENCE_PROXY_TEMPLATE.replaceAll("{{PROVIDER}}", "hf-inference")
const url = params.forceTask
? `${HF_INFERENCE_API_URL}/pipeline/${params.forceTask}/${params.model}`
: `${HF_INFERENCE_API_URL}/models/${params.model}`;
? `${baseUrl}/pipeline/${params.forceTask}/${params.model}`
: `${baseUrl}/models/${params.model}`;
if (params.taskHint === "text-generation" && params.chatCompletion) {
return url + `/v1/chat/completions`;
}
Expand Down

0 comments on commit b753acb

Please sign in to comment.