Skip to content

Not being able to pickle Llamacpp Embedding  #2429

Description

@adriacabeza

👋🏾 I tried using the new Llamacpp LLM and Embedding classes (awesome work!) and I've noticed that any vector store created with it cannot be saved as a pickle as there are cytpe objects containing pointers.

The reason is because the Vector Store has the field embedding_function which is the Callable to
the llama.cpp embedding wrapper and that contains pointers that cannot be pickled. Reproducible code:

from langchain.embeddings import LlamaCppEmbeddings
from langchain.vectorstores import FAISS
import pickle 

llama = LlamaCppEmbeddings(model_path=MODEL_PATH)
store = FAISS.from_texts(
        ["test"],
        llama,
    )
faiss.write_index(store.index, "docs.index")
store.index = None
with open("faiss_store.pkl", "wb") as f:
    pickle.dump(store, f)

which raises a ValueError:

ValueError: ctypes objects containing pointers cannot be pickled

In the case that the save_local method from the FAISS vector store is used, the index cannot be stored due to the same serialisation issue:

RuntimeError: Error in void faiss::write_index(const faiss::Index *, faiss::IOWriter *) at /Users/runner/work/faiss-wheels/faiss-wheels/faiss/faiss/impl/index_write.cpp:822: don't know how to serialize this type of index

I also tried with other libraries like cloudpickle or dill and none worked so I believe that the solution here it to convert the ctypes objects to a format that can be serialised. Do you have any recommended way to store it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions