Skip to content

Commit 754e5d4

Browse files
committed
chore(docs): document context and token utility functions
Adds documentation for `llm_context_size()`, `llm_context_used()`, and `llm_token_count(text)` functions.
1 parent 054798d commit 754e5d4

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

API.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,35 @@ Frees the current inference context.
245245
SELECT llm_context_free();
246246
```
247247

248+
---
249+
## `llm_context_size()`
250+
251+
**Returns:** `INTEGER`
252+
253+
**Description**:
254+
Returns the total token capacity (context window) of the current llama context. Use this after `llm_context_create` to confirm the configured `context_size`. Raises an error if no context is active.
255+
256+
```sql
257+
SELECT llm_context_size();
258+
-- 4096
259+
```
260+
261+
---
262+
263+
## `llm_context_used()`
264+
265+
**Returns:** `INTEGER`
266+
267+
**Description:**
268+
Returns how many tokens of the current llama context have already been consumed. Combine this with `llm_context_size()` to monitor usage. Raises an error if no context is active.
269+
270+
**Example:**
271+
272+
```sql
273+
SELECT llm_context_used();
274+
-- 1024
275+
```
276+
248277
---
249278

250279
## `llm_sampler_create()`
@@ -546,6 +575,22 @@ SELECT llm_sampler_init_penalties(64, 1.2, 0.5, 0.8);
546575

547576
---
548577

578+
## `llm_token_count(text TEXT)`
579+
580+
**Returns:** `INTEGER`
581+
582+
**Description:**
583+
Returns how many tokens the current model would consume for the supplied `text`, using the active context’s vocabulary. Requires a context created via `llm_context_create`.
584+
585+
**Example:**
586+
587+
```sql
588+
SELECT llm_token_count('Hello world!');
589+
-- 5
590+
```
591+
592+
---
593+
549594
## `llm_embed_generate(text TEXT, options TEXT)`
550595

551596
**Returns:** `BLOB` or `TEXT`

src/sqlite-ai.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,27 +2351,6 @@ 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_size (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-
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.");
2368-
return;
2369-
}
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);
2373-
}
2374-
23752354
static void llm_context_create_embedding (sqlite3_context *context, int argc, sqlite3_value **argv) {
23762355
const char *options = AI_DEFAULT_CONTEXT_EMBEDDING_OPTIONS;
23772356
const char *options2 = (argc > 0) ? (const char *)sqlite3_value_text(argv[0]) : NULL;
@@ -2393,6 +2372,27 @@ static void llm_context_create_textgen (sqlite3_context *context, int argc, sqli
23932372
llm_context_create_with_options(context, ai, options, options2);
23942373
}
23952374

2375+
static void llm_context_size (sqlite3_context *context, int argc, sqlite3_value **argv) {
2376+
ai_context *ai = (ai_context *)sqlite3_user_data(context);
2377+
if (!ai->ctx) {
2378+
sqlite_context_result_error(context, SQLITE_MISUSE, "No context found. Please call llm_context_create() before using this function.");
2379+
return;
2380+
}
2381+
uint32_t n_ctx = llama_n_ctx(ai->ctx);
2382+
sqlite3_result_int(context, n_ctx);
2383+
}
2384+
2385+
static void llm_context_used (sqlite3_context *context, int argc, sqlite3_value **argv) {
2386+
ai_context *ai = (ai_context *)sqlite3_user_data(context);
2387+
if (!ai->ctx) {
2388+
sqlite_context_result_error(context, SQLITE_MISUSE, "No context found. Please call llm_context_create() before using this function.");
2389+
return;
2390+
}
2391+
int32_t n_ctx_used = llama_memory_seq_pos_max(llama_get_memory(ai->ctx), 0) + 1;
2392+
if (n_ctx_used < 0) n_ctx_used = 0;
2393+
sqlite3_result_int(context, n_ctx_used);
2394+
}
2395+
23962396
static void llm_model_free (sqlite3_context *context, int argc, sqlite3_value **argv) {
23972397
ai_context *ai = (ai_context *)sqlite3_user_data(context);
23982398
ai_cleanup((void *)ai, true, false);

0 commit comments

Comments
 (0)