Skip to content

Commit c3d664d

Browse files
committed
remove accessToken entirely
1 parent fd20ab4 commit c3d664d

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

packages/inference/src/snippets/getInferenceSnippets.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ const ACCESS_TOKEN_PLACEHOLDER = "<ACCESS_TOKEN>"; // Placeholder to replace wit
127127
const snippetGenerator = (templateName: string, inputPreparationFn?: InputPreparationFn) => {
128128
return (
129129
model: ModelDataMinimal,
130-
accessToken: string,
131130
provider: InferenceProviderOrPolicy,
132131
inferenceProviderMapping?: InferenceProviderModelMapping,
133132
opts?: InferenceSnippetOptions
@@ -151,15 +150,14 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
151150
console.error(`Failed to get provider helper for ${provider} (${task})`, e);
152151
return [];
153152
}
154-
const accessTokenOrPlaceholder = accessToken == "" ? ACCESS_TOKEN_PLACEHOLDER : accessToken;
155153

156154
/// Prepare inputs + make request
157155
const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
158156
const request = makeRequestOptionsFromResolvedModel(
159157
providerModelId,
160158
providerHelper,
161159
{
162-
accessToken: accessTokenOrPlaceholder,
160+
accessToken: ACCESS_TOKEN_PLACEHOLDER,
163161
provider,
164162
...inputs,
165163
} as RequestArgs,
@@ -184,7 +182,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
184182

185183
/// Prepare template injection data
186184
const params: TemplateParams = {
187-
accessToken: accessTokenOrPlaceholder,
185+
accessToken: ACCESS_TOKEN_PLACEHOLDER,
188186
authorizationHeader: (request.info.headers as Record<string, string>)?.Authorization,
189187
baseUrl: removeSuffix(request.url, "/chat/completions"),
190188
fullUrl: request.url,
@@ -253,9 +251,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
253251
}
254252

255253
/// Replace access token placeholder
256-
if (snippet.includes(ACCESS_TOKEN_PLACEHOLDER)) {
257-
snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
258-
}
254+
snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
259255

260256
/// Snippet is ready!
261257
return { language, client: client as string, content: snippet };
@@ -308,7 +304,6 @@ const snippets: Partial<
308304
PipelineType,
309305
(
310306
model: ModelDataMinimal,
311-
accessToken: string,
312307
provider: InferenceProviderOrPolicy,
313308
inferenceProviderMapping?: InferenceProviderModelMapping,
314309
opts?: InferenceSnippetOptions
@@ -348,13 +343,12 @@ const snippets: Partial<
348343

349344
export function getInferenceSnippets(
350345
model: ModelDataMinimal,
351-
accessToken: string,
352346
provider: InferenceProviderOrPolicy,
353347
inferenceProviderMapping?: InferenceProviderModelMapping,
354348
opts?: Record<string, unknown>
355349
): InferenceSnippet[] {
356350
return model.pipeline_tag && model.pipeline_tag in snippets
357-
? snippets[model.pipeline_tag]?.(model, accessToken, provider, inferenceProviderMapping, opts) ?? []
351+
? snippets[model.pipeline_tag]?.(model, provider, inferenceProviderMapping, opts) ?? []
358352
: [];
359353
}
360354

@@ -435,8 +429,8 @@ function replaceAccessTokenPlaceholder(
435429
language: InferenceSnippetLanguage,
436430
provider: InferenceProviderOrPolicy
437431
): string {
438-
// If "accessToken" is empty, the snippets are generated with a placeholder.
439-
// Once snippets are rendered, we replace the placeholder with code to fetch the access token from an environment variable.
432+
// The snippets are generated with a placeholder in place of the access token.
433+
// Once snippets are rendered, we replace the placeholder with correct code to fetch the access token from an environment variable.
440434

441435
// Determine if HF_TOKEN or specific provider token should be used
442436
const accessTokenEnvVar =
@@ -453,7 +447,9 @@ function replaceAccessTokenPlaceholder(
453447
`"Authorization: Bearer $${accessTokenEnvVar}"` // e.g. "Authorization: Bearer $HF_TOKEN"
454448
);
455449
} else if (language === "python") {
456-
snippet = "import os\n" + snippet;
450+
if (snippet.includes(ACCESS_TOKEN_PLACEHOLDER)) {
451+
snippet = "import os\n" + snippet;
452+
}
457453
snippet = snippet.replace(
458454
`"${ACCESS_TOKEN_PLACEHOLDER}"`,
459455
`os.environ["${accessTokenEnvVar}"]` // e.g. os.environ["HF_TOKEN")

packages/tasks-gen/scripts/generate-snippets-fixtures.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ function generateInferenceSnippet(
314314
): InferenceSnippet[] {
315315
const allSnippets = snippets.getInferenceSnippets(
316316
model,
317-
"",
318317
provider,
319318
{
320319
hfModelId: model.id,

0 commit comments

Comments
 (0)