VectorstoreIndexCrerator does't find openai API KEY

There shoud be a place to send the parameter opnai.api-key somewhere

I can’t get rid of this error:

index = VectorstoreIndexCreator(

File “pydantic/”, line 339, in pydantic.main.BaseModel.init
File “pydantic/”, line 1066, in pydantic.main.validate_model
File “pydantic/”, line 439, in pydantic.fields.ModelField.get_default
File “pydantic/”, line 341, in pydantic.main.BaseModel.init
pydantic.error_wrappers.ValidationError: 1 validation error for OpenAIEmbeddings
Did not find openai_api_key, please add an environment variable OPENAI_API_KEY which contains it, or pass openai_api_key as a named parameter. (type=value_error)

Cna somebody help pls?

As far as I could tell, when using LangChain API, setting the key directly in the code is not an available option: you must set it in the environment. That can be done either through the OS or in the code, but it must be done that way. There are existing threads that discuss this and show how.

When using google colab, this works:

!pip install openai

!pip install python-dotenv

import openai

import os

import sys

import dotenv

from dotenv import dotenv_values

env_vars = dotenv_values(‘your .env file path’)

sys.path.append(‘your .env file path’)

openai.api_key = env_vars[‘OPENAI_API_KEY’] # get the variable from ,env file

os.environ[‘OPENAI_API_KEY’] = env_vars[‘OPENAI_API_KEY’] # sets the environ variable

I think that is what I suggested above, but thanks for showing it in Python. What doesn’t work is just assigning a string literal value to the key as it was done in some of the early examples of the prompt engineering course. Then it was an option to do it that way. With langchain, not so.

1 Like