-
I have json file and I like to read, embed, store in to FAISS vector db. Hoever, I am getting error. Please help. Code I am using...
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
To convert a JSON file to nodes and store it in a FAISS vector database, you can follow these steps. Here's a corrected version of your code:
Here's the complete code: import json
import faiss
import numpy as np
from llama_index.core import Document, VectorStoreIndex
from llama_index.core.node_parser.text.semantic_splitter import SemanticSplitterNodeParser
from llama_index.embeddings.openai import OpenAIEmbedding # or your chosen embedding model
# Load JSON file
file_path = "c:/parquet/json/data.json"
with open(file_path, 'r') as f:
data = json.load(f)
# Convert JSON data to a list of Document objects
docs = [Document(text=json.dumps(item)) for item in data["Data"]]
# Initialize the embedding model
embed_model = OpenAIEmbedding() # Replace with your actual embedding model
# Initialize the SemanticSplitterNodeParser
splitter = SemanticSplitterNodeParser(
buffer_size=1,
breakpoint_percentile_threshold=95,
embed_model=embed_model
)
# Split documents into nodes
nodes = splitter.get_nodes_from_documents(docs)
# Convert nodes to embeddings
embeddings = [node.get_embedding() for node in nodes]
embeddings = np.array(embeddings, dtype='float32')
# Initialize FAISS index
dimension = embeddings.shape[1]
faiss_index = faiss.IndexFlatL2(dimension)
faiss_index.add(embeddings)
# Save the FAISS index to disk
faiss.write_index(faiss_index, 'faiss_index.bin')
print("FAISS index created and saved successfully.") Key Points:
This should help you convert your JSON data to nodes and store them in a FAISS vector database without errors [1][2]. |
Beta Was this translation helpful? Give feedback.
-
@dosu I am getting Value error "ValueError: embedding not set." Also I like store FAISS vector databse locally or presisit local drive like "c:/faiss".... please help here. |
Beta Was this translation helpful? Give feedback.
To resolve the "ValueError: embedding not set" error and store the FAISS vector database locally, you need to ensure that the embeddings are properly set before adding them to the FAISS index. Additionally, you can use the
persist
method to save the FAISS index to a specified path.Here's an updated version of your code: