langchain-ai / langchain

πŸ¦œπŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.58k stars 15.3k forks source link

AttributeError: 'Chroma' object has no attribute '_client_settings' #7482

Closed ecerulm closed 1 year ago

ecerulm commented 1 year ago

System Info

on Python 3.10.10

with requirements.txt

pandas==2.0.1
beautifulsoup4==4.12.2
langchain==0.0.229
chromadb==0.3.26
tiktoken==0.4.0
gradio==3.36.1
Flask==2.3.2
torch==2.0.1
sentence-transformers==2.2.2

Who can help?

@hwchase17

Information

Related Components

Reproduction

I'm getting AttributeError: 'Chroma' object has no attribute '_client_settings' when running

from langchain.vectorstores import Chroma
import chromadb
from chromadb.config import Settings
from langchain.embeddings import HuggingFaceEmbeddings
from constants.model_constants import HF_EMBEDDING_MODEL

chroma_client = chromadb.Client(Settings(chroma_api_impl="rest", chroma_server_host="xxxxx", chroma_server_http_port="443", chroma_server_ssl_enabled=True))

embedder = HuggingFaceEmbeddings(
                model_name=HF_EMBEDDING_MODEL,
                model_kwargs={"device": "cpu"},
                encode_kwargs={'normalize_embeddings': False}
            )

chroma_vector_store = Chroma(
            collection_name="test",
            embedding_function=embedder,
            client=chroma_client)

the traceback is

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rubelagu/.pyenv/versions/3.10.10/envs/xxxxTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rubelagu/.pyenv/versions/3.10.10/envs/oraklet_chatbot/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 94, in __init__
    self._client_settings.persist_directory or persist_directory
AttributeError: 'Chroma' object has no attribute '_client_settings'/lib/python3.10/site-packages/langchain/vectorstores/chroma.py", line 94, in __init__
    self._client_settings.persist_directory or persist_directory
AttributeError: 'Chroma' object has no attribute '_client_settings'

Expected behavior

It should not raise an exception,

It seems to me that https://github.com/hwchase17/langchain/blob/5eec74d9a5435c671382e69412072a8725b2ec60/langchain/vectorstores/chroma.py#L93-L95

was introduced by commit https://github.com/hwchase17/langchain/commit/a2830e3056e4e616160b150bf5ea212a97df2dc4 from @nb-programmer and @rlancemartin

that commit assumes that self._client_settings exists always when in reality that won't be created if a client is passed

dosubot[bot] commented 1 year ago

Answer generated by a πŸ€–

Answer

I understand that you're encountering an AttributeError when trying to initialize a Chroma object with a pre-existing client. This error is happening because the _client_settings attribute is only set when a client is not provided. If a client is provided, _client_settings is not set, which leads to the AttributeError when it is accessed later.

To address this issue, you can modify the __init__ method of the Chroma class to ensure the _client_settings attribute is always set. Here's how you can do it:

def __init__(
    self,
    collection_name: str = _LANGCHAIN_DEFAULT_COLLECTION_NAME,
    embedding_function: Optional[Embeddings] = None,
    persist_directory: Optional[str] = None,
    client_settings: Optional[chromadb.config.Settings] = None,
    collection_metadata: Optional[Dict] = None,
    client: Optional[chromadb.Client] = None,
) -> None:
    """Initialize with Chroma client."""
    try:
        import chromadb
        import chromadb.config
    except ImportError:
        raise ValueError(
            "Could not import chromadb python package. "
            "Please install it with `pip install chromadb`."
        )

    if client is not None:
        self._client = client
        self._client_settings = client.settings  # Assuming the client object has a settings attribute
    else:
        if client_settings:
            self._client_settings = client_settings
        else:
            self._client_settings = chromadb.config.Settings()
            if persist_directory is not None:
                self._client_settings = chromadb.config.Settings(
                    chroma_db_impl="duckdb+parquet",
                    persist_directory=persist_directory,
                )
        self._client = chromadb.Client(self._client_settings)

This solution assumes that the client object has a settings attribute. If it doesn't, you'll need to adjust the code accordingly.

Please note that this is one potential solution based on the information provided. If this doesn't resolve your issue or if your experience contradicts this solution, I recommend checking if you're using the latest version of the software as the issue might have been addressed in a more recent update.

If you believe this is a bug that could impact other users and you're comfortable with the proposed solution, I encourage you to make a pull request with this change. Your contribution could help improve the experience for others using this software.

Sources


This response is meant to be useful, save you time, and share context. 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.

ecerulm commented 1 year ago

The workaround is to downgrade to langchain==0.0.226

huineng commented 1 year ago

Can this be merged ? I'm waiting on this fix. Thanks ps i'm solving it for the moment using

Chroma._client_settings= chromadb.config.Settings
Chroma._client_settings.persist_directory = None
ecerulm commented 1 year ago

7639 solved this issue