Skip to content

Commit a2ad61e

Browse files
sinediedhntrl
andauthored
feat(@langchain/openai): support callable function for apiKey (#9301)
Co-authored-by: Hunter Lovell <[email protected]>
1 parent d78c75e commit a2ad61e

File tree

8 files changed

+41
-22
lines changed

8 files changed

+41
-22
lines changed

.changeset/silly-buses-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/openai": patch
3+
---
4+
5+
support callable function for apiKey

libs/providers/langchain-openai/src/azure/chat_models/common.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AzureOpenAI as AzureOpenAIClient } from "openai";
1+
import { AzureOpenAI as AzureOpenAIClient, type ClientOptions } from "openai";
22
import { getEnv, getEnvironmentVariable } from "@langchain/core/utils/env";
33
import type { Serialized } from "@langchain/core/load/serializable";
44
import { ChatOpenAICallOptions } from "../../chat_models/index.js";
@@ -56,8 +56,10 @@ export function _constructAzureFields(
5656
) {
5757
this.azureOpenAIApiKey =
5858
fields?.azureOpenAIApiKey ??
59-
fields?.openAIApiKey ??
60-
fields?.apiKey ??
59+
(typeof fields?.openAIApiKey === "string"
60+
? fields?.openAIApiKey
61+
: undefined) ??
62+
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
6163
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
6264

6365
this.azureOpenAIApiInstanceName =
@@ -106,8 +108,9 @@ export function _getAzureClientOptions(
106108

107109
const endpoint = getEndpoint(openAIEndpointConfig);
108110

109-
const params = {
110-
...this.clientConfig,
111+
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
112+
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
113+
...clientConfigRest,
111114
baseURL: endpoint,
112115
timeout: this.timeout,
113116
maxRetries: 0,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
4141
this.batchSize = fields?.batchSize ?? 1;
4242
this.azureOpenAIApiKey =
4343
fields?.azureOpenAIApiKey ??
44-
fields?.apiKey ??
44+
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
4545
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
4646

4747
this.azureOpenAIApiVersion =
@@ -81,8 +81,9 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
8181

8282
const endpoint = getEndpoint(openAIEndpointConfig);
8383

84-
const params = {
85-
...this.clientConfig,
84+
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
85+
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
86+
...clientConfigRest,
8687
baseURL: endpoint,
8788
timeout: this.timeout,
8889
maxRetries: 0,

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ export class AzureOpenAI extends OpenAI {
7070

7171
this.azureOpenAIApiKey =
7272
fields?.azureOpenAIApiKey ??
73-
fields?.openAIApiKey ??
74-
fields?.apiKey ??
73+
(typeof fields?.openAIApiKey === "string"
74+
? fields?.openAIApiKey
75+
: undefined) ??
76+
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
7577
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
7678

7779
this.azureOpenAIApiInstanceName =
@@ -113,8 +115,9 @@ export class AzureOpenAI extends OpenAI {
113115

114116
const endpoint = getEndpoint(openAIEndpointConfig);
115117

116-
const params = {
117-
...this.clientConfig,
118+
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
119+
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
120+
...clientConfigRest,
118121
baseURL: endpoint,
119122
timeout: this.timeout,
120123
maxRetries: 0,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
type ChatOpenAIResponseFormat,
4141
ResponseFormatConfiguration,
4242
OpenAIVerbosityParam,
43+
type OpenAIApiKey,
4344
} from "../types.js";
4445
import { type OpenAIEndpointConfig, getEndpoint } from "../utils/azure.js";
4546
import {
@@ -245,7 +246,7 @@ export abstract class BaseChatOpenAI<
245246

246247
topLogprobs?: number;
247248

248-
apiKey?: string;
249+
apiKey?: OpenAIApiKey;
249250

250251
organization?: string;
251252

@@ -420,7 +421,8 @@ export abstract class BaseChatOpenAI<
420421
super(fields ?? {});
421422

422423
const configApiKey =
423-
typeof fields?.configuration?.apiKey === "string"
424+
typeof fields?.configuration?.apiKey === "string" ||
425+
typeof fields?.configuration?.apiKey === "function"
424426
? fields?.configuration?.apiKey
425427
: undefined;
426428
this.apiKey =

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
22
import { getEnvironmentVariable } from "@langchain/core/utils/env";
33
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
44
import { chunkArray } from "@langchain/core/utils/chunk_array";
5+
import type { OpenAIApiKey } from "./types.js";
56
import { getEndpoint, OpenAIEndpointConfig } from "./utils/azure.js";
67
import { wrapOpenAIClientError } from "./utils/client.js";
78

@@ -102,16 +103,18 @@ export class OpenAIEmbeddings<TOutput = number[]>
102103

103104
protected clientConfig: ClientOptions;
104105

106+
protected apiKey?: OpenAIApiKey;
107+
105108
constructor(
106109
fields?: Partial<OpenAIEmbeddingsParams> & {
107110
verbose?: boolean;
108111
/**
109112
* The OpenAI API key to use.
110113
* Alias for `apiKey`.
111114
*/
112-
openAIApiKey?: string;
115+
openAIApiKey?: OpenAIApiKey;
113116
/** The OpenAI API key to use. */
114-
apiKey?: string;
117+
apiKey?: OpenAIApiKey;
115118
configuration?: ClientOptions;
116119
}
117120
) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@langchain/core/language_models/llms";
1111
import { chunkArray } from "@langchain/core/utils/chunk_array";
1212
import type {
13+
OpenAIApiKey,
1314
OpenAICallOptions,
1415
OpenAICoreRequestOptions,
1516
OpenAIInput,
@@ -121,9 +122,9 @@ export class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>
121122

122123
streaming = false;
123124

124-
openAIApiKey?: string;
125+
openAIApiKey?: OpenAIApiKey;
125126

126-
apiKey?: string;
127+
apiKey?: OpenAIApiKey;
127128

128129
organization?: string;
129130

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

Lines changed: 5 additions & 4 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,6 +24,8 @@ export type OpenAIChatModelId =
2424

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

27+
export type OpenAIApiKey = ClientOptions["apiKey"];
28+
2729
export declare interface OpenAIBaseInput {
2830
/** Sampling temperature to use */
2931
temperature: number;
@@ -103,12 +105,12 @@ export declare interface OpenAIBaseInput {
103105
* `OPENAI_API_KEY` environment variable.
104106
* Alias for `apiKey`
105107
*/
106-
openAIApiKey?: string;
108+
openAIApiKey?: OpenAIApiKey;
107109
/**
108110
* API key to use when making requests to OpenAI. Defaults to the value of
109111
* `OPENAI_API_KEY` environment variable.
110112
*/
111-
apiKey?: string;
113+
apiKey?: OpenAIApiKey;
112114

113115
/**
114116
* The verbosity of the model's response.
@@ -285,7 +287,6 @@ export interface AzureOpenAIInput {
285287
export interface AzureOpenAIChatInput
286288
extends OpenAIChatInput,
287289
AzureOpenAIInput {
288-
openAIApiKey?: string;
289290
openAIApiVersion?: string;
290291
openAIBasePath?: string;
291292
deploymentName?: string;

0 commit comments

Comments
 (0)