Unable to Specify Azure OpenAI API Deployment Name Manually Without Environment Variable
rossanodr opened this issue · 4 comments
Checked other resources
- I added a very descriptive title to this issue.
- I searched the LangChain.js documentation with the integrated search.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangChain.js rather than my code.
- The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
Example Code
import { OpenAIEmbeddings } from "@langchain/openai";
import { AzureChatOpenAI } from "@langchain/openai";
// Attempting to initialize AzureChatOpenAI with manual deployment parameters
const model = new AzureChatOpenAI({
model: 'modelName',
azureOpenAIApiDeploymentName: 'my-deployment-name',
azureOpenAIApiInstanceName: "my-instance-name",
});
// Attempting to initialize OpenAIEmbeddings with manual API keys and deployment parameters
const embeddings = new OpenAIEmbeddings({
openAIApiKey: process.env.OPENAI_API_KEY,
model: "text-embedding-3-small",
azureOpenAIApiKey: "",
azureOpenAIApiInstanceName: "",
azureOpenAIApiDeploymentName: "",
});
Error Message and Stack Trace (if applicable)
⨯ Error: Azure OpenAI API deployment name not found
Description
When initializing AzureChatOpenAI
or OpenAIEmbeddings
without relying solely on environment variables, the application defaults to using the deployment name defined in the AZURE_OPENAI_API_DEPLOYMENT_NAME
environment variable. This limitation prevents the use of multiple deployment names dynamically within the same project. Specifically:
-
AzureChatOpenAI Initialization:
- Attempting to initialize
AzureChatOpenAI
with a specific deployment name manually results in an error if the environment variableAZURE_OPENAI_API_DEPLOYMENT_NAME
is not set. - When multiple API deployment names exist, only the one specified in the environment variable can be utilized, restricting flexibility.
- Attempting to initialize
-
OpenAIEmbeddings Initialization:
- Even when using the OpenAI API, the Azure environment variables override the specified parameters, causing
OpenAIEmbeddings
to attempt accessing Azure embeddings using the deployment name defined in the environment variables.
- Even when using the OpenAI API, the Azure environment variables override the specified parameters, causing
Desired Behavior:
Allow developers to specify the azureOpenAIApiDeploymentName
(and other related parameters) programmatically without being forced to rely on environment variables. This flexibility is essential for projects that utilize multiple deployment names and cannot have a single default deployment name.
System Info
langchain@npm:0.2.20
├─ Instances: 1
├─ Version: 0.2.20
│
└─ Dependencies
├─ @langchain/core@npm:^0.2.18 → npm:0.2.31
├─ @langchain/openai@npm:>=0.1.0 <0.3.0 → npm:0.2.10
├─ @langchain/textsplitters@npm:~0.0.0 → npm:0.0.3
├─ binary-extensions@npm:^2.2.0 → npm:2.3.0
├─ js-tiktoken@npm:^1.0.12 → npm:1.0.12
├─ js-yaml@npm:^4.1.0 → npm:4.1.0
├─ jsonpointer@npm:^5.0.1 → npm:5.0.1
├─ langsmith@npm:^0.1.56-rc.1 → npm:0.1.64
├─ openapi-types@npm:^12.1.3 → npm:12.1.3
├─ p-retry@npm:4 → npm:4.6.2
├─ uuid@npm:^10.0.0 → npm:10.0.0
├─ yaml@npm:^2.2.1 → npm:2.6.0
├─ zod-to-json-schema@npm:^3.22.3 → npm:3.23.2
└─ zod@npm:^3.22.4 → npm:3.23.8
I found a similar issue that was closed, which might be relevant to your problem:
- Custom azureOpenAIApiBasePath should be applied also with azureADTokenProvider - This issue was closed by a pull request that addressed the problem by allowing custom Azure configurations to be applied programmatically [1].
To specify the azureOpenAIApiDeploymentName
and other related parameters programmatically in LangChain.js without relying on environment variables, you can directly pass these parameters as part of the configuration object when initializing the classes. Here's an example of how you can do this:
import { AzureChatOpenAI } from "@langchain/openai";
import { OpenAIEmbeddings } from "@langchain/openai";
// Initialize AzureChatOpenAI with manual deployment parameters
const model = new AzureChatOpenAI({
model: 'modelName',
azureOpenAIApiKey: 'your-api-key',
azureOpenAIApiDeploymentName: 'my-deployment-name',
azureOpenAIApiInstanceName: 'my-instance-name',
azureOpenAIApiVersion: 'your-api-version', // if needed
});
// Initialize OpenAIEmbeddings with manual API keys and deployment parameters
const embeddings = new OpenAIEmbeddings({
azureOpenAIApiKey: 'your-api-key',
azureOpenAIApiInstanceName: 'my-instance-name',
azureOpenAIApiDeploymentName: 'my-deployment-name',
model: "text-embedding-3-small",
});
This approach allows you to dynamically specify different deployment names and other configurations without relying on environment variables, providing the flexibility you need for projects with multiple deployment names [2][3].
To address the issue of dynamically specifying the azureOpenAIApiDeploymentName
and other related parameters without relying on environment variables, ensure that you are passing all necessary parameters directly in the configuration object when initializing the classes. If this approach is not working, double-check that all required parameters are correctly set and that there are no typos or missing values.
For AzureChatOpenAI
, make sure you include:
azureOpenAIApiKey
azureOpenAIApiDeploymentName
azureOpenAIApiInstanceName
For OpenAIEmbeddings
, ensure similar parameters are set if applicable.
If the problem persists, it might be due to a bug or limitation in the current version of LangChain.js. Consider checking for any updates or patches that might address this issue. If the problem continues, reviewing the implementation details of these classes might provide further insights into any overlooked configuration requirements.
There's a conflict between AZURE_OPENAI_API_KEY and OPENAI_API_KEY. I had to rename AZURE_OPENAI_API_KEY, and now everything is working properly.