Skip to content

Commit e492310

Browse files
committed
refactor: split llm_context_usage into size and used functions
Replaces the llm_context_usage function with two separate functions: llm_context_size, which returns the total context size, and llm_context_used, which returns the number of tokens used. Updates function registration accordingly for improved clarity and granularity.
1 parent 79b8b3a commit e492310

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/sqlite-ai.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,27 +2351,25 @@ static void llm_context_create (sqlite3_context *context, int argc, sqlite3_valu
23512351
llm_context_create_with_options(context, ai, options, NULL);
23522352
}
23532353

2354-
static void llm_context_usage (sqlite3_context *context, int argc, sqlite3_value **argv) {
2354+
static void llm_context_size (sqlite3_context *context, int argc, sqlite3_value **argv) {
23552355
ai_context *ai = (ai_context *)sqlite3_user_data(context);
23562356
if (!ai->ctx) {
23572357
sqlite_context_result_error(context, SQLITE_MISUSE, "No context found. Please call llm_context_create() before using this function.");
23582358
return;
23592359
}
23602360
uint32_t n_ctx = llama_n_ctx(ai->ctx);
2361-
int32_t n_ctx_used = llama_memory_seq_pos_max(llama_get_memory(ai->ctx), 0) + 1;
2362-
if (n_ctx_used < 0) n_ctx_used = 0;
2363-
double usage = (n_ctx == 0) ? 0.0 : ((double)(n_ctx_used) / (double)n_ctx);
2364-
char buffer[256];
2365-
int len = snprintf(buffer, sizeof(buffer),
2366-
"{\"context_size\":%u,\"tokens_used\":%d,\"usage\":%.6f}",
2367-
n_ctx,
2368-
n_ctx_used,
2369-
usage);
2370-
if (len < 0 || len >= (int)sizeof(buffer)) {
2371-
sqlite_context_result_error(context, SQLITE_ERROR, "Failed to format context usage");
2361+
sqlite3_result_int(context, n_ctx);
2362+
}
2363+
2364+
static void llm_context_used (sqlite3_context *context, int argc, sqlite3_value **argv) {
2365+
ai_context *ai = (ai_context *)sqlite3_user_data(context);
2366+
if (!ai->ctx) {
2367+
sqlite_context_result_error(context, SQLITE_MISUSE, "No context found. Please call llm_context_create() before using this function.");
23722368
return;
23732369
}
2374-
sqlite3_result_text(context, buffer, len, SQLITE_TRANSIENT);
2370+
int32_t n_ctx_used = llama_memory_seq_pos_max(llama_get_memory(ai->ctx), 0) + 1;
2371+
if (n_ctx_used < 0) n_ctx_used = 0;
2372+
sqlite3_result_int(context, n_ctx_used);
23752373
}
23762374

23772375
static void llm_context_create_embedding (sqlite3_context *context, int argc, sqlite3_value **argv) {
@@ -2733,7 +2731,10 @@ SQLITE_AI_API int sqlite3_ai_init (sqlite3 *db, char **pzErrMsg, const sqlite3_a
27332731
rc = sqlite3_create_function(db, "llm_context_create", 1, SQLITE_UTF8, ctx, llm_context_create, NULL, NULL);
27342732
if (rc != SQLITE_OK) goto cleanup;
27352733

2736-
rc = sqlite3_create_function(db, "llm_context_usage", 0, SQLITE_UTF8, ctx, llm_context_usage, NULL, NULL);
2734+
rc = sqlite3_create_function(db, "llm_context_size", 0, SQLITE_UTF8, ctx, llm_context_size, NULL, NULL);
2735+
if (rc != SQLITE_OK) goto cleanup;
2736+
2737+
rc = sqlite3_create_function(db, "llm_context_used", 0, SQLITE_UTF8, ctx, llm_context_used, NULL, NULL);
27372738
if (rc != SQLITE_OK) goto cleanup;
27382739

27392740
rc = sqlite3_create_function(db, "llm_context_create_embedding", 0, SQLITE_UTF8, ctx, llm_context_create_embedding, NULL, NULL);

0 commit comments

Comments
 (0)