Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -397,6 +397,43 @@ def create_documents(data_source: str, data: List[dict]) -> List[Document]:
|
|
397 |
|
398 |
import faiss
|
399 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
# Custom FAISS wrapper (optional, if you still want it)
|
401 |
class MyVector_Store:
|
402 |
def __init__(self, index: faiss.Index):
|
|
|
397 |
|
398 |
import faiss
|
399 |
|
400 |
+
import os
|
401 |
+
import numpy as np
|
402 |
+
import faiss
|
403 |
+
|
404 |
+
# Step 1: Define the path where the index will be saved
|
405 |
+
index_dir = "/home/wendy/Downloads/faiss_index"
|
406 |
+
os.makedirs(index_dir, exist_ok=True) # Create the directory if it doesn't exist
|
407 |
+
index_file_path = os.path.join(index_dir, "index.faiss")
|
408 |
+
|
409 |
+
# Step 2: Generate random data (e.g., 1000 vectors of dimension 128)
|
410 |
+
d = 128 # Vector dimensionality
|
411 |
+
n = 1000 # Number of vectors
|
412 |
+
np.random.seed(42) # For reproducibility
|
413 |
+
|
414 |
+
# Generate random vectors (uniform distribution)
|
415 |
+
xb = np.random.random((n, d)).astype('float32')
|
416 |
+
|
417 |
+
# Step 3: Create the FAISS index
|
418 |
+
# Using the L2 distance metric (Euclidean distance)
|
419 |
+
index = faiss.IndexFlatL2(d)
|
420 |
+
|
421 |
+
# Step 4: Add the vectors to the index
|
422 |
+
index.add(xb)
|
423 |
+
|
424 |
+
# Step 5: Save the index to the specified path
|
425 |
+
faiss.write_index(index, index_file_path)
|
426 |
+
|
427 |
+
print(f"FAISS index saved to: {index_file_path}")
|
428 |
+
|
429 |
+
# Step 6: Load the FAISS index from the file
|
430 |
+
loaded_index = faiss.read_index(index_file_path)
|
431 |
+
|
432 |
+
print("FAISS index loaded successfully from:", index_file_path)
|
433 |
+
|
434 |
+
|
435 |
+
|
436 |
+
|
437 |
# Custom FAISS wrapper (optional, if you still want it)
|
438 |
class MyVector_Store:
|
439 |
def __init__(self, index: faiss.Index):
|