pythonopenai-apipython-packaginglangchain

Error using Using DocArrayInMemorySearch in Langchain: Could not import docarray python package


Here is the full code. It runs perfectly fine on https://learn.deeplearning.ai/ notebook. But when I run it on my local machine, I get an error about

ImportError: Could not import docarray python package

I have tried reinstalling/force installing langchain and lanchain[docarray] (both pip and pip3). I use mini conda virtual environment. python version 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))
 

Here is the error:

(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]"`.

Solution

  • Try to install this way: pip install docarray