Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(openai): Make only AzureOpenAI respect Azure env vars, remove class defaults, update withStructuredOutput defaults #7535

Merged
merged 22 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion libs/langchain-openai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/openai",
"version": "0.3.17",
"version": "0.4.0-rc.0",
"description": "OpenAI integrations for LangChain.js",
"type": "module",
"engines": {
Expand Down
71 changes: 58 additions & 13 deletions libs/langchain-openai/src/azure/chat_models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import {
LangSmithParams,
type BaseChatModelParams,
} from "@langchain/core/language_models/chat_models";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { ChatOpenAI } from "../chat_models.js";
import { OpenAIEndpointConfig, getEndpoint } from "../utils/azure.js";
import {
AzureOpenAIInput,
LegacyOpenAIInput,
OpenAIChatInput,
OpenAICoreRequestOptions,
} from "../types.js";

export type { AzureOpenAIInput };

/**
* Azure OpenAI chat model integration.
*
Expand Down Expand Up @@ -425,12 +427,27 @@ import {
* </details>
*/
export class AzureChatOpenAI extends ChatOpenAI {
azureOpenAIApiVersion?: string;

azureOpenAIApiKey?: string;

azureADTokenProvider?: () => Promise<string>;

azureOpenAIApiInstanceName?: string;

azureOpenAIApiDeploymentName?: string;

azureOpenAIBasePath?: string;

azureOpenAIEndpoint?: string;
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved

_llmType(): string {
return "azure_openai";
}

get lc_aliases(): Record<string, string> {
return {
...super.lc_aliases,
openAIApiKey: "openai_api_key",
openAIApiVersion: "openai_api_version",
openAIBasePath: "openai_api_base",
Expand All @@ -442,6 +459,13 @@ export class AzureChatOpenAI extends ChatOpenAI {
};
}

get lc_secrets(): { [key: string]: string } | undefined {
return {
...super.lc_secrets,
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
};
}

constructor(
fields?: Partial<OpenAIChatInput> &
Partial<AzureOpenAIInput> & {
Expand All @@ -450,21 +474,42 @@ export class AzureChatOpenAI extends ChatOpenAI {
openAIBasePath?: string;
deploymentName?: string;
} & BaseChatModelParams & {
configuration?: ClientOptions & LegacyOpenAIInput;
configuration?: ClientOptions;
}
) {
const newFields = fields ? { ...fields } : fields;
if (newFields) {
// don't rewrite the fields if they are already set
newFields.azureOpenAIApiDeploymentName =
newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
newFields.azureOpenAIApiKey =
newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
newFields.azureOpenAIApiVersion =
newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
}
super(fields);
this.azureOpenAIApiDeploymentName =
fields?.azureOpenAIApiDeploymentName ?? fields?.deploymentName;
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.openAIApiKey ??
fields?.apiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ?? fields?.openAIApiVersion;
this.azureOpenAIApiInstanceName =
fields?.azureOpenAIApiInstanceName ??
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");

this.azureOpenAIApiDeploymentName =
fields?.azureOpenAIApiDeploymentName ??
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not in py but we should add it in py


super(newFields);
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ??
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");

this.azureOpenAIBasePath =
fields?.azureOpenAIBasePath ??
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");

this.azureOpenAIEndpoint =
fields?.azureOpenAIEndpoint ??
getEnvironmentVariable("AZURE_OPENAI_ENDPOINT");

if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
throw new Error("Azure OpenAI API key or Token Provider not found");
}
}

getLsParams(options: this["ParsedCallOptions"]): LangSmithParams {
Expand Down
54 changes: 35 additions & 19 deletions libs/langchain-openai/src/azure/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
OpenAI as OpenAIClient,
} from "openai";
import { OpenAIEmbeddings, OpenAIEmbeddingsParams } from "../embeddings.js";
import {
AzureOpenAIInput,
OpenAICoreRequestOptions,
LegacyOpenAIInput,
} from "../types.js";
import { AzureOpenAIInput, OpenAICoreRequestOptions } from "../types.js";
import { getEndpoint, OpenAIEndpointConfig } from "../utils/azure.js";
import { wrapOpenAIClientError } from "../utils/openai.js";
import { getEnvironmentVariable } from "@langchain/core/utils/env";

Check failure on line 10 in libs/langchain-openai/src/azure/embeddings.ts

View workflow job for this annotation

GitHub Actions / Check linting

`@langchain/core/utils/env` import should occur before import of `../embeddings.js`

export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
azureOpenAIApiVersion?: string;

azureOpenAIApiKey?: string;

azureADTokenProvider?: () => Promise<string>;

azureOpenAIApiInstanceName?: string;

azureOpenAIApiDeploymentName?: string;

azureOpenAIBasePath?: string;

constructor(
fields?: Partial<OpenAIEmbeddingsParams> &
Partial<AzureOpenAIInput> & {
Expand All @@ -22,21 +31,28 @@
configuration?: ClientOptions;
deploymentName?: string;
openAIApiVersion?: string;
},
configuration?: ClientOptions & LegacyOpenAIInput
}
) {
const newFields = { ...fields };
if (Object.entries(newFields).length) {
// don't rewrite the fields if they are already set
newFields.azureOpenAIApiDeploymentName =
newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
newFields.azureOpenAIApiKey =
newFields.azureOpenAIApiKey ?? newFields.apiKey;
newFields.azureOpenAIApiVersion =
newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
}

super(newFields, configuration);
super(fields);
this.batchSize = fields?.batchSize ?? 1;
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
this.azureADTokenProvider = fields?.azureADTokenProvider;
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ??
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
this.azureOpenAIBasePath =
fields?.azureOpenAIBasePath ??
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
this.azureOpenAIApiInstanceName =
fields?.azureOpenAIApiInstanceName ??
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
this.azureOpenAIApiDeploymentName =
(fields?.azureOpenAIApiEmbeddingsDeploymentName ||
fields?.azureOpenAIApiDeploymentName) ??
(getEnvironmentVariable("AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME") ||
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
}

protected async embeddingWithRetry(
Expand Down
86 changes: 74 additions & 12 deletions libs/langchain-openai/src/azure/llms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,42 @@
OpenAIInput,
AzureOpenAIInput,
OpenAICoreRequestOptions,
LegacyOpenAIInput,
} from "../types.js";
import { getEnvironmentVariable } from "@langchain/core/utils/env";

Check failure on line 10 in libs/langchain-openai/src/azure/llms.ts

View workflow job for this annotation

GitHub Actions / Check linting

`@langchain/core/utils/env` import should occur before import of `../llms.js`

export class AzureOpenAI extends OpenAI {
azureOpenAIApiVersion?: string;

azureOpenAIApiKey?: string;

azureADTokenProvider?: () => Promise<string>;

azureOpenAIApiInstanceName?: string;

azureOpenAIApiDeploymentName?: string;

azureOpenAIBasePath?: string;

azureOpenAIEndpoint?: string;

get lc_aliases(): Record<string, string> {
return {
...super.lc_aliases,
openAIApiKey: "openai_api_key",
openAIApiVersion: "openai_api_version",
openAIBasePath: "openai_api_base",
deploymentName: "deployment_name",
azureOpenAIEndpoint: "azure_endpoint",
azureOpenAIApiVersion: "openai_api_version",
azureOpenAIBasePath: "openai_api_base",
azureOpenAIApiDeploymentName: "deployment_name",
};
}

get lc_secrets(): { [key: string]: string } | undefined {
return {
...super.lc_secrets,
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
};
}

Expand All @@ -26,21 +53,56 @@
deploymentName?: string;
} & Partial<AzureOpenAIInput> &
BaseLLMParams & {
configuration?: ClientOptions & LegacyOpenAIInput;
configuration?: ClientOptions;
}
) {
const newFields = fields ? { ...fields } : fields;
if (newFields) {
// don't rewrite the fields if they are already set
newFields.azureOpenAIApiDeploymentName =
newFields.azureOpenAIApiDeploymentName ?? newFields.deploymentName;
newFields.azureOpenAIApiKey =
newFields.azureOpenAIApiKey ?? newFields.openAIApiKey;
newFields.azureOpenAIApiVersion =
newFields.azureOpenAIApiVersion ?? newFields.openAIApiVersion;
super(fields);
this.azureOpenAIApiDeploymentName =
(fields?.azureOpenAIApiCompletionsDeploymentName ||
fields?.azureOpenAIApiDeploymentName) ??
(getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
fields?.openAIApiKey ??
fields?.apiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ?? fields?.openAIApiVersion;
this.azureOpenAIApiInstanceName =
fields?.azureOpenAIApiInstanceName ??
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");

this.azureOpenAIApiDeploymentName =
fields?.azureOpenAIApiDeploymentName ??
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME");

this.azureOpenAIApiVersion =
fields?.azureOpenAIApiVersion ??
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");

this.azureOpenAIBasePath =
fields?.azureOpenAIBasePath ??
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");

this.azureOpenAIEndpoint =
fields?.azureOpenAIEndpoint ??
getEnvironmentVariable("AZURE_OPENAI_ENDPOINT");

if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
throw new Error("Azure OpenAI API key or Token Provider not found");
}
this.azureOpenAIApiKey =
fields?.azureOpenAIApiKey ??
getEnvironmentVariable("AZURE_OPENAI_API_KEY");

super(newFields);
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;

if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
throw new Error(
"OpenAI or Azure OpenAI API key or Token Provider not found"
);
}
}

protected _getClientOptions(options: OpenAICoreRequestOptions | undefined) {
Expand Down
Loading
Loading