benman1 / generative_ai_with_langchain

Build large language model (LLM) apps with Python, ChatGPT and other models. This is the companion repository for the book on generative AI with LangChain.
https://amzn.to/43PuIkQ
MIT License
636 stars 256 forks source link

AttributeError: module 'openai' has no attribute 'OpenAI' #36

Closed bdeva1975 closed 6 months ago

bdeva1975 commented 7 months ago

I am getting an error for the following code in Chapter 4:-

from langchain import PromptTemplate, OpenAI from langchain.schema import StrOutputParser llm = OpenAI() prompt = PromptTemplate.from_template( "Summarize this text: {text}?" ) runnable = prompt | llm | StrOutputParser() summary = runnable.invoke({"text": text})

AttributeError: module 'openai' has no attribute 'OpenAI'

I have set the OpenAI API Key as:

import os

Replace "" with your actual key

os.environ["OPENAI_API_KEY"] = ""

benman1 commented 7 months ago

Hi @bdeva1975, similar question to your other issue: could you please check the versions you are using and post them here? Here's the requirements.txt file relevant for the book and the code: https://github.com/benman1/generative_ai_with_langchain/blob/main/requirements.txt

bdeva1975 commented 7 months ago

jupyter>=1.0.0 jupyterlab langchain[docarray]==0.0.284 langchain_experimental==0.0.25 langsmith==0.0.65 huggingface_hub>=0.14.1 openai==0.28.1 google-cloud-aiplatform>=1.26.0 google-generativeai replicate ruff>=0.0.278 pypdf>=3.12.2 tiktoken>=0.4.0 duckduckgo_search>=3.8.4 streamlit>=1.24.1 arxiv>=1.4.8 wikipedia>=1.4.0 wolframalpha>=5.0.0 unstructured>=0.8.1 pandoc>=1.1.0 # needs system install as well! sentence-transformers>=2.2.2 gradio>=3.38.0 gpt4all>=1.0.6 google-api-python-client>=2.94.0 apify-client>=1.4.0 chromadb>=0.4.8 lanarky==0.7.16 ray[serve]>=2.6.3 faiss-cpu>=1.7.4 promptwatch>=0.2.9 langchain_decorators>=0.2.3

This is what I found from your github repository and this is what my requirements.txt file looks like.

Sorry to say, but nothing is working for me, with regard to either OpenAI and HuggingFace. I have ample tokens for OpenAI for the model gpt-3.5 turbo. But nothing seems to work from your book.

With Regards,

Devasish

On Sat, Apr 6, 2024 at 7:03 PM Ben Auffarth @.***> wrote:

Hi @bdeva1975 https://github.com/bdeva1975, similar question to your other issue: could you please check the versions you are using and post them here? Here's the requirements.txt file relevant for the book and the code: https://github.com/benman1/generative_ai_with_langchain/blob/main/requirements.txt

— Reply to this email directly, view it on GitHub https://github.com/benman1/generative_ai_with_langchain/issues/36#issuecomment-2041083282, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWJCHEXGQVQ5P7OUC2YITATY372RBAVCNFSM6AAAAABF2DHMG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBRGA4DGMRYGI . You are receiving this because you were mentioned.Message ID: @.***>

bdeva1975 commented 7 months ago

Dear Ben,

Thanks for your prompt response, always. I must admit I completed till Page 106, without any hassles, as of now.

With Regards,

Devasish

On Mon, Apr 8, 2024 at 7:51 AM Devasish Banerjee @.***> wrote:

jupyter>=1.0.0 jupyterlab langchain[docarray]==0.0.284 langchain_experimental==0.0.25 langsmith==0.0.65 huggingface_hub>=0.14.1 openai==0.28.1 google-cloud-aiplatform>=1.26.0 google-generativeai replicate ruff>=0.0.278 pypdf>=3.12.2 tiktoken>=0.4.0 duckduckgo_search>=3.8.4 streamlit>=1.24.1 arxiv>=1.4.8 wikipedia>=1.4.0 wolframalpha>=5.0.0 unstructured>=0.8.1 pandoc>=1.1.0 # needs system install as well! sentence-transformers>=2.2.2 gradio>=3.38.0 gpt4all>=1.0.6 google-api-python-client>=2.94.0 apify-client>=1.4.0 chromadb>=0.4.8 lanarky==0.7.16 ray[serve]>=2.6.3 faiss-cpu>=1.7.4 promptwatch>=0.2.9 langchain_decorators>=0.2.3

This is what I found from your github repository and this is what my requirements.txt file looks like.

Sorry to say, but nothing is working for me, with regard to either OpenAI and HuggingFace. I have ample tokens for OpenAI for the model gpt-3.5 turbo. But nothing seems to work from your book.

With Regards,

Devasish

On Sat, Apr 6, 2024 at 7:03 PM Ben Auffarth @.***> wrote:

Hi @bdeva1975 https://github.com/bdeva1975, similar question to your other issue: could you please check the versions you are using and post them here? Here's the requirements.txt file relevant for the book and the code: https://github.com/benman1/generative_ai_with_langchain/blob/main/requirements.txt

— Reply to this email directly, view it on GitHub https://github.com/benman1/generative_ai_with_langchain/issues/36#issuecomment-2041083282, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWJCHEXGQVQ5P7OUC2YITATY372RBAVCNFSM6AAAAABF2DHMG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBRGA4DGMRYGI . You are receiving this because you were mentioned.Message ID: @.***>

benman1 commented 7 months ago

Hi @bdeva1975! Nice to hear you are working your way through the book, sorry for the delay in responding. Re hugging face, have you tried the feedback? It'd be good to know so I can close the issue.

Could you please check the versions directly? Instead of showing me your requirements file, I'd like to see what's actually installed. I am strongly suspecting you have a different version of LangChain. Please execute pip show langchain and paste the output.

Here's what I get:

(base) ben@admins-MBP ~ % pip show langchain
Name: langchain
Version: 0.0.251
Summary: Building applications with LLMs through composability
Home-page: https://www.github.com/hwchase17/langchain
Author:
Author-email:
License: MIT
Location: /Users/ben/anaconda3/lib/python3.11/site-packages
Requires: aiohttp, dataclasses-json, langsmith, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity
Required-by: autopack-tools, langchain-decorators, langchain-experimental, promptwatch
(base) ben@admins-MBP ~ % python
Python 3.11.3 (main, Apr 19 2023, 18:51:09) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from langchain import OpenAI
>>>

The import works just fine.

bdeva1975 commented 7 months ago

Name: langchain Version: 0.1.14 Summary: Building applications with LLMs through composability Home-page: https://github.com/langchain-ai/langchain Author: Author-email: License: MIT Location: C:\Users\bdevasish\AppData\Local\anaconda3\Lib\site-packages Requires: aiohttp, dataclasses-json, jsonpatch, langchain-community, langchain-core, langchain-text-splitters, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity Required-by:

On Tue, Apr 9, 2024 at 5:32 PM Ben Auffarth @.***> wrote:

Hi @bdeva1975 https://github.com/bdeva1975! Nice to hear you are working your way through the book, sorry for the delay in responding. Re hugging face, have you tried the feedback <#m_-349894716010601523_34>? It'd be good to know so I can close the issue.

Could you please check the versions directly? Instead of showing me your requirements file, I'd like to see what's actually installed. I am strongly suspecting you have a different version of LangChain. Please execute pip show langchain and paste the output.

Here's what I get:

(base) @. ~ % pip show langchain Name: langchain Version: 0.0.251 Summary: Building applications with LLMs through composability Home-page: https://www.github.com/hwchase17/langchain Author: Author-email: License: MIT Location: /Users/ben/anaconda3/lib/python3.11/site-packages Requires: aiohttp, dataclasses-json, langsmith, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity Required-by: autopack-tools, langchain-decorators, langchain-experimental, promptwatch (base) @. ~ % python Python 3.11.3 (main, Apr 19 2023, 18:51:09) [Clang 14.0.6 ] on darwin Type "help", "copyright", "credits" or "license" for more information.

from langchain import OpenAI

The import works just fine.

— Reply to this email directly, view it on GitHub https://github.com/benman1/generative_ai_with_langchain/issues/36#issuecomment-2044973174, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWJCHEW6DIYF2DZH2EDDWILY4PKG3AVCNFSM6AAAAABF2DHMG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBUHE3TGMJXGQ . You are receiving this because you were mentioned.Message ID: @.***>

bdeva1975 commented 7 months ago

Dear Ben,

Please ignore my previous mail.

Please consider the following:-

Name: langchain Version: 0.1.14 Summary: Building applications with LLMs through composability Home-page: https://github.com/langchain-ai/langchain Author: Author-email: License: MIT Location: e:\generativeai_book\lib\site-packages Requires: aiohttp, async-timeout, dataclasses-json, jsonpatch, langchain-community, langchain-core, langchain-text-splitters, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity Required-by: lanarky, langchain-decorators, langchain-experimental, promptwatch

With Regards,

Devasish

On Wed, Apr 10, 2024 at 9:32 AM Devasish Banerjee @.***> wrote:

Name: langchain Version: 0.1.14 Summary: Building applications with LLMs through composability Home-page: https://github.com/langchain-ai/langchain Author: Author-email: License: MIT Location: C:\Users\bdevasish\AppData\Local\anaconda3\Lib\site-packages Requires: aiohttp, dataclasses-json, jsonpatch, langchain-community, langchain-core, langchain-text-splitters, langsmith, numpy, pydantic, PyYAML, requests, SQLAlchemy, tenacity Required-by:

On Tue, Apr 9, 2024 at 5:32 PM Ben Auffarth @.***> wrote:

Hi @bdeva1975 https://github.com/bdeva1975! Nice to hear you are working your way through the book, sorry for the delay in responding. Re hugging face, have you tried the feedback <#m_-4572997500577807403m-349894716010601523_34>? It'd be good to know so I can close the issue.

Could you please check the versions directly? Instead of showing me your requirements file, I'd like to see what's actually installed. I am strongly suspecting you have a different version of LangChain. Please execute pip show langchain and paste the output.

Here's what I get:

(base) @. ~ % pip show langchain Name: langchain Version: 0.0.251 Summary: Building applications with LLMs through composability Home-page: https://www.github.com/hwchase17/langchain Author: Author-email: License: MIT Location: /Users/ben/anaconda3/lib/python3.11/site-packages Requires: aiohttp, dataclasses-json, langsmith, numexpr, numpy, openapi-schema-pydantic, pydantic, PyYAML, requests, SQLAlchemy, tenacity Required-by: autopack-tools, langchain-decorators, langchain-experimental, promptwatch (base) @. ~ % python Python 3.11.3 (main, Apr 19 2023, 18:51:09) [Clang 14.0.6 ] on darwin Type "help", "copyright", "credits" or "license" for more information.

from langchain import OpenAI

The import works just fine.

— Reply to this email directly, view it on GitHub https://github.com/benman1/generative_ai_with_langchain/issues/36#issuecomment-2044973174, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWJCHEW6DIYF2DZH2EDDWILY4PKG3AVCNFSM6AAAAABF2DHMG6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBUHE3TGMJXGQ . You are receiving this because you were mentioned.Message ID: @.***>

benman1 commented 7 months ago

Here's the requirements.txt file relevant for the book and the code: https://github.com/benman1/generative_ai_with_langchain/blob/main/requirements.txt

Hi @bdeva1975! Again: the book and code are relevant to LangChain version 0.0.284. Have you done the installation as recommended in chapter 3? Please see my response above - the import works with the right version.