### Build and Run Loading Example Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Provides shell commands to navigate to the loading example directory, create a database for the example, configure and build the example using CMake, and execute the compiled example program. ```sh cd examples/loading createdb pgvector_example cmake -S . -B build cmake --build build build/example ``` -------------------------------- ### Setup Development Environment and Run Tests Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Provides shell commands to clone the repository, navigate into it, create a test database, configure CMake with testing enabled, build the project, and run the tests. ```sh git clone https://github.com/pgvector/pgvector-cpp.git cd pgvector-cpp createdb pgvector_cpp_test cmake -S . -B build -DBUILD_TESTING=ON cmake --build build build/test ``` -------------------------------- ### Install pgvector-cpp with CMake FetchContent Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Demonstrates how to integrate the pgvector-cpp library into a CMake project using FetchContent. Declares the remote repository and tag, makes it available, and links the library to a target. ```cmake include(FetchContent) FetchContent_Declare(pgvector GIT_REPOSITORY https://github.com/pgvector/pgvector-cpp.git GIT_TAG v0.2.2) FetchContent_MakeAvailable(pgvector) target_link_libraries(app PRIVATE pgvector::pgvector) ``` -------------------------------- ### Get Dimensions of pgvector::SparseVector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Retrieves the total number of dimensions defined for a `pgvector::SparseVector` object. ```cpp int dim = vec.dimensions(); ``` -------------------------------- ### Get Values of Non-Zero Elements in pgvector::SparseVector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Retrieves a vector containing the values of the non-zero elements within a `pgvector::SparseVector`. ```cpp auto values = vec.values(); ``` -------------------------------- ### Get Indices of Non-Zero Elements in pgvector::SparseVector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Retrieves a vector containing the indices of the non-zero elements within a `pgvector::SparseVector`. ```cpp auto indices = vec.indices(); ``` -------------------------------- ### Enable pgvector Extension in PostgreSQL (libpqxx) Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Executes a SQL command using a libpqxx transaction object (`tx`) to create the `vector` extension in the PostgreSQL database if it doesn't already exist. ```cpp tx.exec("CREATE EXTENSION IF NOT EXISTS vector"); ``` -------------------------------- ### Include pgvector libpqxx Header Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Includes the necessary header file to use pgvector functionality with the libpqxx C++ library. ```cpp #include ``` -------------------------------- ### Create pgvector::HalfVector from std::vector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Demonstrates how to construct a `pgvector::HalfVector` object from a `std::vector` initializer list. ```cpp auto vec = pgvector::HalfVector({1, 2, 3}); ``` -------------------------------- ### Create pgvector::Vector from std::vector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Demonstrates how to construct a `pgvector::Vector` object directly from a `std::vector` initializer list. ```cpp auto vec = pgvector::Vector({1, 2, 3}); ``` -------------------------------- ### Query Nearest Neighbors (libpqxx) Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Executes a SQL query using a libpqxx transaction object (`tx`) to find the 5 nearest neighbors to a given `embedding` using the `<->` (L2 distance) operator and stores the result in a `pqxx::result` object. ```cpp pqxx::result r = tx.exec("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", {embedding}); ``` -------------------------------- ### Create Table with Vector Column (libpqxx) Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Executes a SQL command using a libpqxx transaction object (`tx`) to create a table named `items` with a primary key and a `vector` column of dimension 3. ```cpp tx.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"); ``` -------------------------------- ### Create pgvector::SparseVector from std::unordered_map Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Shows how to construct a `pgvector::SparseVector` from a map of non-zero indices and values, specifying the total number of dimensions. ```cpp std::unordered_map map = {{0, 1}, {2, 2}, {4, 3}}; auto vec = pgvector::SparseVector(map, 6); ``` -------------------------------- ### Insert Vector into Table (libpqxx) Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Creates a `pgvector::Vector` object from a `std::vector` and inserts it into the `items` table using a parameterized SQL query via a libpqxx transaction. ```cpp auto embedding = pgvector::Vector({1, 2, 3}); tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding}); ``` -------------------------------- ### Create pgvector::SparseVector from std::vector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Demonstrates how to construct a `pgvector::SparseVector` object from a `std::vector`, automatically handling zero values. ```cpp auto vec = pgvector::SparseVector({1, 0, 2, 0, 3, 0}); ``` -------------------------------- ### Retrieve Vector from Table (libpqxx) Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Executes a SQL query using a libpqxx transaction object (`tx`) to select a vector from the `items` table, retrieves a single row, and casts the vector column value to a `pgvector::Vector` object. ```cpp auto row = tx.exec("SELECT embedding FROM items LIMIT 1").one_row(); auto embedding = row[0].as(); ``` -------------------------------- ### Convert pgvector::Vector to std::vector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Shows how to cast a `pgvector::Vector` object back to its underlying `std::vector` representation. ```cpp auto float_vec = static_cast>(vec); ``` -------------------------------- ### Convert pgvector::HalfVector to std::vector Source: https://github.com/pgvector/pgvector-cpp/blob/master/README.md Shows how to cast a `pgvector::HalfVector` object back to its underlying `std::vector` representation. ```cpp auto float_vec = static_cast>(vec); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.