-
Most of the time I query in memory index. However, I am wondering if I store the same 'df_nodes' suggested in the below exapmple for the documents.
The way I create df_nodes are like below and my goal is store that df_nodes into vector database like above example:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
To store # Assuming df_nodes is a list of BaseNode objects
df_nodes = [...] # Your list of BaseNode objects
# Set up MongoDB connection
import pymongo
import os
import getpass
mongo_uri = getpass.getpass("MONGO_URI:")
if not mongo_uri:
print("MONGO_URI not set")
def get_mongo_client(mongo_uri):
"""Establish connection to the MongoDB."""
try:
client = pymongo.MongoClient(mongo_uri)
print("Connection to MongoDB successful")
return client
except pymongo.errors.ConnectionFailure as e:
print(f"Connection failed: {e}")
return None
mongo_client = get_mongo_client(mongo_uri)
DB_NAME = "your_database"
COLLECTION_NAME = "your_collection"
db = mongo_client[DB_NAME]
collection = db[COLLECTION_NAME]
# To ensure we are working with a fresh collection
collection.delete_many({})
# Initialize the MongoDBAtlasVectorSearch
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
vector_store = MongoDBAtlasVectorSearch(
mongo_client,
db_name=DB_NAME,
collection_name=COLLECTION_NAME,
index_name="vector_index",
)
# Add nodes to the vector store
vector_store.add(df_nodes)
print("Successfully added nodes to the vector store") This code initializes the |
Beta Was this translation helpful? Give feedback.
To retrieve the original pandas DataFrame from the DuckDB vector database, you can use the
duckdb
package to execute a query and fetch the results into a DataFrame. Here is an example of how you can do this:Replace
'path_to_your_database.duckdb'
with the path to your DuckDB database file. This code will connect to the DuckDB database, execute a query to select all data from the…