Skip to content

Commit 7c2324f

Browse files
committed
feat: add llm_context_usage function to report context usage
Introduces the llm_context_usage SQLite function, which returns a JSON object with the current context size, tokens used, and usage ratio. This helps users monitor LLM context utilization and manage resources more effectively.
1 parent f590d4c commit 7c2324f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/sqlite-ai.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,29 @@ 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) {
2355+
ai_context *ai = (ai_context *)sqlite3_user_data(context);
2356+
if (!ai->ctx) {
2357+
sqlite_context_result_error(context, SQLITE_MISUSE, "No context found. Please call llm_context_create() before using this function.");
2358+
return;
2359+
}
2360+
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");
2372+
return;
2373+
}
2374+
sqlite3_result_text(context, buffer, len, SQLITE_TRANSIENT);
2375+
}
2376+
23542377
static void llm_context_create_embedding (sqlite3_context *context, int argc, sqlite3_value **argv) {
23552378
const char *options = AI_DEFAULT_CONTEXT_EMBEDDING_OPTIONS;
23562379
const char *options2 = (argc > 0) ? (const char *)sqlite3_value_text(argv[0]) : NULL;
@@ -2710,6 +2733,9 @@ SQLITE_AI_API int sqlite3_ai_init (sqlite3 *db, char **pzErrMsg, const sqlite3_a
27102733
rc = sqlite3_create_function(db, "llm_context_create", 1, SQLITE_UTF8, ctx, llm_context_create, NULL, NULL);
27112734
if (rc != SQLITE_OK) goto cleanup;
27122735

2736+
rc = sqlite3_create_function(db, "llm_context_usage", 0, SQLITE_UTF8, ctx, llm_context_usage, NULL, NULL);
2737+
if (rc != SQLITE_OK) goto cleanup;
2738+
27132739
rc = sqlite3_create_function(db, "llm_context_create_embedding", 0, SQLITE_UTF8, ctx, llm_context_create_embedding, NULL, NULL);
27142740
if (rc != SQLITE_OK) goto cleanup;
27152741

0 commit comments

Comments
 (0)