L2 openai.api_key VS os.environ['OPENAI_API_KEY']

In the second code cell I commented out the line:

openai.api_key  = os.environ['OPENAI_API_KEY']

and I ran this short example:

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm = OpenAI()
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

it still worked.
So if one sets the API key in the .env file for example with “OPENAI_API_KEY” then one does not need to set it in opena9_api_key, correct?
Is there some specific reason that line is used?


Hi @gkouro

Welcome to the community.

i’m not quite sure if i fully understand your question.

however, the notebooks on the course platform load all the environment variables for you.

But the notebooks also are develop in a way that you can use it locally. That might be a explanation for your question i think.

Best regards