### Start Attu UI in Docker Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Command to start the Attu UI, a graphical interface for Milvus, within a Docker network. Ensure Docker is installed and running. This command assumes a docker-compose.yml file is present in the current directory. ```bash docker compose up -d attu ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Install all Python dependencies required for the project, including the backend and benchmarking tools. This command reads dependencies from the 'requirements.txt' file. ```sh pip install -r requirements.txt ``` -------------------------------- ### Start Local Databases with Docker Compose (Shell) Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Starts local database services defined in a Docker Compose file. This command is used to set up the necessary database instances for benchmarking. Requires Docker and Docker Compose to be installed and running. ```sh docker compose -f scripts/docker-compose.yml up -d ``` -------------------------------- ### Install Project Dependencies (Shell) Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Installs project dependencies using pip within a virtual environment. It first creates a virtual environment named '.venv' and then activates it before installing packages from 'requirements.txt'. Ensure Python and pip are installed. ```sh python -m venv .venv && source .venv/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Run UI Backend Server Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Start the FastAPI backend server for the vector database comparison UI. This server handles requests for live search and latency measurements. Requires 'uvicorn' and a '.env' file for configuration. ```sh uvicorn ui.backend.server:app --reload --port 8000 ``` -------------------------------- ### Docker Compose: Deploy and Manage Vector Databases Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This snippet demonstrates how to deploy, manage, and view logs for multiple vector databases using Docker Compose. It covers starting, stopping, and accessing the database UIs. ```bash # Start all databases cd scripts docker compose up -d # Access database UIs # Qdrant: http://localhost:6333/dashboard # Milvus (Attu): http://localhost:3000 # Weaviate: http://localhost:8080/v1/meta # ChromaDB: http://localhost:8001 # Check running services docker compose ps # View logs docker compose logs qdrant docker compose logs milvus # Stop all services docker compose down # Stop and remove volumes (clean state) docker compose down -v ``` -------------------------------- ### Python: Load and Access Environment Variables Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This Python script demonstrates how to load environment variables from a .env file using the `dotenv` library and access them using `os.getenv`. It shows examples of retrieving database connection details and API keys. ```python from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Access configuration qdrant_url = os.getenv("QDRANT_URL", "http://localhost:6333") milvus_host = os.getenv("MILVUS_HOST", "localhost") openai_key = os.getenv("OPENAI_API_KEY") print(f"Qdrant URL: {qdrant_url}") print(f"Milvus Host: {milvus_host}") print(f"OpenAI configured: {bool(openai_key)}") ``` -------------------------------- ### YAML: Define Query Configurations Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This YAML file defines a list of test queries, each with associated expected tags and genres. This structure is used to guide the benchmarking process and evaluate search relevance. ```yaml # queries.yaml queries: - text: "aggressive rap song" expected: tags: ["aggressive"] genres: ["rap", "hip-hop"] - text: "calm and romantic ballad" expected: tags: ["romantic", "calm"] genres: ["pop", "ballad"] - text: "high energy metal track" expected: tags: ["energetic"] genres: ["metal"] ``` -------------------------------- ### Real-time Semantic Search with FastAPI Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Provides a FastAPI server for real-time semantic search, capable of querying multiple vector databases simultaneously. It includes an example of sending a search request to the server and processing the responses from different databases, displaying latency and search results. ```python from fastapi import FastAPI from ui.backend.server import SearchRequest, SearchResponse import requests # Start server: uvicorn ui.backend.server:app --reload --port 8000 # Query multiple databases simultaneously search_request = { "query": "uplifting pop anthem", "topk": 5, "dbs": ["qdrant", "milvus", "weaviate", "chroma"], "model": "sentence-transformers/all-MiniLM-L6-v2" } response = requests.post( "http://localhost:8000/search", json=search_request ) results = response.json() print(f"Query: {results['query']}") for db_name, db_result in results['by_db'].items(): if db_result['ok']: print(f"\n{db_name.upper()}:") print(f" Latency: {db_result['latency_ms']:.2f}ms") for i, result in enumerate(db_result['results'][:3], 1): print(f" {i}. {result['track']} - {result['artist']}") print(f" Score: {result['score']:.4f}, Genre: {result['genre']}") else: print(f"\n{db_name.upper()}: ERROR - {db_result['error']}") ``` -------------------------------- ### Milvus Client for High-Speed Vector Search Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Provides a client for the Milvus vector database, designed for high-speed similarity search using its gRPC interface and HNSW indexing. The client supports collection setup, batch upserting of vectors with metadata, and performing similarity searches. It connects to a Milvus instance via host and port. ```python from databases.milvus_client import Milvus import numpy as np # Initialize Milvus client milvus = Milvus(host="localhost", port="19530", collection="music") # Setup collection milvus.setup(dim=384) # Prepare data vectors = np.random.rand(5000, 384).tolist() payloads = [ { "row_id": i, "track": f"Track {i}", "artist": f"Artist {i}", "genre": "rock" if i % 2 == 0 else "jazz", "seeds": "['calm', 'romantic']", "text": f"Track {i} description" } for i in range(5000) ] # Batch insert with automatic indexing milvus.upsert(vectors=vectors, payloads=payloads) # Perform similarity search query = np.random.rand(384).tolist() results = milvus.search(query=query, top_k=5) for r in results: print(f"ID: {r['id']}, Score: {r['score']:.4f}") print(f"Track: {r['payload']['track']}, Genre: {r['payload']['genre']}") ``` -------------------------------- ### Define Vector Database Base Interface Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Defines the abstract base interface for all vector database clients. It specifies methods for setup, upserting vectors, searching, and teardown, ensuring a consistent API across different database implementations. This interface is crucial for abstracting database-specific logic. ```python from databases.base import VectorDB from typing import List, Dict, Any class CustomVectorDB(VectorDB): def setup(self, dim: int): """Initialize the database with specified vector dimension""" pass def upsert(self, vectors: List[List[float]], payloads: List[Dict[str, Any]]): """Insert or update vectors with associated metadata""" pass def search(self, query: List[float], top_k: int) -> List[Dict[str, Any]]: """Search for top-k most similar vectors""" pass def teardown(self): """Clean up database resources""" pass def close(self): """Close database connections""" pass ``` -------------------------------- ### Python: Load Query Configurations Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This Python script demonstrates how to programmatically load and parse the query configurations defined in a YAML file. It iterates through the queries and prints their text and expected labels. ```python import yaml # Load queries programmatically with open("queries.yaml", "r") as f: config = yaml.safe_load(f) for query in config["queries"]: print(f"Query: {query['text']}") print(f"Expected tags: {query['expected']['tags']}") print(f"Expected genres: {query['expected']['genres']}") ``` -------------------------------- ### Run Vector DB Benchmark CLI Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Execute the main benchmarking script with specified CSV, embeddings, databases, and parameters. Supports configuration of top-k values, repetitions, concurrency, and teardown options. Results are saved in the 'results/' directory. ```sh python benchmark.py --csv data/muse.csv --embeddings data/embeddings.parquet --dbs qdrant milvus weaviate chroma pinecone sqlite topk --topk 10 --repetitions 15 [--teardown_after_benchmark] # TopK included in example above ``` -------------------------------- ### Environment Configuration (.env) Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This file defines environment variables for configuring database connections, API keys for cloud services, and UI settings. It's used to manage settings for different vector databases and external services. ```bash # .env file QDRANT_URL=http://localhost:6333 MILVUS_HOST=localhost MILVUS_PORT=19530 WEAVIATE_URL=http://localhost:8080 CHROMA_HOST=localhost CHROMA_PORT=8001 SQLITE_DB_PATH=music_vectors.db # Cloud services PINECONE_API_KEY=your-pinecone-api-key PINECONE_ENVIRONMENT=us-west1-gcp TOPK_API_KEY=your-topk-api-key OPENAI_API_KEY=your-openai-api-key # UI settings UI_MODEL=sentence-transformers/all-MiniLM-L6-v2 UI_WARMUP_DBS=qdrant,milvus,weaviate ``` -------------------------------- ### Run Vector DB Benchmark with Top-K Sweep CLI Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Execute the main benchmarking script and perform a sweep across multiple 'topk' values. This is more efficient than running the script multiple times for different top-k configurations. Results are saved in the 'results/' directory. ```sh python benchmark.py --csv data/muse.csv --embeddings data/embeddings.parquet --dbs qdrant milvus weaviate chroma pinecone sqlite topk --repetitions 15 --topk_sweep 5 10 15 20 25 50 # TopK included in example above ``` -------------------------------- ### Qdrant Client for Vector Search Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Implements a client for the Qdrant vector database, utilizing HNSW indexing for high-performance similarity search. It includes functionalities to initialize the client, set up a collection with a specified vector dimension, upsert vectors with associated payloads, and perform similarity searches. The client connects to a Qdrant instance running locally. ```python from databases.qdrant_client import Qdrant import numpy as np # Initialize Qdrant client qdrant = Qdrant(url="http://localhost:6333", collection="music") # Setup collection with 384-dimensional vectors qdrant.setup(dim=384) # Upsert sample music vectors vectors = np.random.rand(1000, 384).tolist() payloads = [ { "track": f"Song {i}", "artist": f"Artist {i % 100}", "genre": "pop", "seeds": "['energetic', 'happy']", "text": f"Song {i} by Artist {i % 100}", "row_id": i } for i in range(1000) ] qdrant.upsert(vectors=vectors, payloads=payloads) # Search for similar tracks query_vector = np.random.rand(384).tolist() results = qdrant.search(query=query_vector, top_k=10) for result in results: print(f"Score: {result['score']:.4f}") print(f"Track: {result['payload']['track']}") print(f"Artist: {result['payload']['artist']}") ``` -------------------------------- ### View Docker Compose Service Logs Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Retrieves logs for a specific service defined in a docker-compose.yml file. Replace 'SERVICE_NAME' with the name of the service (e.g., 'milvus', 'attu'). Essential for debugging startup issues. ```bash docker compose logs SERVICE_NAME ``` -------------------------------- ### Execute Vector Database Benchmarks Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Runs comprehensive benchmarks across multiple vector databases. This can be done via command line, specifying databases, top-k values, and repetitions, or programmatically by loading embeddings, setting up a database instance, and performing search operations. It measures latency and queries per second. ```bash # Basic benchmark with Qdrant and Milvus python benchmark.py \ --csv data/muse.csv \ --embeddings data/embeddings.parquet \ --dbs qdrant milvus \ --topk 10 \ --repetitions 15 # Advanced: Top-k sweep across multiple values python benchmark.py \ --csv data/muse.csv \ --embeddings data/embeddings.parquet \ --dbs qdrant milvus weaviate chroma pinecone sqlite topk \ --topk_sweep 5 10 15 20 50 \ --repetitions 20 \ --concurrency 4 \ --teardown_after_benchmark ``` ```python # Programmatic benchmark execution from benchmark import get_db, load_embeddings, embed_query from sentence_transformers import SentenceTransformer import time # Load data vectors, payloads = load_embeddings("data/embeddings.parquet") print(f"Loaded {len(vectors)} vectors, dimension: {vectors.shape[1]}") # Initialize database db = get_db("qdrant", None) db.setup(dim=vectors.shape[1]) db.upsert(vectors=vectors.tolist(), payloads=payloads) # Load query model model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2") # Benchmark query performance query_text = "aggressive rap song" query_vec = embed_query(query_text, model) latencies = [] for _ in range(100): start = time.time() results = db.search(query_vec, top_k=10) latencies.append(time.time() - start) print(f"Avg latency: {sum(latencies)/len(latencies)*1000:.2f}ms") print(f"P95 latency: {sorted(latencies)[94]*1000:.2f}ms") print(f"QPS: {1.0 / (sum(latencies)/len(latencies)):.2f}") ``` -------------------------------- ### Run Vector Database Benchmark (Python) Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Executes the vector database benchmark comparing multiple databases. It requires specifying the dataset, embeddings, list of databases to test, desired top-k values, and the number of repetitions for reliable metrics. All CLI flags can be viewed with --help. ```python python benchmark.py --csv data/muse.csv --embeddings data/embeddings.parquet --dbs qdrant milvus weaviate chroma pinecone sqlite topk --topk 10 --repetitions 15 # For TopK: add 'topk' to --dbs list # See all CLI flags with: python benchmark.py --help ``` -------------------------------- ### Check Running Docker Containers Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Lists all currently running Docker containers. This is useful for identifying container names and ports, especially when troubleshooting network or port conflicts. ```bash docker ps ``` -------------------------------- ### Weaviate Client for Feature-Rich Vector Search Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt A client implementation for Weaviate, a vector database offering GraphQL and REST APIs. This client facilitates setting up the database schema, inserting vectors with associated metadata, and performing similarity searches. It demonstrates connecting to a local Weaviate instance and includes a method to close the database connection. ```python from databases.weaviate_client import WeaviateDB import numpy as np # Initialize Weaviate weaviate_db = WeaviateDB(url="http://localhost:8080", class_name="Track") # Setup schema weaviate_db.setup(dim=384) # Insert music vectors vectors = np.random.rand(2000, 384).tolist() payloads = [ { "row_id": i, "track": f"Song {i}", "artist": f"Band {i % 50}", "genre": "electronic", "seeds": "['uplifting', 'energetic']", "text": f"Electronic song {i}" } for i in range(2000) ] weaviate_db.upsert(vectors=vectors, payloads=payloads) # Query similar tracks query_vec = np.random.rand(384).tolist() matches = weaviate_db.search(query=query_vec, top_k=10) for match in matches: print(f"Distance: {match['score']:.4f}") print(f"Track: {match['payload']['track']}") # Clean up connection weaviate_db.close() ``` -------------------------------- ### Generate Embeddings using OpenAI CLI Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Generate embeddings for a CSV file using OpenAI's embedding models. The output is saved in a Parquet file. Requires the 'use_openai' flag and specifying the desired OpenAI model. ```sh python embeddings/embed.py --csv data/muse.csv --out data/embeddings.parquet --use_openai --model text-embedding-3-large ``` -------------------------------- ### Check Port Usage with lsof Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Command to identify processes using a specific network port. Replace ':PORT' with the actual port number (e.g., :8080). This helps in diagnosing port conflicts. ```bash lsof -i :PORT ``` -------------------------------- ### Python: Visualize Benchmark Results Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt This Python script generates performance charts and tables from benchmark results. It uses a custom `plot_benchmarks` module to load metrics and display summary plots, and also shows how to programmatically read and interpret the JSON metrics. ```python from plot_benchmarks import load_metrics, plot_summary from pathlib import Path import subprocess # Run plot generation script subprocess.run([ "python", "plot_benchmarks.py", "results/metrics.json", "results/benchmark_summary" ]) # Results include: # - results/benchmark_summary.png: Overall comparison # - results/k5/avg_query_latency_sec_k5.png # - results/k10/avg_recall_at_10_k10.png # - results/k10/metrics_table_k10.png # Read metrics programmatically import json with open("results/metrics.json", "r") as f: metrics = json.load(f) for db_name in ["qdrant", "milvus", "weaviate"]: k10_metrics = metrics[db_name]["k=10"] print(f"\n{db_name.upper()} (k=10):") print(f" Avg Latency: {k10_metrics['avg_query_latency_sec']*1000:.2f}ms") print(f" P95 Latency: {k10_metrics['p95_query_latency_sec']*1000:.2f}ms") print(f" Recall@10: {k10_metrics['avg_recall_at_10']:.4f}") print(f" QPS: {k10_metrics['avg_qps']:.2f}") print(f" Ingest Time: {k10_metrics['ingest_time_sec']:.2f}s") ``` -------------------------------- ### Calculate Relevance Metrics for Search Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Calculates hit rate and recall metrics to evaluate search quality. It includes functions for calculating hits based on row ID matching and performing heuristic relevance matching using tags and genres. These metrics help assess the effectiveness of search results. ```python from utils.metrics import hits_at_k, relevance_hit # Example: Calculate hits using row_id matching results_payloads = [ {"row_id": 42, "track": "Song A", "genre": "rock"}, {"row_id": 15, "track": "Song B", "genre": "pop"}, {"row_id": 99, "track": "Song C", "genre": "rock"} ] expected_row_ids = {15, 42, 101, 250} hit_count = hits_at_k(results_payloads, expected_row_ids) hit_rate = hit_count / len(results_payloads) print(f"Hit rate: {hit_rate:.2f} ({hit_count}/{len(results_payloads)})") # Example: Heuristic relevance matching with tags/genres query_expected = { "tags": ["aggressive", "energetic"], "genres": ["rap", "hip-hop"] } result_payload = { "track": "Fight Song", "seeds": "['aggressive', 'loud']", "genre": "rap" } is_relevant = relevance_hit(result_payload, query_expected) print(f"Result is relevant: {is_relevant}") # True (tag match) # Batch evaluation results = [ {"seeds": "['aggressive']", "genre": "rap"}, {"seeds": "['calm']", "genre": "classical"}, {"seeds": "['energetic']", "genre": "hip-hop"} ] hits = hits_at_k(results, query_expected) print(f"Hits at k=3: {hits}") # 2 hits ``` -------------------------------- ### Generate Embeddings using Sentence Transformers (Python) Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Generates embeddings for a dataset using the sentence-transformers library. This script takes a CSV file as input and outputs embeddings to a Parquet file. It supports using OpenAI embeddings with an additional flag. ```python python embeddings/embed.py --csv data/muse.csv --out data/embeddings.parquet # For OpenAI: add --use_openai [--model text-embedding-3-large] ``` -------------------------------- ### Check Docker Compose Service Status Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Displays the status of services defined in a docker-compose.yml file, including whether they are running and healthy. Useful for verifying that all components of an application are operational. ```bash docker compose ps ``` -------------------------------- ### Stop a Docker Container Source: https://github.com/scriptstar/vector-db-benchmark/blob/master/README.md Stops a running Docker container. Replace 'CONTAINER_NAME' with the name or ID of the container to be stopped. Useful for freeing up resources or resolving conflicts. ```bash docker stop CONTAINER_NAME ``` -------------------------------- ### Generate Embeddings from Music Metadata Source: https://context7.com/scriptstar/vector-db-benchmark/llms.txt Generates embeddings from music metadata using sentence transformers or OpenAI. It involves loading a CSV, building text representations, generating embeddings, and saving them to a Parquet file. Dependencies include pandas and the embeddings library. The output is a DataFrame with an 'embedding' column. ```python import pandas as pd from embeddings.embed import build_text_row, embed_st, embed_openai # Load music dataset df = pd.read_csv("data/muse.csv") # Build text representations df["text"] = df.apply(build_text_row, axis=1) # Example output: "Bohemian Rhapsody by Queen. Genre: rock. Tags: ['dramatic', 'epic']" # Generate embeddings with sentence transformers (default) vectors = embed_st(df, model_name="sentence-transformers/all-MiniLM-L6-v2") print(f"Generated {len(vectors)} embeddings of dimension {vectors.shape[1]}") # Save embeddings to parquet df_out = df.copy() df_out["embedding"] = [v.tolist() for v in vectors] df_out.to_parquet("data/embeddings.parquet", index=False) # Alternative: Use OpenAI embeddings import os os.environ["OPENAI_API_KEY"] = "your-api-key" openai_vectors = embed_openai(df, model_name="text-embedding-3-large") print(f"OpenAI embeddings dimension: {openai_vectors.shape[1]}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.