- Notifications
You must be signed in to change notification settings - Fork5
pgvector/pgvector-cpp
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pgvector support for C++
Supportslibpqxx
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)
Follow the instructions for your database library:
Or check out some examples:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with llama.cpp (Reciprocal Rank Fusion)
- Sparse search with Text Embeddings Inference
- Morgan fingerprints with RDKit
- Recommendations with Disco
- Horizontal scaling with Citus
- Bulk loading with
COPY
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
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);
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);
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();
View thechangelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs andsubmit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.