Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions libs/providers/langchain-openai/src/azure/chat_models/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AzureOpenAI as AzureOpenAIClient } from "openai";
import { AzureOpenAI as AzureOpenAIClient, type ClientOptions } from "openai";
import { getEnv, getEnvironmentVariable } from "@langchain/core/utils/env";
import type { Serialized } from "@langchain/core/load/serializable";
import { ChatOpenAICallOptions } from "../../chat_models/index.js";
Expand Down Expand Up @@ -56,8 +56,10 @@ export function _constructAzureFields(
) {
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.openAIApiKey ??
fields?.apiKey ??
(typeof fields?.openAIApiKey === "string"
? fields?.openAIApiKey
: undefined) ??
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");

this.azureOpenAIApiInstanceName =
Expand Down Expand Up @@ -106,8 +108,9 @@ export function _getAzureClientOptions(

const endpoint = getEndpoint(openAIEndpointConfig);

const params = {
...this.clientConfig,
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
...clientConfigRest,
baseURL: endpoint,
timeout: this.timeout,
maxRetries: 0,
Expand Down
7 changes: 4 additions & 3 deletions libs/providers/langchain-openai/src/azure/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
this.batchSize = fields?.batchSize ?? 1;
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.apiKey ??
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");

this.azureOpenAIApiVersion =
Expand Down Expand Up @@ -81,8 +81,9 @@ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {

const endpoint = getEndpoint(openAIEndpointConfig);

const params = {
...this.clientConfig,
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
...clientConfigRest,
baseURL: endpoint,
timeout: this.timeout,
maxRetries: 0,
Expand Down
11 changes: 7 additions & 4 deletions libs/providers/langchain-openai/src/azure/llms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ export class AzureOpenAI extends OpenAI {

this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.openAIApiKey ??
fields?.apiKey ??
(typeof fields?.openAIApiKey === "string"
? fields?.openAIApiKey
: undefined) ??
(typeof fields?.apiKey === "string" ? fields?.apiKey : undefined) ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");

this.azureOpenAIApiInstanceName =
Expand Down Expand Up @@ -113,8 +115,9 @@ export class AzureOpenAI extends OpenAI {

const endpoint = getEndpoint(openAIEndpointConfig);

const params = {
...this.clientConfig,
const { apiKey: existingApiKey, ...clientConfigRest } = this.clientConfig;
const params: Omit<ClientOptions, "apiKey"> & { apiKey?: string } = {
...clientConfigRest,
baseURL: endpoint,
timeout: this.timeout,
maxRetries: 0,
Expand Down
16 changes: 9 additions & 7 deletions libs/providers/langchain-openai/src/chat_models/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
type ChatOpenAIResponseFormat,
ResponseFormatConfiguration,
OpenAIVerbosityParam,
type OpenAIApiKey,
} from "../types.js";
import { type OpenAIEndpointConfig, getEndpoint } from "../utils/azure.js";
import {
Expand Down Expand Up @@ -245,7 +246,7 @@ export abstract class BaseChatOpenAI<

topLogprobs?: number;

apiKey?: string;
apiKey?: OpenAIApiKey;

organization?: string;

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

const configApiKey =
typeof fields?.configuration?.apiKey === "string"
typeof fields?.configuration?.apiKey === "string" ||
typeof fields?.configuration?.apiKey === "function"
? fields?.configuration?.apiKey
: undefined;
this.apiKey =
Expand Down Expand Up @@ -615,7 +617,7 @@ export abstract class BaseChatOpenAI<
: this._convertChatOpenAIToolToCompletionsTool(tool, { strict })
),
...kwargs,
});
} as Partial<CallOptions>);
}

override async stream(input: BaseLanguageModelInput, options?: CallOptions) {
Expand Down Expand Up @@ -894,7 +896,7 @@ export abstract class BaseChatOpenAI<
kwargs: { method: "json_mode" },
schema: { title: name ?? "extract", ...asJsonSchema },
},
});
} as Partial<CallOptions>);
} else if (method === "jsonSchema") {
const openaiJsonSchemaParams = {
name: name ?? "extract",
Expand All @@ -917,7 +919,7 @@ export abstract class BaseChatOpenAI<
...asJsonSchema,
},
},
});
} as Partial<CallOptions>);
if (isInteropZodSchema(schema)) {
const altParser = StructuredOutputParser.fromZodSchema(schema);
outputParser = RunnableLambda.from<AIMessageChunk, RunOutput>(
Expand Down Expand Up @@ -960,7 +962,7 @@ export abstract class BaseChatOpenAI<
},
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
...(config?.strict !== undefined ? { strict: config.strict } : {}),
});
} as Partial<CallOptions>);
outputParser = new JsonOutputKeyToolsParser({
returnSingle: true,
keyName: functionName,
Expand Down Expand Up @@ -1004,7 +1006,7 @@ export abstract class BaseChatOpenAI<
},
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
...(config?.strict !== undefined ? { strict: config.strict } : {}),
});
} as Partial<CallOptions>);
outputParser = new JsonOutputKeyToolsParser<RunOutput>({
returnSingle: true,
keyName: functionName,
Expand Down
7 changes: 5 additions & 2 deletions libs/providers/langchain-openai/src/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
import { chunkArray } from "@langchain/core/utils/chunk_array";
import type { OpenAIApiKey } from "./types.js";
import { getEndpoint, OpenAIEndpointConfig } from "./utils/azure.js";
import { wrapOpenAIClientError } from "./utils/client.js";

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

protected clientConfig: ClientOptions;

protected apiKey?: OpenAIApiKey;

constructor(
fields?: Partial<OpenAIEmbeddingsParams> & {
verbose?: boolean;
/**
* The OpenAI API key to use.
* Alias for `apiKey`.
*/
openAIApiKey?: string;
openAIApiKey?: OpenAIApiKey;
/** The OpenAI API key to use. */
apiKey?: string;
apiKey?: OpenAIApiKey;
configuration?: ClientOptions;
}
) {
Expand Down
5 changes: 3 additions & 2 deletions libs/providers/langchain-openai/src/llms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@langchain/core/language_models/llms";
import { chunkArray } from "@langchain/core/utils/chunk_array";
import type {
OpenAIApiKey,
OpenAICallOptions,
OpenAICoreRequestOptions,
OpenAIInput,
Expand Down Expand Up @@ -121,9 +122,9 @@ export class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions>

streaming = false;

openAIApiKey?: string;
openAIApiKey?: OpenAIApiKey;

apiKey?: string;
apiKey?: OpenAIApiKey;

organization?: string;

Expand Down
9 changes: 5 additions & 4 deletions libs/providers/langchain-openai/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OpenAI as OpenAIClient } from "openai";
import type { OpenAI as OpenAIClient, ClientOptions } from "openai";
import type {
ResponseFormatText,
ResponseFormatJSONObject,
Expand All @@ -24,6 +24,8 @@ export type OpenAIChatModelId =

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

export type OpenAIApiKey = ClientOptions["apiKey"];

export declare interface OpenAIBaseInput {
/** Sampling temperature to use */
temperature: number;
Expand Down Expand Up @@ -103,12 +105,12 @@ export declare interface OpenAIBaseInput {
* `OPENAI_API_KEY` environment variable.
* Alias for `apiKey`
*/
openAIApiKey?: string;
openAIApiKey?: OpenAIApiKey;
/**
* API key to use when making requests to OpenAI. Defaults to the value of
* `OPENAI_API_KEY` environment variable.
*/
apiKey?: string;
apiKey?: OpenAIApiKey;

/**
* The verbosity of the model's response.
Expand Down Expand Up @@ -285,7 +287,6 @@ export interface AzureOpenAIInput {
export interface AzureOpenAIChatInput
extends OpenAIChatInput,
AzureOpenAIInput {
openAIApiKey?: string;
openAIApiVersion?: string;
openAIBasePath?: string;
deploymentName?: string;
Expand Down