langchain
langchain copied to clipboard
`openai` has no `ChatCompletion` attribute
Trying to initialize a ChatOpenAI
is resulting in this error:
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI(temperature=0)
openai
has noChatCompletion
attribute, this is likely due to an old version of the openai package.
I've upgraded all my packages to latest...
pip3 list
Package Version
------------------------ ---------
langchain 0.0.133
openai 0.27.4
I can't replicate this, here's a minimal example from the docs:
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
chat = ChatOpenAI(temperature=0)
chain = LLMChain(llm=chat, prompt=prompt)
print(chain.run('colorful socks'))
langchain version -> 0.0.133 and openai -> 0.27.4
Same here with openai version 0.27.4 and langchain 0.0.134. Everything was OK 4 hours before. Can't remember what I had done.
Same here with openai version 0.27.4 and langchain 0.0.134. Everything was OK 4 hours before. Can't remember what I had done.
My bad. I put openai.py to the current directory and run the python script that import openai which lead to the error. Removed openai.py and everything is OK.
I also faced similar issues, but it's not because accidentally putting openai.py into current directory.
I suspect that openai change their code from ChatCompletion
to Completion
already, and causing this error...
Source: https://pypi.org/project/openai/
the langchain docs have changed to:
llm.predict("say hi!")
'\n\nHi there!'
chat_model.predict("say hi!")
'Hello there!'
which is now causing this issue... what openai package do we have to lock to (not indicated in docs), but the getting started page is failing miserably
For a temporary fix/workaround I was able to remove the try ... except
block here:
https://github.com/hwchase17/langchain/blob/dcee8936c120aa0037d82713b10e43411cedcf09/langchain/chat_models/azure_openai.py#L93C1-L100
The new code snippet would look like this
openai.api_key = openai_api_key
if openai_organization:
openai.organization = openai_organization
if openai_api_base:
openai.api_base = openai_api_base
values["client"] = openai.ChatCompletion
if values["n"] < 1:
raise ValueError("n must be at least 1.")
if values["n"] > 1 and values["streaming"]:
raise ValueError("n must be 1 when streaming.")
return values
Hi, @ChuckJonas! 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.
Based on my understanding, you were experiencing an error when trying to initialize a ChatOpenAI
object due to the openai
package not having a ChatCompletion
attribute. Other users have tried to replicate the issue but couldn't. One user accidentally placed an openai.py
file in the current directory, causing the error. Another user suspects that the openai
package has changed the attribute from ChatCompletion
to Completion
. The documentation for langchain
has also changed, causing confusion.
However, the issue has been resolved by marcjulianschwarz who suggested a temporary fix/workaround by modifying the code in azure_openai.py
.
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 understanding and contribution to the LangChain project!