langchain-ai / langchainjs

πŸ¦œπŸ”— Build context-aware reasoning applications πŸ¦œπŸ”—
https://js.langchain.com/docs/
MIT License
12.71k stars 2.19k forks source link

Unable to Specify Azure OpenAI API Deployment Name Manually Without Environment Variable #7206

Closed rossanodr closed 5 hours ago

rossanodr commented 2 days ago

Checked other resources

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:

  1. AzureChatOpenAI Initialization:

    • Attempting to initialize AzureChatOpenAI with a specific deployment name manually results in an error if the environment variable AZURE_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.
  2. 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.

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

dosubot[bot] commented 2 days ago

I found a similar issue that was closed, which might be relevant to your problem:

  1. 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 continue talking to Dosu, mention @dosu.

rossanodr commented 1 day ago

dynamically within the same proj @dosu, this is exactly what is not working.

dosubot[bot] commented 1 day ago

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:

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.

To continue talking to Dosu, mention @dosu.

rossanodr commented 5 hours ago

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.