langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Encapsulate API keys

Open amir-bio opened this issue 1 year ago • 3 comments

I'm building a flow where I'm using both gpt-3.5 and gpt-4 based chains and I need to use different API keys for each (due to API access + external factors)

Both ChatOpenAI and OpenAI set openai.api_key = openai_api_key which is a global variable on the package.

This means that if I instantiate multiple ChatOpenAI instances, the last one's API key will override the other ones and that one will be used when calling the OpenAI endpoints.

Based on https://github.com/openai/openai-python/issues/233#issuecomment-1464732160 there's an undocumented feature where we can pass the api_key on each openai client call and that key will be used.

As a side note, I've also noticed ChatOpenAI and a few other classes take in an optional openai_api_key as part of initialisation which is correctly used over the env var but the docstring says that the OPENAI_API_KEY env var should be set, which doesn't seem to be case. Can we confirm if this env var is needed elsewhere or if it's possible to just pass in the values when instantiating the chat models.

Thanks!

amir-bio avatar Apr 24 '23 15:04 amir-bio

I also encountered this problem. Is there a solution?

xxb0120 avatar May 08 '23 09:05 xxb0120

Same, can anyone look into this issue.

parulekarmihir avatar May 11 '23 08:05 parulekarmihir

the same with https://github.com/hwchase17/langchain/issues/2091

FlowerWrong avatar May 18 '23 08:05 FlowerWrong

Hi, @amir-bio! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, the issue you raised is about encapsulating API keys for different versions of GPT. You suggested passing the API key as a parameter when instantiating the models instead of using a global variable. There have been a few comments from other users expressing similar issues and asking for a solution. It seems that the issue has been resolved with your suggestion of passing the API key as a parameter.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!

dosubot[bot] avatar Sep 17 '23 17:09 dosubot[bot]