-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 866af95
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: build | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ankane/setup-postgres@v1 | ||
with: | ||
database: pgvector_c_test | ||
- run: | | ||
cd /tmp | ||
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git | ||
cd pgvector | ||
make | ||
sudo make install | ||
- run: gcc -Wall -Wextra -Werror -o test/pq test/pq_test.cpp -lpq | ||
- run: test/pq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/test/pq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2023 Andrew Kane | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# pgvector-c | ||
|
||
[pgvector](https://github.com/pgvector/pgvector) examples for C | ||
|
||
Supports [libpq](https://www.postgresql.org/docs/current/libpq.html) | ||
|
||
[![Build Status](https://github.com/pgvector/pgvector-c/workflows/build/badge.svg?branch=master)](https://github.com/pgvector/pgvector-c/actions) | ||
|
||
## Getting Started | ||
|
||
Follow the instructions for your database library: | ||
|
||
- [libpq](#libpq) | ||
|
||
## libpq | ||
|
||
Enable the extension | ||
|
||
```c | ||
PGresult *res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector"); | ||
``` | ||
|
||
Create a table | ||
|
||
```c | ||
PGresult *res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"); | ||
``` | ||
|
||
Insert vectors | ||
|
||
```c | ||
const char *paramValues[2] = {"[1,2,3]", "[4,5,6]"}; | ||
PGresult *res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2)", 2, NULL, paramValues, NULL, NULL, 0); | ||
``` | ||
Get the nearest neighbors | ||
```c | ||
const char *paramValues[1] = {"[3,1,2]"}; | ||
PGresult *res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, NULL, paramValues, NULL, NULL, 0); | ||
``` | ||
|
||
See a [full example](test/pq_test.c) | ||
|
||
## Contributing | ||
|
||
Everyone is encouraged to help improve this project. Here are a few ways you can help: | ||
|
||
- [Report bugs](https://github.com/pgvector/pgvector-c/issues) | ||
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-c/pulls) | ||
- Write, clarify, or fix documentation | ||
- Suggest or add new features | ||
|
||
To get started with development: | ||
|
||
```sh | ||
git clone https://github.com/pgvector/pgvector-c.git | ||
cd pgvector-c | ||
createdb pgvector_c_test | ||
gcc -Wall -Wextra -Werror -o test/pq test/pq_test.c -lpq | ||
test/pq | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <assert.h> | ||
#include <libpq-fe.h> | ||
|
||
int main() { | ||
PGconn *conn; | ||
PGresult *res; | ||
|
||
conn = PQconnectdb("postgres://localhost/pgvector_c_test"); | ||
assert(PQstatus(conn) == CONNECTION_OK); | ||
|
||
res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector"); | ||
assert(PQresultStatus(res) == PGRES_COMMAND_OK); | ||
PQclear(res); | ||
|
||
res = PQexec(conn, "DROP TABLE IF EXISTS items"); | ||
assert(PQresultStatus(res) == PGRES_COMMAND_OK); | ||
PQclear(res); | ||
|
||
res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"); | ||
assert(PQresultStatus(res) == PGRES_COMMAND_OK); | ||
PQclear(res); | ||
|
||
const char *paramValues[3] = {"[1,1,1]", "[2,2,2]", "[1,1,2]"}; | ||
res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", 3, NULL, paramValues, NULL, NULL, 0); | ||
assert(PQresultStatus(res) == PGRES_COMMAND_OK); | ||
PQclear(res); | ||
|
||
const char *paramValues2[1] = {"[1,1,1]"}; | ||
res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, NULL, paramValues2, NULL, NULL, 0); | ||
assert(PQresultStatus(res) == PGRES_TUPLES_OK); | ||
int ntuples = PQntuples(res); | ||
for (int i = 0; i < ntuples; i++) { | ||
printf("%s: %s\n", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1)); | ||
} | ||
PQclear(res); | ||
|
||
PQfinish(conn); | ||
|
||
return 0; | ||
} |