langchain icon indicating copy to clipboard operation
langchain copied to clipboard

OpenAI and GooseAI each overwrite openai.api_key

Open zswitten opened this issue 2 years ago • 3 comments

import os
os.environ['OPENAI_API_KEY'] = 'ooo'
os.environ['GOOSEAI_API_KEY'] = 'ggg'
import openai
print(openai.api_key)
o = OpenAI()
print(openai.api_key)
g = GooseAI()
print(openai.api_key)
o = OpenAI()
print(openai.api_key)

------

ooo
ooo
ggg
ooo

This makes it annoying (impossible?) to switch back and forth between calls to OpenAI models like Babbage and GooseAI models like GPT-J in the same notebook.

prompt = 'The best basketball player of all time is '
args = {'max_tokens': 3, 'temperature': 0}
openai_llm = OpenAI(**args)
print(openai_llm(prompt))
gooseai_llm = GooseAI(**args)
print(gooseai_llm(prompt))
print(openai_llm(prompt))

---

Michael Jordan

Michael Jordan

InvalidRequestError: you must provide a model parameter

I'm pretty sure this behavior comes from the validate_environment method in the two llm files which despite its name seems to not just validate the environment but also change it.

zswitten avatar Feb 21 '23 00:02 zswitten

Actually I think the more pertinent issue (since both your GOOSE_API_KEY and OPENAI_API_KEY should be valid) is that https://github.com/hwchase17/langchain/blob/master/langchain/llms/gooseai.py overwrites openai.api_base.

zswitten avatar Feb 21 '23 01:02 zswitten

A workaround is to set openai.api_base = 'https://api.openai.com/v1 before each call to OpenAI.

zswitten avatar Feb 21 '23 01:02 zswitten

Aha! This explains the errors I was getting on the OpenAI endpoint:

InvalidRequestError: you must provide a model parameter 

after using GooseAI!

This should be upgraded to a bug IMO.

FergusFettes avatar Mar 09 '23 09:03 FergusFettes

Hi, @zswitten! I'm Dosu, and I'm helping 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 reported was about the openai.api_key getting overwritten when using both OpenAI and GooseAI in the same notebook. The suggested workaround was to set openai.api_base before each call to OpenAI. Another user mentioned that they were experiencing similar errors on the OpenAI endpoint and suggested upgrading it to a bug.

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 Aug 27 '23 16:08 dosubot[bot]