Skip to content

Commit d69530f

Browse files
committed
refactor: use openai sdk apikey function handling
1 parent c1f21b9 commit d69530f

File tree

5 files changed

+30
-55
lines changed

5 files changed

+30
-55
lines changed

libs/providers/langchain-openai/src/chat_models/base.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
type OpenAIApiKey,
4343
} from "../types.js";
4444
import { type OpenAIEndpointConfig, getEndpoint } from "../utils/azure.js";
45-
import { resolveOpenAIApiKey } from "../utils/client.js";
4645
import {
4746
type FunctionDef,
4847
formatFunctionDefinitions,
@@ -463,17 +462,12 @@ export abstract class BaseChatOpenAI<
463462
if (this.disableStreaming) this.streamUsage = false;
464463

465464
const clientConfig: ClientOptions = {
465+
apiKey: this.apiKey,
466466
dangerouslyAllowBrowser: true,
467467
...fields?.configuration,
468468
organization: this.organization,
469469
};
470470

471-
if (typeof this.apiKey === "string") {
472-
clientConfig.apiKey = this.apiKey;
473-
} else {
474-
clientConfig.apiKey = undefined;
475-
}
476-
477471
this.clientConfig = clientConfig;
478472

479473
// If `supportsStrictToolCalling` is explicitly set, use that value.
@@ -555,7 +549,7 @@ export abstract class BaseChatOpenAI<
555549
protected async _getClientOptions(
556550
options: OpenAICoreRequestOptions | undefined
557551
): Promise<OpenAICoreRequestOptions> {
558-
const resolvedApiKey = await resolveOpenAIApiKey(this.apiKey);
552+
const currentApiKey = this.apiKey ?? this.clientConfig.apiKey;
559553

560554
if (!this.client) {
561555
const openAIEndpointConfig: OpenAIEndpointConfig = {
@@ -571,19 +565,21 @@ export abstract class BaseChatOpenAI<
571565
maxRetries: 0,
572566
};
573567

574-
if (resolvedApiKey !== undefined) {
575-
params.apiKey = resolvedApiKey;
576-
} else if (typeof this.clientConfig.apiKey === "string") {
577-
params.apiKey = this.clientConfig.apiKey;
568+
if (currentApiKey !== undefined) {
569+
params.apiKey = currentApiKey;
578570
}
579571

580572
if (!params.baseURL) {
581573
delete params.baseURL;
582574
}
583575

584576
this.client = new OpenAIClient(params);
585-
} else if (resolvedApiKey !== undefined) {
586-
this.client.apiKey = resolvedApiKey;
577+
} else if (currentApiKey !== undefined) {
578+
if (typeof currentApiKey === "string") {
579+
this.client.apiKey = currentApiKey;
580+
} else {
581+
this.client = this.client.withOptions({ apiKey: currentApiKey });
582+
}
587583
}
588584

589585
const requestOptions = {

libs/providers/langchain-openai/src/embeddings.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
44
import { chunkArray } from "@langchain/core/utils/chunk_array";
55
import type { OpenAIApiKey } from "./types.js";
66
import { getEndpoint, OpenAIEndpointConfig } from "./utils/azure.js";
7-
import { resolveOpenAIApiKey, wrapOpenAIClientError } from "./utils/client.js";
7+
import { wrapOpenAIClientError } from "./utils/client.js";
88

99
/**
1010
* @see https://platform.openai.com/docs/guides/embeddings#embedding-models
@@ -149,10 +149,8 @@ export class OpenAIEmbeddings<TOutput = number[]>
149149
organization: this.organization,
150150
};
151151

152-
if (typeof apiKey === "string") {
152+
if (apiKey !== undefined) {
153153
clientConfig.apiKey = apiKey;
154-
} else {
155-
clientConfig.apiKey = undefined;
156154
}
157155

158156
this.clientConfig = clientConfig;
@@ -228,7 +226,7 @@ export class OpenAIEmbeddings<TOutput = number[]>
228226
protected async embeddingWithRetry(
229227
request: OpenAIClient.EmbeddingCreateParams
230228
) {
231-
const resolvedApiKey = await resolveOpenAIApiKey(this.apiKey);
229+
const currentApiKey = this.apiKey ?? this.clientConfig.apiKey;
232230

233231
if (!this.client) {
234232
const openAIEndpointConfig: OpenAIEndpointConfig = {
@@ -244,19 +242,21 @@ export class OpenAIEmbeddings<TOutput = number[]>
244242
maxRetries: 0,
245243
};
246244

247-
if (resolvedApiKey !== undefined) {
248-
params.apiKey = resolvedApiKey;
249-
} else if (typeof this.clientConfig.apiKey === "string") {
250-
params.apiKey = this.clientConfig.apiKey;
245+
if (currentApiKey !== undefined) {
246+
params.apiKey = currentApiKey;
251247
}
252248

253249
if (!params.baseURL) {
254250
delete params.baseURL;
255251
}
256252

257253
this.client = new OpenAIClient(params);
258-
} else if (resolvedApiKey !== undefined) {
259-
this.client.apiKey = resolvedApiKey;
254+
} else if (currentApiKey !== undefined) {
255+
if (typeof currentApiKey === "string") {
256+
this.client.apiKey = currentApiKey;
257+
} else {
258+
this.client = this.client.withOptions({ apiKey: currentApiKey });
259+
}
260260
}
261261
const requestOptions = {};
262262

libs/providers/langchain-openai/src/llms.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
OpenAIInput,
1717
} from "./types.js";
1818
import { OpenAIEndpointConfig, getEndpoint } from "./utils/azure.js";
19-
import { resolveOpenAIApiKey, wrapOpenAIClientError } from "./utils/client.js";
19+
import { wrapOpenAIClientError } from "./utils/client.js";
2020

2121
export type { OpenAICallOptions, OpenAIInput };
2222

@@ -198,10 +198,8 @@ export class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>
198198
organization: this.organization,
199199
};
200200

201-
if (typeof this.apiKey === "string") {
201+
if (this.apiKey !== undefined) {
202202
clientConfig.apiKey = this.apiKey;
203-
} else {
204-
clientConfig.apiKey = undefined;
205203
}
206204

207205
this.clientConfig = clientConfig;
@@ -470,7 +468,7 @@ export class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>
470468
protected async _getClientOptions(
471469
options: OpenAICoreRequestOptions | undefined
472470
): Promise<OpenAICoreRequestOptions> {
473-
const resolvedApiKey = await resolveOpenAIApiKey(this.apiKey);
471+
const currentApiKey = this.apiKey ?? this.clientConfig.apiKey;
474472

475473
if (!this.client) {
476474
const openAIEndpointConfig: OpenAIEndpointConfig = {
@@ -486,19 +484,17 @@ export class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>
486484
maxRetries: 0,
487485
};
488486

489-
if (resolvedApiKey !== undefined) {
490-
params.apiKey = resolvedApiKey;
491-
} else if (typeof this.clientConfig.apiKey === "string") {
492-
params.apiKey = this.clientConfig.apiKey;
487+
if (currentApiKey !== undefined) {
488+
params.apiKey = currentApiKey;
493489
}
494490

495491
if (!params.baseURL) {
496492
delete params.baseURL;
497493
}
498494

499495
this.client = new OpenAIClient(params);
500-
} else if (resolvedApiKey !== undefined) {
501-
this.client.apiKey = resolvedApiKey;
496+
} else if (currentApiKey !== undefined) {
497+
this.client = this.client.withOptions({ apiKey: currentApiKey });
502498
}
503499

504500
const requestOptions = {

libs/providers/langchain-openai/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { OpenAI as OpenAIClient } from "openai";
1+
import type { OpenAI as OpenAIClient, ClientOptions } from "openai";
22
import type {
33
ResponseFormatText,
44
ResponseFormatJSONObject,
@@ -24,7 +24,7 @@ export type OpenAIChatModelId =
2424

2525
export type OpenAIVerbosityParam = "low" | "medium" | "high" | null;
2626

27-
export type OpenAIApiKey = string | (() => string | Promise<string>);
27+
export type OpenAIApiKey = ClientOptions["apiKey"];
2828

2929
export declare interface OpenAIBaseInput {
3030
/** Sampling temperature to use */

libs/providers/langchain-openai/src/utils/client.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import { APIConnectionTimeoutError, APIUserAbortError } from "openai";
2-
import type { OpenAIApiKey } from "../types.js";
32
import { addLangChainErrorFields } from "./errors.js";
43

5-
export async function resolveOpenAIApiKey(
6-
apiKey: OpenAIApiKey | undefined
7-
): Promise<string | undefined> {
8-
if (typeof apiKey === "function") {
9-
const value = await apiKey();
10-
if (value == null || typeof value !== "string" || value.length === 0) {
11-
throw new Error(
12-
"OpenAI apiKey callback must resolve to a non-empty string value."
13-
);
14-
}
15-
return value;
16-
}
17-
18-
return apiKey;
19-
}
20-
214
export function wrapOpenAIClientError(e: unknown) {
225
if (!e || typeof e !== "object") {
236
return e;

0 commit comments

Comments
 (0)