Skip to content

Commit a5513dc

Browse files
zekecoyotte508SBrandeis
authored
add user-agent header to inference requests (#1150)
This PR updates the inference client to include a user-agent header in outgoing API requests. The value is derived from the `name` and `version` of the inference package, e.g. `@huggingface/[email protected]` This will allow third-party inference providers to identify API requests that originate from HF's new inference integration. See https://huggingface.co/blog/inference-providers cc @julien-c @SBrandeis --------- Co-authored-by: coyotte508 <[email protected]> Co-authored-by: Simon Brandeis <[email protected]>
1 parent 7508458 commit a5513dc

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

packages/inference/src/lib/makeRequestOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { TOGETHER_API_BASE_URL, TOGETHER_SUPPORTED_MODEL_IDS } from "../provider
77
import type { InferenceProvider } from "../types";
88
import type { InferenceTask, Options, RequestArgs } from "../types";
99
import { isUrl } from "./isUrl";
10+
import { version as packageVersion, name as packageName } from "../../package.json";
1011

1112
const HF_HUB_INFERENCE_PROXY_TEMPLATE = `${HF_HUB_URL}/api/inference-proxy/{{PROVIDER}}`;
1213

@@ -89,6 +90,12 @@ export async function makeRequestOptions(
8990
provider === "fal-ai" && authMethod === "provider-key" ? `Key ${accessToken}` : `Bearer ${accessToken}`;
9091
}
9192

93+
// e.g. @huggingface/inference/3.1.3
94+
const ownUserAgent = `${packageName}/${packageVersion}`;
95+
headers["User-Agent"] = [ownUserAgent, typeof navigator !== "undefined" ? navigator.userAgent : undefined]
96+
.filter((x) => x !== undefined)
97+
.join(" ");
98+
9299
const binary = "data" in args && !!args.data;
93100

94101
if (!binary) {

packages/inference/test/vcr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ async function vcr(
179179
const tape: Tape = {
180180
url,
181181
init: {
182-
headers: init.headers && omit(init.headers as Record<string, string>, "Authorization"),
182+
headers: init.headers && omit(init.headers as Record<string, string>, ["Authorization", "User-Agent"]),
183183
method: init.method,
184184
body: typeof init.body === "string" && init.body.length < 1_000 ? init.body : undefined,
185185
},

packages/inference/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"noImplicitOverride": true,
1212
"declaration": true,
1313
"declarationMap": true,
14-
"outDir": "./dist"
14+
"outDir": "./dist",
15+
"resolveJsonModule": true
1516
},
1617
"include": ["src", "test"],
1718
"exclude": ["dist"]

0 commit comments

Comments
 (0)