在Langchain中使用DocArrayInMemorySearch时出现错误:无法导入docarray Python包

这是完整的代码。在https://learn.deeplearning.ai/的笔记本上运行得很好。但当我在本地机器上运行时,出现了关于

ImportError: Could not import docarray python package

的错误。我尝试重新安装/强制安装了langchain和lanchain[docarray](使用pip和pip3)。我使用的是mini conda虚拟环境,Python版本为3.11.4

from langchain.vectorstores import DocArrayInMemorySearchfrom langchain.schema import Documentfrom langchain.indexes import VectorstoreIndexCreatorimport openaiimport osos.environ['OPENAI_API_KEY'] =  "xxxxxx" #在DLAI中不需要docs = [    Document(        page_content="""[{"API_Name":"get_invoice_transactions","API_Description":"这个API调用时会提供交易列表","API_Inputs":[],"API_Outputs":[]}]"""    ),    Document(        page_content="""[{"API_Name":"get_invoice_summary_year","API_Description":"这个API按供应商、产品和年份汇总发票","API_Inputs":[{"API_Input":"Year","API_Input_Type":"Text"}],"API_Outputs":[{"API_Output":"Purchase Volume","API_Output_Type":"Float"},{"API_Output":"Vendor Name","API_Output_Type":"Text"},{"API_Output":"Year","API_Output_Type":"Text"},{"API_Output":"Item","API_Output_Type":"Text"}]}]"""    ),    Document(        page_content="""[{"API_Name":"loan_payment","API_Description":"这个API计算贷款的月供","API_Inputs":[{"API_Input":"Loan_Amount","API_Input_Type":"Float"},{"API_Input":"Interest_Rate","API_Input_Type":"Float"},{"API_Input":"Loan_Term","API_Input_Type":"Integer"}],"API_Outputs":[{"API_Output":"Monthly_Payment","API_Output_Type":"Float"},{"API_Output":"Total_Interest","API_Output_Type":"Float"}]}]"""    ),    Document(        page_content="""[{"API_Name":"image_processing","API_Description":"这个API处理图像并应用指定的滤镜","API_Inputs":[{"API_Input":"Image_URL","API_Input_Type":"URL"},{"API_Input":"Filters","API_Input_Type":"List"}],"API_Outputs":[{"API_Output":"Processed_Image_URL","API_Output_Type":"URL"}]}]"""    ),    Document(        page_content="""[{"API_Name":"movies_catalog","API_Description":"这个API根据用户偏好提供电影目录","API_Inputs":[{"API_Input":"Genre","API_Input_Type":"Text"},{"API_Input":"Release_Year","API_Input_Type":"Integer"}],"API_Outputs":[{"API_Output":"Movie_Title","API_Output_Type":"Text"},{"API_Output":"Genre","API_Output_Type":"Text"},{"API_Output":"Release_Year","API_Output_Type":"Integer"},{"API_Output":"Rating","API_Output_Type":"Float"}]}]"""    ),    # 在这里添加更多文档   ]index = VectorstoreIndexCreator(        vectorstore_cls=DocArrayInMemorySearch        ).from_documents(docs)api_desc = "对电影进行分析"query = f"根据以下API描述搜索相关API:{api_desc}\        以JSON对象的形式返回API的page_contents列表。"print(index.query(query)) 

这是错误信息:

(streamlit) C02Z8202LVDQ:sage_response praneeth.gadam$ /Users/praneeth.gadam/opt/miniconda3/envs/streamlit/bin/python /Users/praneeth.gadam/sage_response/docsearch_copy.py Traceback (most recent call last):   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/vectorstores/docarray/base.py", line 19, in _check_docarray_import    import docarray ModuleNotFoundError: No module named 'docarray'During handling of the above exception, another exception occurred:Traceback (most recent call last):   File "/Users/praneeth.gadam/sage_response/docsearch_copy.py", line 30, in <module>    ).from_documents(docs)      ^^^^^^^^^^^^^^^^^^^^   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/indexes/vectorstore.py", line 88, in from_documents    vectorstore = self.vectorstore_cls.from_documents(                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/vectorstores/base.py", line 420, in from_documents    return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/vectorstores/docarray/in_memory.py", line 67, in from_texts    store = cls.from_params(embedding, **kwargs)            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/vectorstores/docarray/in_memory.py", line 38, in from_params    _check_docarray_import()   File "/Users/praneeth.gadam/opt/miniconda3/envs/streamlit/lib/python3.11/site-packages/langchain/vectorstores/docarray/base.py", line 29, in _check_docarray_import    raise ImportError( ImportError: Could not import docarray python package. Please install it with `pip install "langchain[docarray]"`.

回答:

尝试这样安装:pip install docarray

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注