这是完整的代码。在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