|
ailia_llm
1.5.1.0
|
LLM inference library. More...
#include <wchar.h>
Go to the source code of this file.
Classes | |
| struct | _AILIALLMChatMessage |
| struct | _AILIALLMMediaData |
| Image/audio data for multimodal processing. Audio requires an audio-capable projector. Images can be loaded from file path, encoded buffer (JPG, PNG, etc.), or raw RGB data. Supported encoded buffer formats are JPG, PNG, TGA, BMP, PSD, GIF, HDR, and PIC. More... | |
| struct | _AILIALLMMultimodalChatMessage |
| Multimodal chat message with media attachments. More... | |
Macros | |
| #define | AILIA_LLM_API __stdcall |
| #define | AILIA_LLM_STATUS_SUCCESS (0) |
| Successful. More... | |
| #define | AILIA_LLM_STATUS_INVALID_ARGUMENT (-1) |
| Incorrect argument. More... | |
| #define | AILIA_LLM_STATUS_ERROR_FILE_API (-2) |
| File access failed. More... | |
| #define | AILIA_LLM_STATUS_INVALID_VERSION (-3) |
| Incorrect struct version. More... | |
| #define | AILIA_LLM_STATUS_BROKEN (-4) |
| A corrupt file was passed. More... | |
| #define | AILIA_LLM_STATUS_MEMORY_INSUFFICIENT (-5) |
| Insufficient memory. More... | |
| #define | AILIA_LLM_STATUS_THREAD_ERROR (-6) |
| Thread creation failed. More... | |
| #define | AILIA_LLM_STATUS_INVALID_STATE (-7) |
| The internal status is incorrect. More... | |
| #define | AILIA_LLM_STATUS_CONTEXT_FULL (-8) |
| Exceeded the context length. More... | |
| #define | AILIA_LLM_STATUS_ERROR_BUFFER_API (-9) |
| Buffer read failed. More... | |
| #define | AILIA_LLM_STATUS_PARSE_ERROR (-10) |
| Failed to parse the generated text. More... | |
| #define | AILIA_LLM_STATUS_UNIMPLEMENTED (-15) |
| Unimplemented error. More... | |
| #define | AILIA_LLM_STATUS_OTHER_ERROR (-128) |
| Unknown error. More... | |
Typedefs | |
| typedef struct _AILIALLMChatMessage | AILIALLMChatMessage |
| typedef struct _AILIALLMMediaData | AILIALLMMediaData |
| Image/audio data for multimodal processing. Audio requires an audio-capable projector. Images can be loaded from file path, encoded buffer (JPG, PNG, etc.), or raw RGB data. Supported encoded buffer formats are JPG, PNG, TGA, BMP, PSD, GIF, HDR, and PIC. More... | |
| typedef struct _AILIALLMMultimodalChatMessage | AILIALLMMultimodalChatMessage |
| Multimodal chat message with media attachments. More... | |
Functions | |
| AILIA_LLM_API int | ailiaLLMGetBackendCount (unsigned int *env_count) |
| Gets the number of available computational environments (CPU, GPU, HTP (QNN)). More... | |
| AILIA_LLM_API int | ailiaLLMGetBackendName (const char **env, unsigned int env_idx) |
| Gets the list of computational environments. More... | |
| AILIA_LLM_API int | ailiaLLMGetBackendDeviceName (const char **name, unsigned int env_idx) |
| Gets the device name for a computational environment. More... | |
| AILIA_LLM_API int | ailiaLLMSetBackend (struct AILIALLM *llm, unsigned int backend_idx) |
| Selects the backend for this instance before opening a model. More... | |
| AILIA_LLM_API int | ailiaLLMSetNumThreads (struct AILIALLM *llm, unsigned int n_threads) |
| Sets the CPU thread count before opening the model. More... | |
| AILIA_LLM_API int | ailiaLLMCreate (struct AILIALLM **llm) |
| Creates a LLM instance. More... | |
| AILIA_LLM_API int | ailiaLLMOpenModelFileA (struct AILIALLM *llm, const char *path, unsigned int n_ctx) |
| Open model file. More... | |
| AILIA_LLM_API int | ailiaLLMOpenModelFileW (struct AILIALLM *llm, const wchar_t *path, unsigned int n_ctx) |
| AILIA_LLM_API int | ailiaLLMOpenMtpModelFileA (struct AILIALLM *llm, const char *path, unsigned int n_draft) |
| Open an MTP assistant and enable draft/target verification decoding. More... | |
| AILIA_LLM_API int | ailiaLLMOpenMtpModelFileW (struct AILIALLM *llm, const wchar_t *path, unsigned int n_draft) |
| AILIA_LLM_API int | ailiaLLMGetContextSize (struct AILIALLM *llm, unsigned int *context_size) |
| Gets the size of context. More... | |
| AILIA_LLM_API int | ailiaLLMSetSamplingParams (struct AILIALLM *llm, unsigned int top_k, float top_p, float temp, unsigned int dist) |
| Set the sampling parameter. More... | |
| AILIA_LLM_API int | ailiaLLMSetThinking (struct AILIALLM *llm, unsigned int enable) |
| Enable or disable thinking (reasoning output). More... | |
| AILIA_LLM_API int | ailiaLLMSetPrompt (struct AILIALLM *llm, const AILIALLMChatMessage *message, unsigned int message_cnt) |
| Set the prompt. More... | |
| AILIA_LLM_API int | ailiaLLMGenerate (struct AILIALLM *llm, unsigned int *done) |
| Perform generate. More... | |
| AILIA_LLM_API int | ailiaLLMGetDeltaTextSize (struct AILIALLM *llm, unsigned int *buf_size) |
| Gets the size of text. (Include null) More... | |
| AILIA_LLM_API int | ailiaLLMGetDeltaText (struct AILIALLM *llm, char *text, unsigned int buf_size) |
| Gets the decoded text. More... | |
| AILIA_LLM_API int | ailiaLLMGetTokenCount (struct AILIALLM *llm, unsigned int *cnt, const char *text) |
| Gets the count of token. More... | |
| AILIA_LLM_API int | ailiaLLMGetPromptTokenCount (struct AILIALLM *llm, unsigned int *cnt) |
| Gets the count of prompt token. More... | |
| AILIA_LLM_API int | ailiaLLMGetGeneratedTokenCount (struct AILIALLM *llm, unsigned int *cnt) |
| Gets the count of prompt token. More... | |
| AILIA_LLM_API void | ailiaLLMDestroy (struct AILIALLM *llm) |
| It destroys the LLM instance. More... | |
| AILIA_LLM_API int | ailiaLLMOpenMultimodalProjectorFileA (struct AILIALLM *llm, const char *mmproj_path) |
| Load multimodal projector file. More... | |
| AILIA_LLM_API int | ailiaLLMOpenMultimodalProjectorFileW (struct AILIALLM *llm, const wchar_t *mmproj_path) |
| AILIA_LLM_API int | ailiaLLMGetMultimodalCapabilities (struct AILIALLM *llm, unsigned int *vision_support, unsigned int *audio_support) |
| Check if multimodal features are supported. More... | |
| AILIA_LLM_API int | ailiaLLMSetMultimodalPrompt (struct AILIALLM *llm, const AILIALLMMultimodalChatMessage *message, unsigned int message_cnt) |
| Set multimodal prompt. More... | |
| AILIA_LLM_API int | ailiaLLMSetTools (struct AILIALLM *llm, const char *tools_json) |
| Sets OpenAI-compatible tool definitions. NULL, empty string or array clears tools. More... | |
| AILIA_LLM_API int | ailiaLLMSetPromptJson (struct AILIALLM *llm, const char *messages_json) |
| Sets a prompt from structured JSON conversation history. More... | |
| AILIA_LLM_API int | ailiaLLMGetResponseJsonSize (struct AILIALLM *llm, unsigned int *buf_size) |
| Gets the buffered assistant JSON size, including the terminating null. More... | |
| AILIA_LLM_API int | ailiaLLMGetResponseJson (struct AILIALLM *llm, char *json, unsigned int buf_size) |
| Gets structured assistant JSON from output accumulated inside the SDK. More... | |
| AILIA_LLM_API const char * | ailiaLLMGetErrorDetail (struct AILIALLM *llm) |
| Returns details of the last model-open, prompt-setup or generation error. More... | |
| AILIA_LLM_API int | ailiaLLMGetQNNModelName (const char **model_name) |
| Gets the QNN model name for the current device. More... | |
LLM inference library.
| #define AILIA_LLM_API __stdcall |
| #define AILIA_LLM_STATUS_BROKEN (-4) |
A corrupt file was passed.
| #define AILIA_LLM_STATUS_CONTEXT_FULL (-8) |
Exceeded the context length.
| #define AILIA_LLM_STATUS_ERROR_BUFFER_API (-9) |
Buffer read failed.
| #define AILIA_LLM_STATUS_ERROR_FILE_API (-2) |
File access failed.
| #define AILIA_LLM_STATUS_INVALID_ARGUMENT (-1) |
Incorrect argument.
| #define AILIA_LLM_STATUS_INVALID_STATE (-7) |
The internal status is incorrect.
| #define AILIA_LLM_STATUS_INVALID_VERSION (-3) |
Incorrect struct version.
| #define AILIA_LLM_STATUS_MEMORY_INSUFFICIENT (-5) |
Insufficient memory.
| #define AILIA_LLM_STATUS_OTHER_ERROR (-128) |
Unknown error.
| #define AILIA_LLM_STATUS_PARSE_ERROR (-10) |
Failed to parse the generated text.
| #define AILIA_LLM_STATUS_SUCCESS (0) |
Successful.
| #define AILIA_LLM_STATUS_THREAD_ERROR (-6) |
Thread creation failed.
| #define AILIA_LLM_STATUS_UNIMPLEMENTED (-15) |
Unimplemented error.
| typedef struct _AILIALLMChatMessage AILIALLMChatMessage |
| typedef struct _AILIALLMMediaData AILIALLMMediaData |
Image/audio data for multimodal processing. Audio requires an audio-capable projector. Images can be loaded from file path, encoded buffer (JPG, PNG, etc.), or raw RGB data. Supported encoded buffer formats are JPG, PNG, TGA, BMP, PSD, GIF, HDR, and PIC.
| typedef struct _AILIALLMMultimodalChatMessage AILIALLMMultimodalChatMessage |
Multimodal chat message with media attachments.
| AILIA_LLM_API int ailiaLLMCreate | ( | struct AILIALLM ** | llm | ) |
Creates a LLM instance.
| llm | A pointer to the LLM instance pointer |
Creates a LLM instance.
| AILIA_LLM_API void ailiaLLMDestroy | ( | struct AILIALLM * | llm | ) |
It destroys the LLM instance.
| llm | A LLM instance pointer |
| AILIA_LLM_API int ailiaLLMGenerate | ( | struct AILIALLM * | llm, |
| unsigned int * | done | ||
| ) |
Perform generate.
| llm | A LLM instance pointer |
| done | Generation complete? |
The decoded result is obtained through the ailiaLLMGetDeltaText API. Each call to ailiaLLMGenerate decodes one token at a time. The value of done is 0 or 1. If done is 1, the generation is complete.
| AILIA_LLM_API int ailiaLLMGetBackendCount | ( | unsigned int * | env_count | ) |
Gets the number of available computational environments (CPU, GPU, HTP (QNN)).
| env_count | The storage location of the number of computational environment information |
| AILIA_LLM_API int ailiaLLMGetBackendDeviceName | ( | const char ** | name, |
| unsigned int | env_idx | ||
| ) |
Gets the device name for a computational environment.
| name | Storage for the device name (valid while the library remains loaded) |
| env_idx | Index enumerated by ailiaLLMGetBackendCount() |
| AILIA_LLM_API int ailiaLLMGetBackendName | ( | const char ** | env, |
| unsigned int | env_idx | ||
| ) |
Gets the list of computational environments.
| env | Storage for the environment name (valid while the library remains loaded) |
| env_idx | The index of the computational environment information (0 to ailiaLLMGetBackendCount() -1) |
| AILIA_LLM_API int ailiaLLMGetContextSize | ( | struct AILIALLM * | llm, |
| unsigned int * | context_size | ||
| ) |
Gets the size of context.
| llm | A LLM instance pointer |
| len | The length of context |
| AILIA_LLM_API int ailiaLLMGetDeltaText | ( | struct AILIALLM * | llm, |
| char * | text, | ||
| unsigned int | buf_size | ||
| ) |
Gets the decoded text.
| llm | A LLM instance pointer |
| text | Text(UTF8) |
| buf_size | Buffer size |
If ailiaLLMGenerate() is not run at all, the function returns AILIA_LLM_STATUS_INVALID_STATE .
| AILIA_LLM_API int ailiaLLMGetDeltaTextSize | ( | struct AILIALLM * | llm, |
| unsigned int * | buf_size | ||
| ) |
Gets the size of text. (Include null)
| llm | A LLM instance pointer |
| buf_size | The length of text |
| AILIA_LLM_API const char* ailiaLLMGetErrorDetail | ( | struct AILIALLM * | llm | ) |
Returns details of the last model-open, prompt-setup or generation error.
| AILIA_LLM_API int ailiaLLMGetGeneratedTokenCount | ( | struct AILIALLM * | llm, |
| unsigned int * | cnt | ||
| ) |
Gets the count of prompt token.
| llm | A LLM instance pointer |
| cnt | The count of generated token |
It can be called after calling ailiaLLMGenerate.
| AILIA_LLM_API int ailiaLLMGetMultimodalCapabilities | ( | struct AILIALLM * | llm, |
| unsigned int * | vision_support, | ||
| unsigned int * | audio_support | ||
| ) |
Check if multimodal features are supported.
| llm | A LLM instance pointer |
| vision_support | Whether image input (VLM) is supported |
| audio_support | Whether audio input (ALM) is supported |
Can be called after ailiaLLMOpenMultimodalProjectorFile.
| AILIA_LLM_API int ailiaLLMGetPromptTokenCount | ( | struct AILIALLM * | llm, |
| unsigned int * | cnt | ||
| ) |
Gets the count of prompt token.
| llm | A LLM instance pointer |
| cnt | The count of prompt token |
It can be called after calling ailiaLLMSetPrompt.
| AILIA_LLM_API int ailiaLLMGetQNNModelName | ( | const char ** | model_name | ) |
Gets the QNN model name for the current device.
| model_name | Storage for the QNN model name (for example, "sm8475"). The library owns the returned string; the caller must not free it. |
The returned name matches the text model filename emitted by ailia_llm_compile_c, excluding .qnn. The image-input (VLM) / audio-input (ALM) projector uses the same name followed by -mmproj.qnn. QNN is supported on Android arm64 and Windows ARM64. This call lazily loads the QNN plug-in. An unsupported SoC returns AILIA_LLM_STATUS_OTHER_ERROR. A missing plug-in or a non-BUILD_QNN library returns AILIA_LLM_STATUS_UNIMPLEMENTED.
| AILIA_LLM_API int ailiaLLMGetResponseJson | ( | struct AILIALLM * | llm, |
| char * | json, | ||
| unsigned int | buf_size | ||
| ) |
Gets structured assistant JSON from output accumulated inside the SDK.
| llm | LLM instance |
| json | UTF-8 output buffer |
| buf_size | Buffer size |
No GetDeltaText calls or application-side concatenation are required. Append the returned object to SetPromptJson history. A new prompt resets accumulated output. Call after generation completes. Unfinished calls/thinking return PARSE_ERROR without repair. An unset prompt or changed tools/thinking returns INVALID_STATE. Before generation, returns an empty assistant. Null arguments or insufficient buffers return INVALID_ARGUMENT. Reading does not consume the output.
| AILIA_LLM_API int ailiaLLMGetResponseJsonSize | ( | struct AILIALLM * | llm, |
| unsigned int * | buf_size | ||
| ) |
Gets the buffered assistant JSON size, including the terminating null.
| llm | LLM instance |
| buf_size | Required buffer size |
Same parsing rules and errors as ailiaLLMGetResponseJson.
| AILIA_LLM_API int ailiaLLMGetTokenCount | ( | struct AILIALLM * | llm, |
| unsigned int * | cnt, | ||
| const char * | text | ||
| ) |
Gets the count of token.
| llm | A LLM instance pointer |
| cnt | The count of token |
| text | Text(UTF8) |
| AILIA_LLM_API int ailiaLLMOpenModelFileA | ( | struct AILIALLM * | llm, |
| const char * | path, | ||
| unsigned int | n_ctx | ||
| ) |
Open model file.
| llm | A pointer to the LLM instance pointer |
| path | Path to a GGUF or self-contained .qnn model |
| n_ctx | Context length for model (0 is model default) |
Opens a GGUF or self-contained ailia QNN model. Without an explicit backend selection, .qnn selects HTP and GGUF selects CPU/GPU automatically. An explicit selection must match the model format. For .qnn, n_ctx=0 uses the context length stored in the model.
| AILIA_LLM_API int ailiaLLMOpenModelFileW | ( | struct AILIALLM * | llm, |
| const wchar_t * | path, | ||
| unsigned int | n_ctx | ||
| ) |
| AILIA_LLM_API int ailiaLLMOpenMtpModelFileA | ( | struct AILIALLM * | llm, |
| const char * | path, | ||
| unsigned int | n_draft | ||
| ) |
Open an MTP assistant and enable draft/target verification decoding.
| llm | LLM instance |
| path | Path to a matching Gemma 4 assistant GGUF, or assistant .qnn for a QNN target (A: UTF-8, W: UTF-16) |
| n_draft | Number of draft tokens (at least 1). QNN supports up to 3. |
Call once after opening the target and before setting a prompt. Supports text generation with CPU/GPU Gemma 4 GGUF and QNN models. Verify the requested draft tokens together with one target token.
| AILIA_LLM_API int ailiaLLMOpenMtpModelFileW | ( | struct AILIALLM * | llm, |
| const wchar_t * | path, | ||
| unsigned int | n_draft | ||
| ) |
| AILIA_LLM_API int ailiaLLMOpenMultimodalProjectorFileA | ( | struct AILIALLM * | llm, |
| const char * | mmproj_path | ||
| ) |
Load multimodal projector file.
| llm | A LLM instance pointer |
| mmproj_path | Path to an MMPROJ GGUF or self-contained ailia QNN projector (.qnn) |
To use multimodal features, you must first load the text model with ailiaLLMOpenModelFile, then load the multimodal projector with this function. A .qnn projector includes its device-specific context and needs no separate GGUF. Image input (VLM) requires a vision-capable projector, while audio input (ALM) requires an audio-capable projector.
| AILIA_LLM_API int ailiaLLMOpenMultimodalProjectorFileW | ( | struct AILIALLM * | llm, |
| const wchar_t * | mmproj_path | ||
| ) |
| AILIA_LLM_API int ailiaLLMSetBackend | ( | struct AILIALLM * | llm, |
| unsigned int | backend_idx | ||
| ) |
Selects the backend for this instance before opening a model.
| llm | LLM instance |
| backend_idx | Device index returned by ailiaLLMGetBackendCount/Name |
Without a selection, the model format selects the backend automatically. HTP (QNN) accepts only .qnn; an explicit CPU/GPU selection rejects .qnn. Reopen the model in a new instance to switch.
| AILIA_LLM_API int ailiaLLMSetMultimodalPrompt | ( | struct AILIALLM * | llm, |
| const AILIALLMMultimodalChatMessage * | message, | ||
| unsigned int | message_cnt | ||
| ) |
Set multimodal prompt.
| llm | A LLM instance pointer |
| message | Array of multimodal messages |
| message_cnt | Number of messages |
Set multimodal prompt. Include <media> placeholders in message content, and set corresponding media data in media_data. Example: "Describe this image: <__media__>" The content of message is copied internally, so it can be freed after the call. Images can be loaded from file path, encoded buffer (JPG, PNG, etc.), or raw RGB data. Audio accepts WAV/MP3/FLAC file paths or encoded buffers, with width/height=0. KV cache reuse identifies file and buffer media by their content hashes. Encoder outputs are retained only for media in the current prompt; removed media is evicted. Decoded images/audio are released when prompt setup returns. Successfully switching to a prompt without media releases the media caches.
| AILIA_LLM_API int ailiaLLMSetNumThreads | ( | struct AILIALLM * | llm, |
| unsigned int | n_threads | ||
| ) |
Sets the CPU thread count before opening the model.
| llm | LLM instance |
| n_threads | Thread count (0 selects automatically). |
The default is 0 when this API is not called. On Android, automatic selection uses the number of performance (big/prime) cores. If topology is unavailable, the existing core-count heuristic is used. Target decode/prefill, assistant and VLM/ALM CPU inference threads share this value.
| AILIA_LLM_API int ailiaLLMSetPrompt | ( | struct AILIALLM * | llm, |
| const AILIALLMChatMessage * | message, | ||
| unsigned int | message_cnt | ||
| ) |
Set the prompt.
| llm | A pointer to the LLM instance pointer |
| message | Array of messages |
| message_cnt | Number of messages |
Set the prompt to query the LLM. Please include ChatHistory in the message as well. Returns INVALID_STATE while tools are configured. Use ailiaLLMSetPromptJson from the first user message. Clear tools with ailiaLLMSetTools to use this API again. The contents of the message are copied internally, so you can free them after the call.
| AILIA_LLM_API int ailiaLLMSetPromptJson | ( | struct AILIALLM * | llm, |
| const char * | messages_json | ||
| ) |
Sets a prompt from structured JSON conversation history.
| llm | A LLM instance pointer |
| messages_json | Nonempty UTF-8 JSON message array (null terminated) |
Roles are system/user/assistant/tool; content is a string or null. User content also accepts arrays. Parts are {"type":"text","text":"..."}, or type image/audio with exactly one of file_path (local file) and data (standard padded Base64 encoded file). JPEG/PNG and WAV/MP3/FLAC are supported by the decoder. Media requires a compatible loaded projector. URLs, raw PCM/RGB and video are unsupported. Parts retain input order. Matching history prefixes reuse KV when ordered media inputs are unchanged and the required KV positions remain resident. Encoded media is reused subject to backend cache conditions. GGUF and QNN reuse retained encoder outputs even when KV must be rebuilt. File content hashes are checked on each call; changed media content is re-evaluated. Encoder outputs are retained only for media in the current prompt; removed media is evicted. Decoded images/audio are released when prompt setup returns. Successfully switching to a prompt without media releases the media caches. Assistant content is not reparsed as model-specific syntax. Supply reasoning_content and tool_calls explicitly. Append the assistant JSON returned by ailiaLLMGetResponseJson directly to the array. Each tool call requires type="function", nonempty id and function.name, and function.arguments as a JSON-object string. IDs must be unique within an assistant message but may be reused in later turns. Tool results require string content and tool_call_id matching the preceding assistant. Consecutive results are matched by ID, not position; names are filled in. Duplicate/unmatched results are rejected. Incomplete arguments are rejected, never repaired or discarded. If GetResponseJson returns PARSE_ERROR, do not append the failed assistant; append a retry user message to the existing history. Historical tool calls/results remain accepted after SetTools(NULL). Retrieve response JSON before clearing tools. Input can be freed after the call. On success, generate with ailiaLLMGenerate.
| AILIA_LLM_API int ailiaLLMSetSamplingParams | ( | struct AILIALLM * | llm, |
| unsigned int | top_k, | ||
| float | top_p, | ||
| float | temp, | ||
| unsigned int | dist | ||
| ) |
Set the sampling parameter.
| llm | A pointer to the LLM instance pointer |
| top_k | Sampling probability value's top number, default 40 |
| top_p | Sampling probability value range, default 0.9 (0.9 to 1.0) |
| temp | Temperature parameter, default 0.4 |
| dist | Seed, default 1234 |
Set LLM sampling parameters. Must be run before ailiaLLMSetPrompt.
| AILIA_LLM_API int ailiaLLMSetThinking | ( | struct AILIALLM * | llm, |
| unsigned int | enable | ||
| ) |
Enable or disable thinking (reasoning output).
| llm | A pointer to the LLM instance pointer |
| enable | 0 to disable, non-zero to enable (default: disabled) |
Controls whether thinking models (e.g. Gemma4) output their reasoning process. Must be called before ailiaLLMSetPrompt.
| AILIA_LLM_API int ailiaLLMSetTools | ( | struct AILIALLM * | llm, |
| const char * | tools_json | ||
| ) |
Sets OpenAI-compatible tool definitions. NULL, empty string or array clears tools.
| llm | LLM instance |
| tools_json | UTF-8 tool definition array |
SetPrompt/SetMultimodalPrompt return INVALID_STATE while tools are configured. Use SetPromptJson from the first user input, generate, then retrieve assistant JSON with GetResponseJson. Deltas are optional previews; no concatenation is required. Return results using tool_call_id. Set a new prompt after changing tools. Invalid definitions return INVALID_ARGUMENT.