FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pgvector-cpp/examples/openai/example.cpp at master · pgvector/pgvector-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pgvector
/
pgvector-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
25
Code
Issues
0
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
pgvector-cpp
/
examples
/
openai
/
example.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (63 loc) · 2.28 KB
Breadcrumbs
pgvector-cpp
/
examples
/
openai
/
example.cpp
Copy path
File metadata and controls
74 lines (63 loc) · 2.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
include
<
iostream
>
#
include
<
string
>
#
include
<
vector
>
#
include
<
cpr/cpr.h
>
#
include
<
nlohmann/json.hpp
>
#
include
<
pgvector/pqxx.hpp
>
#
include
<
pqxx/pqxx
>
using
json = nlohmann::json;
//
https://platform.openai.com/docs/guides/embeddings/how-to-get-embeddings
//
input can be an array with 2048 elements
std::vector<std::vector<
float
>>
embed
(
const
std::vector<std::string>& input,
char
* api_key) {
std::string url{
"
https://api.openai.com/v1/embeddings
"
};
json data{{
"
input
"
, input}, {
"
model
"
,
"
text-embedding-3-small
"
}};
cpr::Response r =
cpr::Post
(
cpr::Url{url},
cpr::Body{data.
dump
()},
cpr::Bearer{api_key},
cpr::Header{{
"
Content-Type
"
,
"
application/json
"
}}
);
if
(r.
status_code
!=
200
) {
throw
std::runtime_error{
"
Bad status:
"
+
std::to_string
(r.
status_code
)};
}
json response =
json::parse
(r.
text
);
std::vector<std::vector<
float
>> embeddings;
for
(
const
auto
& v : response[
"
data
"
]) {
embeddings.
emplace_back
(v[
"
embedding
"
]);
}
return
embeddings;
}
int
main
() {
char
* api_key =
std::getenv
(
"
OPENAI_API_KEY
"
);
if
(!api_key) {
std::cout <<
"
Set OPENAI_API_KEY
"
<< std::endl;
return
1
;
}
pqxx::connection conn{
"
dbname=pgvector_example
"
};
pqxx::nontransaction tx{conn};
tx.
exec
(
"
CREATE EXTENSION IF NOT EXISTS vector
"
);
tx.
exec
(
"
DROP TABLE IF EXISTS documents
"
);
tx.
exec
(
"
CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))
"
);
std::vector<std::string> input{
"
The dog is barking
"
,
"
The cat is purring
"
,
"
The bear is growling
"
};
std::vector<std::vector<
float
>> embeddings =
embed
(input, api_key);
for
(
size_t
i =
0
; i < input.
size
(); i++) {
tx.
exec
(
"
INSERT INTO documents (content, embedding) VALUES ($1, $2)
"
,
pqxx::params{input[i], pgvector::Vector{embeddings[i]}}
);
}
std::string query{
"
forest
"
};
std::vector<
float
> query_embedding =
embed
({query}, api_key)[
0
];
pqxx::result result = tx.
exec
(
"
SELECT content FROM documents ORDER BY embedding <=> $1 LIMIT 5
"
,
pqxx::params{pgvector::Vector{query_embedding}}
);
for
(
const
auto
& row : result) {
std::cout << row[
0
].
as
<std::string>() << std::endl;
}
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL