I have a script that prompts the user to enter their OpenAI API key, validates it, and then uses the ChatOpenAI
class from langchain_openai
to create an agent for interacting with a pandas DataFrame. However, when I enter a valid API key and a prompt, I encounter the following error:
AttributeError: module ‘openai’ has no attribute ‘OpenAI’
Package dependencies and their versions:
openai==0.28.0
langchain==0.2.11
langchain-openai==0.1.19
langchain-community==0.2.10
langchain-experimental== 0.0.63
Here’s the relevant portion of my code:
import openai
from langchain_openai import ChatOpenAI
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
def check_openai_key(api_key):
openai.api_key = api_key
try:
openai.Model.list()
return True
except openai.error.AuthenticationError:
return False
except Exception as e:
print(f"An error occurred: {e}")
return False
openai_api_key = st.text_input("Enter your OpenAPI key",type="password",placeholder="sk-...",)
is_valid = check_openai_key(openai_api_key)
if openai_api_key:
if is_valid:
if user_prompt.strip():
chat = ChatOpenAI(openai_api_key = openai_api_key, temperature=0.9)
agent = create_pandas_dataframe_agent(chat, data, verbose=True, allow_dangerous_code=True)
response = agent.invoke(user_prompt)
# Print response["output]
else:
# Warning message : "⚠️ Please enter a prompt."
else:
# Warning message : "⚠️ Invalid OpenAI API key. Please enter a valid key."
else:
# Warning message : "⚠️ Please enter your OpenAI API key."