-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
155 lines (126 loc) · 4.91 KB
/
example.c
File metadata and controls
155 lines (126 loc) · 4.91 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cjson/cJSON.h>
#include <curl/curl.h>
#include <libpq-fe.h>
// note: error handling omitted for simplicity
char **embed(
const char *const *texts,
size_t texts_size,
const char *input_type,
const char *api_key
) {
// prepare request
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cohere.com/v2/embed");
// prepare request data
cJSON *request_json = cJSON_CreateObject();
cJSON *texts_json = cJSON_AddArrayToObject(request_json, "texts");
for (size_t i = 0; i < texts_size; i++) {
cJSON_AddItemToArray(texts_json, cJSON_CreateStringReference(texts[i]));
}
cJSON_AddStringToObject(request_json, "model", "embed-v4.0");
cJSON_AddStringToObject(request_json, "input_type", input_type);
cJSON *embedding_types = cJSON_AddArrayToObject(request_json, "embedding_types");
cJSON_AddItemToArray(embedding_types, cJSON_CreateString("ubinary"));
char *request_data = cJSON_PrintUnformatted(request_json);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_data);
// prepare request headers
struct curl_slist *headers = NULL;
char authorization[1024];
snprintf(authorization, sizeof(authorization), "Authorization: Bearer %s", api_key);
headers = curl_slist_append(headers, authorization);
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// prepare response stream
char *response_data;
size_t unused;
FILE *response_stream = open_memstream(&response_data, &unused);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response_stream);
// perform request
CURLcode res = curl_easy_perform(curl);
assert(res == CURLE_OK);
// check response code
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
assert(response_code == 200);
// process response data
fclose(response_stream);
cJSON *response_json = cJSON_Parse(response_data);
cJSON *embeddings_json = cJSON_GetObjectItemCaseSensitive(response_json, "embeddings");
cJSON *ubinary = cJSON_GetObjectItemCaseSensitive(embeddings_json, "ubinary");
char **embeddings = malloc(texts_size * sizeof(char *));
for (size_t i = 0; i < texts_size; i++) {
cJSON *embedding_json = cJSON_GetArrayItem(ubinary, i);
char *embedding = malloc((cJSON_GetArraySize(embedding_json) * 8 + 1) * sizeof(char));
int pos = 0;
cJSON *v;
cJSON_ArrayForEach(v, embedding_json) {
for (int j = 0; j < 8; j++)
embedding[pos++] = (v->valueint & (1 << j)) ? '1' : '0';
}
embedding[pos] = 0;
embeddings[i] = embedding;
}
// free data
cJSON_Delete(request_json);
free(request_data);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
free(response_data);
cJSON_Delete(response_json);
return embeddings;
}
int main(void) {
char *api_key = getenv("CO_API_KEY");
if (!api_key) {
printf("Set CO_API_KEY\n");
return 1;
}
curl_global_init(CURL_GLOBAL_DEFAULT);
PGconn *conn = PQconnectdb("postgres://localhost/pgvector_example");
assert(PQstatus(conn) == CONNECTION_OK);
PGresult *res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector");
assert(PQresultStatus(res) == PGRES_COMMAND_OK);
PQclear(res);
res = PQexec(conn, "DROP TABLE IF EXISTS documents");
assert(PQresultStatus(res) == PGRES_COMMAND_OK);
PQclear(res);
res = PQexec(
conn, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1536))"
);
assert(PQresultStatus(res) == PGRES_COMMAND_OK);
PQclear(res);
const char *input[] = {"The dog is barking", "The cat is purring", "The bear is growling"};
char **embeddings = embed(input, 3, "search_document", api_key);
for (size_t i = 0; i < 3; i++) {
const char *params[] = {input[i], embeddings[i]};
res = PQexecParams(
conn, "INSERT INTO documents (content, embedding) VALUES ($1, $2)", 2, NULL, params,
NULL, NULL, 0
);
assert(PQresultStatus(res) == PGRES_COMMAND_OK);
PQclear(res);
free(embeddings[i]);
}
free(embeddings);
const char *query[] = {"forest"};
char **query_embeddings = embed(query, 1, "search_query", api_key);
const char *query_params[] = {query_embeddings[0]};
res = PQexecParams(
conn, "SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5", 1, NULL,
query_params, NULL, NULL, 0
);
assert(PQresultStatus(res) == PGRES_TUPLES_OK);
int ntuples = PQntuples(res);
for (int i = 0; i < ntuples; i++) {
printf("%s\n", PQgetvalue(res, i, 0));
}
PQclear(res);
free(query_embeddings[0]);
free(query_embeddings);
curl_global_cleanup();
return 0;
}