-
I am using llama_parse and creating recursive_index on fly. Is it possible to store vectors after parse? Code Example taken from : https://github.com/run-llama/llama_parse/blob/main/examples/demo_insurance.ipynb Here my objective is store the vector index locally either in Faiss or ChromaDB and call the stored index and run the query_1 without parsing again to save cost. Appreciate some code samples to store vectors and retrieve vectors for query.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, it is possible to save a FaissStoring Vectorsfrom llama_index.legacy.vector_stores.faiss import FaissVectorStore
import faiss
import numpy as np
from llama_index.legacy.schema import BaseNode
# Create a faiss index
d = 1536 # dimension
faiss_index = faiss.IndexFlatL2(d)
# Initialize the vector store
vector_store = FaissVectorStore(faiss_index=faiss_index)
# Add vectors to the store
vectors = [np.random.rand(d).astype('float32') for _ in range(10)]
nodes = [BaseNode(embedding=vector) for vector in vectors]
vector_store.add(nodes)
# Persist the vector store to disk
persist_path = 'faiss_index.bin'
vector_store.persist(persist_path) Retrieving Vectorsfrom llama_index.legacy.vector_stores.faiss import FaissVectorStore
import numpy as np
from llama_index.legacy.vector_stores.types import VectorStoreQuery
# Load the vector store from disk
persist_path = 'faiss_index.bin'
vector_store = FaissVectorStore.from_persist_path(persist_path)
# Query the vector store
query_vector = np.random.rand(1536).astype('float32')
query = VectorStoreQuery(query_embedding=query_vector.tolist(), similarity_top_k=5)
result = vector_store.query(query)
print(result.ids, result.similarities) ChromaDBStoring Vectorsimport chromadb
from chromadb.config import Settings
import numpy as np
# Initialize ChromaDB client
client = chromadb.Client(Settings(chroma_db_impl="duckdb+parquet", persist_directory="./chromadb"))
# Create a collection
collection = client.create_collection("example_collection")
# Add vectors to the collection
vectors = [list(np.random.rand(1536).astype('float32')) for _ in range(10)]
ids = [str(i) for i in range(10)]
collection.add(embeddings=vectors, ids=ids) Retrieving Vectorsimport chromadb
from chromadb.config import Settings
import numpy as np
# Initialize ChromaDB client
client = chromadb.Client(Settings(chroma_db_impl="duckdb+parquet", persist_directory="./chromadb"))
# Get the collection
collection = client.get_collection("example_collection")
# Query the collection
query_vector = list(np.random.rand(1536).astype('float32'))
results = collection.query(query_embeddings=[query_vector], n_results=5)
print(results) These code samples demonstrate how to store and retrieve vectors using both Faiss and ChromaDB [1][2]. |
Beta Was this translation helpful? Give feedback.
Yes, it is possible to save a
Recursive_index
in a Faiss Vector store locally. Below are detailed code samples for both Faiss and ChromaDB to store and retrieve vectors for querying.Faiss
Storing Vectors