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

问题描述 投票:0回答:1

这是完整的代码。它在 https://learn.deeplearning.ai/ 笔记本上运行得非常好。但是当我在本地计算机上运行它时,我收到一个错误:

导入错误:无法导入 docarray python 包

我尝试重新安装/强制安装 langchain 和 lanchain[docarray] (pip 和 pip3)。我使用迷你 conda 虚拟环境。 python版本3.11.4

from langchain.vectorstores import DocArrayInMemorySearch
from langchain.schema import Document
from langchain.indexes import VectorstoreIndexCreator
import openai
import os

os.environ['OPENAI_API_KEY'] =  "xxxxxx" #not needed in DLAI

docs = [
    Document(
        page_content="""[{"API_Name":"get_invoice_transactions","API_Description":"This API when called will provide the list of transactions","API_Inputs":[],"API_Outputs":[]}]"""
    ),
    Document(
        page_content="""[{"API_Name":"get_invoice_summary_year","API_Description":"this api summarizes the invoices by vendor, product and year","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":"This API calculates the monthly payment for a loan","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":"This API processes an image and applies specified filters","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":"This API provides a catalog of movies based on user preferences","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"}]}]"""
    ),
    # Add more documents here   
]

index = VectorstoreIndexCreator(
        vectorstore_cls=DocArrayInMemorySearch
        ).from_documents(docs)

api_desc = "do analytics about movies"
query = f"Search for related APIs based on following API Description: {api_desc}\
        Return list of API page_contents as JSON objects."


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]"`.
python openai-api python-packaging langchain
1个回答
0
投票

尝试这样安装:pip install docarray

© www.soinside.com 2019 - 2024. All rights reserved.