Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pgvector support for C++

License

NotificationsYou must be signed in to change notification settings

pgvector/pgvector-cpp

Repository files navigation

pgvector support for C++

Supportslibpqxx

Build Status

Installation

Addthe headers to your project (supports C++17 and greater).

There is also support for CMake and FetchContent:

include(FetchContent)FetchContent_Declare(pgvectorGIT_REPOSITORYhttps://github.com/pgvector/pgvector-cpp.gitGIT_TAGv0.2.3)FetchContent_MakeAvailable(pgvector)target_link_libraries(appPRIVATEpgvector::pgvector)

Getting Started

Follow the instructions for your database library:

Or check out some examples:

libpqxx

Include the header

#include<pgvector/pqxx.hpp>

Enable the extension

tx.exec("CREATE EXTENSION IF NOT EXISTS vector");

Create a table

tx.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");

Insert a vector

auto embedding = pgvector::Vector({1,2,3});tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});

Get the nearest neighbors

pqxx::result r = tx.exec("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", {embedding});

Retrieve a vector

auto row = tx.exec("SELECT embedding FROM items LIMIT 1").one_row();auto embedding = row[0].as<pgvector::Vector>();

Usestd::optional<pgvector::Vector> if the value could beNULL

Reference

Vectors

Create a vector from astd::vector<float>

auto vec = pgvector::Vector({1,2,3});

Convert to astd::vector<float>

auto float_vec =static_cast<std::vector<float>>(vec);

Half Vectors

Create a half vector from astd::vector<float>

auto vec = pgvector::HalfVector({1,2,3});

Convert to astd::vector<float>

auto float_vec =static_cast<std::vector<float>>(vec);

Sparse Vectors

Create a sparse vector from astd::vector<float>

auto vec = pgvector::SparseVector({1,0,2,0,3,0});

Or a map of non-zero elements

std::unordered_map<int,float> map = {{0,1}, {2,2}, {4,3}};auto vec = pgvector::SparseVector(map,6);

Get the number of dimensions

int dim = vec.dimensions();

Get the indices of non-zero elements

auto indices = vec.indices();

Get the values of non-zero elements

auto values = vec.values();

History

View thechangelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-cpp.gitcd pgvector-cppcreatedb pgvector_cpp_testcmake -S. -B build -DBUILD_TESTING=ONcmake --build buildbuild/test

To run an example:

cd examples/loadingcreatedb pgvector_examplecmake -S. -B buildcmake --build buildbuild/example

About

pgvector support for C++

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp