truera / trulens

Evaluation and Tracking for LLM Experiments
https://www.trulens.org/
MIT License
2.15k stars 187 forks source link

getting error that openai is not installed. #1145

Closed rajib76 closed 5 months ago

rajib76 commented 5 months ago

Getting the below error

Traceback (most recent call last):
  File "/Users/joyeed/truelens/true_lens_examples/01_rag_triad_relevance.py", line 31, in <module>
    openai_provider = OpenAI()
                      ^^^^^^^^
  File "/Users/joyeed/truelens/true_lens_examples/venv/lib/python3.11/site-packages/trulens_eval/feedback/provider/openai.py", line 75, in __init__
    self_kwargs['endpoint'] = OpenAIEndpoint(
                              ^^^^^^^^^^^^^^^
  File "/Users/joyeed/truelens/true_lens_examples/venv/lib/python3.11/site-packages/trulens_eval/utils/imports.py", line 363, in __call__
    raise self.exception_class(self.message)
ModuleNotFoundError: 
openai package is required for using OpenAI models.
You should be able to install it with pip:

    ```bash
    pip install "openai<2,>=1.1.1"

I am trying to run this program

import os
from typing import Optional, Union

from dotenv import load_dotenv
from trulens_eval import OpenAI

load_dotenv()
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')

def get_relevance(provider: Union[OpenAI], prompt, response):
    response = provider.relevance(
        prompt=prompt,
        response=response
    )

    return response

if __name__ == "__main__":

    prompt = "Please answer question based on provided context only " \
             "<context> " \
             "Taj Mahal is the name of Rajib's house. It is situated on the banks of river Howrah " \
             "<question> Where is Taj Mahal?  "
    response = "I am very confident that Taj Mahal is in America."

    openai_provider = OpenAI()
    response_openai = get_relevance(openai_provider, prompt, response)
    print("Open AI Response ", response_openai)
dosubot[bot] commented 5 months ago

To resolve the ModuleNotFoundError for the openai package, follow these targeted steps:

  1. Ensure openai is Installed: Run pip install "openai<2,>=1.1.1" in your terminal to install the required version of the openai package. If it's already installed, verify it by running pip list in the terminal to see if openai appears in the list.

  2. Activate Virtual Environment: If you're using a virtual environment, ensure it's activated before running your script. The openai package must be installed in the same environment your script is running in.

  3. Python Version Compatibility: Confirm that the Python version where openai is installed matches the version you're using to run your script. You mentioned using Python 3.11; make sure the openai package is installed under this version.

  4. Reinstall OpenAI Package: If the issue persists, try reinstalling the openai package with pip install --force-reinstall openai to fix any potential installation issues.

  5. Check Python Path: Print sys.path in your script to ensure the Python interpreter can locate the installed openai module. This checks if there's a path issue preventing the module from being found.

  6. New Virtual Environment: To rule out dependency conflicts, create a new virtual environment and install only the openai package there. This can help identify if the problem is due to conflicting dependencies.

  7. Verify Import Statement: Double-check your script for any typos in the import statement for the openai package to ensure it's correctly spelled.

By following these steps, you should be able to resolve the issue with the openai package not being found. If the problem continues, providing more details about your setup, such as how you installed the openai package and the exact error message, can help diagnose the issue further.

#### Details **Sources** - [Error in the file trulens_eval/feedback/provider/endpoint/openai.py](https://github.com/truera/trulens/discussions/805) - [AttributeError: module 'openai' has no attribute 'OpenAI'](https://github.com/truera/trulens/issues/794) - [trulens_eval/trulens_eval/feedback/provider/endpoint/openai.py](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/feedback/provider/endpoint/openai.py) - [.azure_pipelines/ci-eval-pr.yaml](https://github.com/truera/trulens/blob/main/.azure_pipelines/ci-eval-pr.yaml) - [trulens_eval/trulens_eval/feedback/provider/openai.py](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/feedback/provider/openai.py) - [trulens_eval/examples/expositional/frameworks/openai_assistants/openai_assistants_api.ipynb](https://github.com/truera/trulens/blob/main/trulens_eval/examples/expositional/frameworks/openai_assistants/openai_assistants_api.ipynb) - [trulens_eval/trulens_eval/utils/imports.py](https://github.com/truera/trulens/blob/main/trulens_eval/trulens_eval/utils/imports.py)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

beamaia commented 5 months ago

I'm having the same issue despite the fact that openai is installed and I can even import it in my script. I need to evaluate a few models so I was trying to debug trulens and ended up removing verification made by trulens_eval.utils.imports and this was enough for script to work. This isn't really the ideal workaround, I was in need due to work, I edited the lib in my venv directory.

I'm still trying to properly understand exactly what's happening. I'm using trulens_eval version 0.29.0 and I'm starting with trulens_eval.utils.imports class like OptionalImports and functions like format_import_errors.

rajib76 commented 5 months ago

I also removed the verification but now I am getting a new error missing 1 positional argument "name" , this is coming from openai.py

rajib76 commented 5 months ago

Traceback (most recent call last): File "/Users/joyeed/truelens/true_lens_examples/01_rag_triad_relevance.py", line 33, in openai_provider = OpenAI() ^^^^^^^^ File "/Users/joyeed/truelens/true_lens_examples/venv/lib/python3.11/site-packages/trulens_eval/feedback/provider/openai.py", line 75, in init self_kwargs['endpoint'] = OpenAIEndpoint( ^^^^^^^^^^^^^^^ TypeError: Dummy.new() missing 1 required positional argument: 'name'

joshreini1 commented 5 months ago

We are investigating this issue, the error seems to be related tolangchain-community.

As a temporary fix, this issue is resolved by installing langchain-community into your python environment. Please give this a try.

We'll let you know in this issue when a permanent fix is available

Abbass3434 commented 5 months ago

@joshreini1 Installing langchain-community did not work and gives the same error -

_ModuleNotFoundError: openai package is required for using OpenAI models. You should be able to install it with pip:

```bash
pip install "openai<2,>=1.1.1"_
ankitrs7 commented 5 months ago

I am also getting the same error , while running quick start guild for llama_index

tirumaraiselvan commented 5 months ago

FWIW, the fix mentioned in comment did work for me.

As a temporary fix, this issue is resolved by installing langchain-community into your python environment. Please give this a try.

DishaAnDS commented 5 months ago

We are investigating this issue, the error seems to be related tolangchain-community.

As a temporary fix, this issue is resolved by installing langchain-community into your python environment. Please give this a try.

We'll let you know in this issue when a permanent fix is available

This also works for me. You may need to add this to your notebook if you run from your databricks notebook after installing everything: dbutils.library.restartPython()

piotrm0 commented 5 months ago

Hi everyone still with this problem, can you post here the output of running pip list. I'm having trouble replicating in my env.

piotrm0 commented 5 months ago

Also, anyone here still have the problem after installing langchain_community ?

Abbass3434 commented 5 months ago

Also, anyone here still have the problem after installing langchain_community ?

I still have the same problem even after installing langchain_community. I am using trulens-eval==0.28.1 openai==1.14.1. This issue was only coming since few days as it used to work fine before. (FYI I am using trulens-eval with outside logs process )

Abbass3434 commented 5 months ago

Hi everyone still with this problem, can you post here the output of running pip list. I'm having trouble replicating in my env.

Package Version


aiohttp 3.9.5 aiosignal 1.3.1 alembic 1.13.1 altair 5.3.0 annotated-types 0.7.0 ansible 5.10.0 ansible-core 2.12.10 anyio 4.3.0 async-timeout 4.0.3 attrs 19.3.0 Automat 0.8.0 awscli 1.32.109 bcrypt 3.1.7 beautifulsoup4 4.12.3 blinker 1.4 boto 2.49.0 boto3 1.34.109 botocore 1.34.109 cachetools 5.3.3 certifi 2019.11.28 chardet 3.0.4 charset-normalizer 3.3.2 Click 7.0 cloud-init 24.1.3 colorama 0.4.3 command-not-found 0.3 configobj 5.0.6 constantly 15.1.0 contourpy 1.1.1 cryptography 2.8 cycler 0.12.1 dataclasses-json 0.6.6 dbus-python 1.2.16 dill 0.3.8 distro 1.9.0 distro-info 0.23+ubuntu1.1 dnspython 2.6.1 docutils 0.16 ec2-hibinit-agent 1.0.0 entrypoints 0.4 exceptiongroup 1.2.1 Faker 25.2.0 favicon 0.7.0 fonttools 4.51.0 frozendict 2.4.4 frozenlist 1.4.1 gitdb 4.0.11 GitPython 3.1.43 greenlet 3.0.3 h11 0.14.0 hibagent 1.0.1 htbuilder 0.6.2 httpcore 1.0.5 httplib2 0.14.0 httpx 0.27.0 humanize 4.9.0 hyperlink 19.0.0 idna 2.8 importlib_metadata 7.1.0 importlib_resources 6.4.0 incremental 16.10.1 Jinja2 2.10.1 jmespath 0.9.4 joblib 1.4.2 jsonpatch 1.33 jsonpointer 2.0 jsonschema 3.2.0 keyring 18.0.1 kiwisolver 1.4.5 langchain 0.2.0 langchain-community 0.2.0 langchain-core 0.2.0 langchain-text-splitters 0.2.0 langsmith 0.1.60 language-selector 0.1 launchpadlib 1.10.13 lazr.restfulclient 0.14.2 lazr.uri 1.0.3 lxml 5.2.2 Mako 1.3.5 Markdown 3.6 markdown-it-py 3.0.0 markdownlit 0.0.7 MarkupSafe 1.1.0 marshmallow 3.21.2 matplotlib 3.7.5 mdurl 0.1.2 merkle-json 1.0.0 millify 0.1.1 more-itertools 4.2.0 multidict 6.0.5 munch 4.0.0 mypy-extensions 1.0.0 nest-asyncio 1.6.0 netifaces 0.10.4 nltk 3.8.1 ntlm-auth 1.1.0 numpy 1.24.4 oauthlib 3.1.0 openai 1.14.1 orjson 3.10.3 packaging 23.2 pandas 2.0.3 paramiko 2.6.0 pexpect 4.6.0 pillow 10.3.0 pip 24.0 prometheus_client 0.20.0 protobuf 4.25.3 psutil 5.9.8 psycopg2-binary 2.9.9 pyarrow 16.1.0 pyasn1 0.4.2 pyasn1-modules 0.2.1 pydantic 2.7.1 pydantic_core 2.18.2 pydeck 0.9.1 Pygments 2.18.0 PyGObject 3.36.0 PyHamcrest 1.9.0 PyJWT 1.7.1 pykerberos 1.1.14 pymacaroons 0.13.0 pymdown-extensions 10.8.1 pymongo 4.7.2 PyNaCl 1.3.0 pyOpenSSL 19.0.0 pyparsing 2.4.6 pyrsistent 0.15.5 pyserial 3.4 python-apt 2.0.1+ubuntu0.20.4.1 python-dateutil 2.9.0.post0 python-debian 0.1.36+ubuntu1.1 python-decouple 3.8 python-dotenv 1.0.1 pytz 2024.1 pywinrm 0.3.0 PyYAML 5.3.1 regex 2024.5.15 requests 2.32.1 requests-kerberos 0.12.0 requests-ntlm 1.1.0 requests-unixsocket 0.2.0 resolvelib 0.5.4 rich 13.7.1 rsa 4.7.2 s3transfer 0.10.1 SecretStorage 2.3.1 service-identity 18.1.0 setuptools 45.2.0 simplejson 3.16.0 six 1.14.0 smmap 5.0.1 sniffio 1.3.1 sos 4.5.6 soupsieve 2.5 SQLAlchemy 2.0.30 ssh-import-id 5.10 st-annotated-text 4.0.1 streamlit 1.34.0 streamlit-aggrid 0.3.4 streamlit-camera-input-live 0.2.0 streamlit-card 1.0.2 streamlit-embedcode 0.1.2 streamlit-extras 0.4.2 streamlit-faker 0.0.3 streamlit-image-coordinates 0.1.6 streamlit-keyup 0.2.4 streamlit-pills 0.3.0 streamlit-toggle-switch 1.0.2 streamlit-vertical-slider 2.5.5 systemd-python 234 tenacity 8.3.0 toml 0.10.2 toolz 0.12.1 tornado 6.4 tqdm 4.66.4 trulens-eval 0.28.1 Twisted 18.9.0 typing_extensions 4.11.0 typing-inspect 0.9.0 tzdata 2024.1 ubuntu-pro-client 8001 ufw 0.36 unattended-upgrades 0.1 urllib3 1.25.8 validators 0.28.1 wadllib 1.3.3 watchdog 4.0.0 wheel 0.34.2 xmltodict 0.12.0 yarl 1.9.4 zipp 3.18.2 zope.interface 4.7.1

joshreini1 commented 5 months ago

Hey folks - for anyone still experiencing this issue, we've merged a possible fix. Please try installing from GitHub and let us know if the issue is resolved. We'll push this out in a release soon if successful.

pip uninstall trulens_eval -y # to remove existing PyPI version
pip install git+https://github.com/truera/trulens#subdirectory=trulens_eval
joshreini1 commented 5 months ago

This fix is released in 0.30.1 - please update and reopen if issues persist.

pip install -U trulens_eval

Thanks!