langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
11.74k stars 1.96k forks source link

400 Either API-KEY or Authorization header must be provided but not both #3856

Closed KaterynaHryhorenko closed 6 months ago

KaterynaHryhorenko commented 6 months ago

I'm trying to setup ChatOpenAI according to documentation, but I recieved an error: BadRequestError: 400 Either API-KEY or Authorization header must be provided but not both. I'm not sure what is going wrong could you please help with this?

Code Example

import { ChatOpenAI } from 'langchain/chat_models/openai'

const chat = new ChatOpenAI({
temperature: 0.9, 
azureOpenAIApiDeploymentName: "gpt-35-turbo",  
azureOpenAIApiKey: "key", 
azureOpenAIApiVersion: '2023-06-01-preview',  
azureOpenAIBasePath: "https://deployment_name.com/openai/deployments"  
});
dosubot[bot] commented 6 months ago

🤖

Hello @KaterynaHryhorenko,

I'm Dosu, a bot designed to assist with bugs, answer questions, and guide potential contributors. While we wait for a human maintainer, I'm here to lend a hand! Let's tackle this challenge together.

The error you're encountering, BadRequestError: 400 Either API-KEY or Authorization header must be provided but not both, is likely due to the presence of both OPENAI_API_KEY and AZURE_OPENAI_API_KEY in your environment variables or in the fields passed to the ChatOpenAI constructor.

In the ChatOpenAI constructor, both this.openAIApiKey and this.azureOpenAIApiKey are being set from either the fields passed to the constructor or from the environment variables. If both keys are present, it could lead to a situation where both are being sent in the request headers, causing the BadRequestError.

Here is the relevant code snippet from the ChatOpenAI constructor:

this.openAIApiKey =
  fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");

this.azureOpenAIApiKey =
  fields?.azureOpenAIApiKey ??
  getEnvironmentVariable("AZURE_OPENAI_API_KEY");

To resolve this issue, ensure that only one of the API keys (OPENAI_API_KEY or AZURE_OPENAI_API_KEY) is provided in the environment variables or in the fields passed to the ChatOpenAI constructor.

You can find more details about this in the langchainjs repository.

Please let me know if this resolves your issue or if you need further assistance.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

KaterynaHryhorenko commented 6 months ago

It works. Thank you!