ChatOpenAI() suggesting validation error

I have been trying to implement the short course provided by Harrison Chase and Andrew on models,prompts, and parsers. I want to copy the code provided in the course into my notebook for learning. The first part of the code works by passing openai_api_key=“sk-xxxx”. But when I import langchain and pass ChatOpenAi(), I get validation error requesting no API key found. Is it that the code provided in the course cannot work outside the platforms notebook or I am not passing the correct class for ChatOpenAI()?

For lang chain you have to set the value in the environment. Either do it from the operating system before you launch the notebook, or do it inside the notebook. Either way, just assigning the string works for using openai API, but not for langchain.

I think, It’s a openai.api_key, not openai_api_key.

Just assigning the string doesn’t work for langchain api calls.

openai.api_key = 'string...'

is necessary but not sufficient. Add this

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

By doing that, I was able to run the langchain exercises on my laptop

can you provide the format to which you were able to use the ChatopenAI(). like
import modules

import langchain

pass the ChatOpenAI() so I can reference.

Sorry but I don’t understand the question. I just downloaded the notebooks and data files and ran them locally. For the prompt eng exercises I had been just sticking my key in as a string, but that threw an error which lead me to the solution shared above. Other than that, I didn’t change the notebooks.

Thanks. actually, it did work. its been helpful.

1 Like