summaryrefslogtreecommitdiff
path: root/vectordb.c
blob: b6fae647a7e5404423a824074eb1a0a9654c2b1d (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdint.h>

#include "llama.h"
#include "vectordb.h"
#include "nonstd.h"

#define VDB_MAGIC 0x31424456u /* "VDB1" */
#define VDB_VERSION 1u

typedef struct {
	uint32_t magic;
	uint32_t version;
	uint32_t embed_size;
	uint32_t max_text;
	uint32_t count;
} VdbFileHeader;

static float cosine_similarity(float *a, float *b, int n) {
	float dot = 0, norm_a = 0, norm_b = 0;
	for (int i = 0; i < n; i++) {
		dot += a[i] * b[i];
		norm_a += a[i] * a[i];
		norm_b += b[i] * b[i];
	}
	return dot / (sqrtf(norm_a) * sqrtf(norm_b) + 1e-8f);
}

static void embed_text(struct llama_context *ctx, const char *text, float *out) {
	llama_token tokens[512];
	const struct llama_model *model = llama_get_model(ctx);
	const struct llama_vocab *vocab = llama_model_get_vocab(model);
	int n_tokens = llama_tokenize(vocab, text, strlen(text), tokens, 512, true, true);
	if (n_tokens < 0) {
		return;
	}

	struct llama_batch batch = llama_batch_get_one(tokens, n_tokens);
	llama_decode(ctx, batch);

	const float *emb = llama_get_embeddings(ctx);
	memcpy(out, emb, sizeof(float) * VDB_EMBED_SIZE);

}

void vdb_init(VectorDB *db, struct llama_context *embed_ctx) {
	memset(db, 0, sizeof(VectorDB));
	db->embed_ctx = embed_ctx;
}

void vdb_free(VectorDB *db) {
	(void)db;
}

void vdb_add_document(VectorDB *db, const char *text) {
	if (db->count >= VDB_MAX_DOCS) {
		log_message(stdout, LOG_INFO, "Vector database full");
		return;
	}

	VectorDoc *doc = &db->docs[db->count++];
	strncpy(doc->text, text, VDB_MAX_TEXT - 1);
	doc->text[VDB_MAX_TEXT - 1] = 0;

	log_message(stdout, LOG_INFO, "Embedding doc %d...", db->count);
	embed_text(db->embed_ctx, text, doc->embedding);
}

void vdb_embed_query(VectorDB *db, const char *text, float *out_embedding) {
	embed_text(db->embed_ctx, text, out_embedding);
}

void vdb_search(VectorDB *db, float *query, int top_k, int *results) {
	float best_scores[top_k];
	for (int i = 0; i < top_k; i++) {
		best_scores[i] = -1.0f;
		results[i] = -1;
	}

	for (int i = 0; i < db->count; i++) {
		float score = cosine_similarity(query, db->docs[i].embedding, VDB_EMBED_SIZE);

		for (int j = 0; j < top_k; j++) {
			if (score > best_scores[j]) {
				for (int k = top_k - 1; k > j; k--) {
					best_scores[k] = best_scores[k - 1];
					results[k] = results[k - 1];
				}
				best_scores[j] = score;
				results[j] = i;
				break;
			}
		}
	}
}

int vdb_save(const VectorDB *db, const char *path) {
	FILE *fp = fopen(path, "wb");
	if (!fp) {
		return 1;
	}

	VdbFileHeader header = {
		.magic = VDB_MAGIC,
		.version = VDB_VERSION,
		.embed_size = VDB_EMBED_SIZE,
		.max_text = VDB_MAX_TEXT,
		.count = (uint32_t)db->count,
	};

	if (fwrite(&header, sizeof(header), 1, fp) != 1) {
		fclose(fp);
		return 2;
	}

	if (db->count > 0) {
		size_t wrote = fwrite(db->docs, sizeof(VectorDoc), (size_t)db->count, fp);
		if (wrote != (size_t)db->count) {
			fclose(fp);
			return 3;
		}
	}

	if (fclose(fp) != 0) {
		return 4;
	}

	return 0;
}

int vdb_load(VectorDB *db, const char *path) {
	struct llama_context *ctx = db->embed_ctx;
	FILE *fp = fopen(path, "rb");
	if (!fp) {
		return -1;
	}

	VdbFileHeader header = {0};
	if (fread(&header, sizeof(header), 1, fp) != 1) {
		fclose(fp);
		return -2;
	}

	if (header.magic != VDB_MAGIC || header.version != VDB_VERSION) {
		fclose(fp);
		return -3;
	}

	if (header.embed_size != VDB_EMBED_SIZE || header.max_text != VDB_MAX_TEXT) {
		fclose(fp);
		return -4;
	}

	if (header.count > VDB_MAX_DOCS) {
		fclose(fp);
		return -5;
	}

	memset(db, 0, sizeof(VectorDB));
	db->embed_ctx = ctx;
	db->count = (int)header.count;

	if (db->count > 0) {
		size_t read = fread(db->docs, sizeof(VectorDoc), (size_t)db->count, fp);
		if (read != (size_t)db->count) {
			fclose(fp);
			return -6;
		}
	}

	if (fclose(fp) != 0) {
		return -7;
	}

	return 0;
}