create_collection()收到了一个意外的关键字参数’embedding_fn’。

huangapple go评论56阅读模式
英文:

create_collection() got an unexpected keyword argument 'embedding_fn'

问题

我在尝试使用 langchain 库创建一个问答系统。但是当我尝试使用 chromadb 库在文档中进行搜索时,它显示以下错误:

> TypeError: create_collection() 收到一个意外的关键字参数 'embedding_fn'

这是我正在处理的代码:

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader
from langchain.vectorstores import Chroma

loader = TextLoader('./info.txt')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings).

最后一行生成了错误。

英文:

I was trying to use the langchain library to create a question answering system. But when I try to search in the document using the chromadb library it gives this error:

> TypeError: create_collection() got an unexpected keyword argument 'embedding_fn'

Here's the code am working on

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader
from langchain.vectorstores import Chroma

loader = TextLoader('./info.txt')
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings).

The last line generates the error.

This is the complete error message:

TypeError                                 Traceback (most recent call last)
Input In [36], in <cell line: 1>()
----> 1 docsearch = Chroma.from_documents(texts, embeddings)

File ~\anaconda3\lib\site-packages\langchain\vectorstores\chroma.py:212, in Chroma.from_documents(cls, documents, embedding, ids, collection_name, persist_directory, **kwargs)
    210 texts = [doc.page_content for doc in documents]
    211 metadatas = [doc.metadata for doc in documents]
--> 212 return cls.from_texts(
    213     texts=texts,
    214     embedding=embedding,
    215     metadatas=metadatas,
    216     ids=ids,
    217     collection_name=collection_name,
    218     persist_directory=persist_directory,
    219 )

File ~\anaconda3\lib\site-packages\langchain\vectorstores\chroma.py:178, in Chroma.from_texts(cls, texts, embedding, metadatas, ids, collection_name, persist_directory, **kwargs)
    151 @classmethod
    152 def from_texts(
    153     cls,
   (...)
    160     **kwargs: Any,
    161 ) -> Chroma:
    162     """Create a Chroma vectorstore from a raw documents.
    163 
    164     If a persist_directory is specified, the collection will be persisted there.
   (...)
    176         Chroma: Chroma vectorstore.
    177     """
--> 178     chroma_collection = cls(
    179         collection_name=collection_name,
    180         embedding_function=embedding,
    181         persist_directory=persist_directory,
    182     )
    183     chroma_collection.add_texts(texts=texts, metadatas=metadatas, ids=ids)
    184     return chroma_collection

File ~\anaconda3\lib\site-packages\langchain\vectorstores\chroma.py:65, in Chroma.__init__(self, collection_name, embedding_function, persist_directory)
     60     logger.warning(
     61         f"Collection {collection_name} already exists,"
     62         " Do you have the right embedding function?"
     63     )
     64 else:
---> 65     self._collection = self._client.create_collection(
     66         name=collection_name,
     67         embedding_fn=self._embedding_function.embed_documents
     68         if self._embedding_function is not None
     69         else None,
     70     )

TypeError: create_collection() got an unexpected keyword argument 'embedding_fn'

答案1

得分: 1

chromadb.Clientcreate_collection方法在2天前更改,embedding_fn参数已重命名为embedding_function

https://github.com/chroma-core/chroma/commit/6ce2388e219d47048e854be72be54617df647224

截止到版本0.0.87的langchain.vectorstores.chroma.Chroma类的源代码似乎已在您提出问题的3小时前进行了更新,以匹配chromadb库:

https://github.com/hwchase17/langchain/commit/34cba2da3264ccc9100f7efd16807c8d2a51734c

因此,您可以通过安装LangChain的最新版本来解决这个问题。

英文:

The create_collection method of chromadb.Client was changed 2 days ago and the embedding_fn parameter was renamed to embedding_function:

https://github.com/chroma-core/chroma/commit/6ce2388e219d47048e854be72be54617df647224

The source code for the langchain.vectorstores.chroma.Chroma class as of version 0.0.87 seems to have been updated already (3 hours before you asked the question) to match the chromadb library:

https://github.com/hwchase17/langchain/commit/34cba2da3264ccc9100f7efd16807c8d2a51734c

So you should be able to fix the problem by installing the newest version of LangChain.

huangapple
  • 本文由 发表于 2023年2月16日 17:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定