ailia_llm  1.5.1.0
Classes | Macros | Typedefs | Functions
ailia_llm.h File Reference

LLM inference library. More...

#include <wchar.h>
Include dependency graph for ailia_llm.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...
 

Detailed Description

LLM inference library.

Date
October 11, 2026

Macro Definition Documentation

◆ AILIA_LLM_API

#define AILIA_LLM_API   __stdcall

◆ AILIA_LLM_STATUS_BROKEN

#define AILIA_LLM_STATUS_BROKEN   (-4)

A corrupt file was passed.

Remarks
Please check model file are correct or not, and please pass correct model.

◆ AILIA_LLM_STATUS_CONTEXT_FULL

#define AILIA_LLM_STATUS_CONTEXT_FULL   (-8)

Exceeded the context length.

Remarks
Please reduce the context length given to SetPrompt.

◆ AILIA_LLM_STATUS_ERROR_BUFFER_API

#define AILIA_LLM_STATUS_ERROR_BUFFER_API   (-9)

Buffer read failed.

Remarks
Please check that the buffer format and size are correct.

◆ AILIA_LLM_STATUS_ERROR_FILE_API

#define AILIA_LLM_STATUS_ERROR_FILE_API   (-2)

File access failed.

Remarks
Please check file is exist or not, and check access permission.

◆ AILIA_LLM_STATUS_INVALID_ARGUMENT

#define AILIA_LLM_STATUS_INVALID_ARGUMENT   (-1)

Incorrect argument.

Remarks
Please check argument of called API.

◆ AILIA_LLM_STATUS_INVALID_STATE

#define AILIA_LLM_STATUS_INVALID_STATE   (-7)

The internal status is incorrect.

Remarks
Please check API document and API call steps.

◆ AILIA_LLM_STATUS_INVALID_VERSION

#define AILIA_LLM_STATUS_INVALID_VERSION   (-3)

Incorrect struct version.

Remarks
Please check struct version that passed with API and please pass correct struct version.

◆ AILIA_LLM_STATUS_MEMORY_INSUFFICIENT

#define AILIA_LLM_STATUS_MEMORY_INSUFFICIENT   (-5)

Insufficient memory.

Remarks
Please check usage of main memory and VRAM. And please call API after free memory.

◆ AILIA_LLM_STATUS_OTHER_ERROR

#define AILIA_LLM_STATUS_OTHER_ERROR   (-128)

Unknown error.

Remarks
The misc error has been occurred.

◆ AILIA_LLM_STATUS_PARSE_ERROR

#define AILIA_LLM_STATUS_PARSE_ERROR   (-10)

Failed to parse the generated text.

Remarks
The model output did not match the tool call syntax of the chat template. Please check that the same tool definitions (ailiaLLMSetTools) and thinking setting as used for the generation are set, and that the text is the complete output of the generation.

◆ AILIA_LLM_STATUS_SUCCESS

#define AILIA_LLM_STATUS_SUCCESS   (0)

Successful.

◆ AILIA_LLM_STATUS_THREAD_ERROR

#define AILIA_LLM_STATUS_THREAD_ERROR   (-6)

Thread creation failed.

Remarks
Please check usage of system resource (e.g. thread). And please call API after release system resources.

◆ AILIA_LLM_STATUS_UNIMPLEMENTED

#define AILIA_LLM_STATUS_UNIMPLEMENTED   (-15)

Unimplemented error.

Remarks
The called API are not available on current environment. Please contact support desk that described on document.

Typedef Documentation

◆ AILIALLMChatMessage

◆ 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.

◆ AILIALLMMultimodalChatMessage

Multimodal chat message with media attachments.

Function Documentation

◆ ailiaLLMCreate()

AILIA_LLM_API int ailiaLLMCreate ( struct AILIALLM **  llm)

Creates a LLM instance.

Parameters
llmA pointer to the LLM instance pointer
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Creates a LLM instance.

Remarks
When using QNN, the LLM instance must be destroyed by calling ailiaLLMDestroy on the same thread that called this function.

◆ ailiaLLMDestroy()

AILIA_LLM_API void ailiaLLMDestroy ( struct AILIALLM *  llm)

It destroys the LLM instance.

Parameters
llmA LLM instance pointer
Remarks
When using QNN, this function must be called on the same thread that called ailiaLLMCreate. When using QNN, moving only the release operation to another thread, such as the UI thread, may cause QnnDevice_create failed: code 14001 when QNN is subsequently used on a different thread.

◆ ailiaLLMGenerate()

AILIA_LLM_API int ailiaLLMGenerate ( struct AILIALLM *  llm,
unsigned int *  done 
)

Perform generate.

Parameters
llmA LLM instance pointer
doneGeneration complete?
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

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.

◆ ailiaLLMGetBackendCount()

AILIA_LLM_API int ailiaLLMGetBackendCount ( unsigned int *  env_count)

Gets the number of available computational environments (CPU, GPU, HTP (QNN)).

Parameters
env_countThe storage location of the number of computational environment information
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetBackendDeviceName()

AILIA_LLM_API int ailiaLLMGetBackendDeviceName ( const char **  name,
unsigned int  env_idx 
)

Gets the device name for a computational environment.

Parameters
nameStorage for the device name (valid while the library remains loaded)
env_idxIndex enumerated by ailiaLLMGetBackendCount()
Returns
AILIA_LLM_STATUS_SUCCESS on success.

◆ ailiaLLMGetBackendName()

AILIA_LLM_API int ailiaLLMGetBackendName ( const char **  env,
unsigned int  env_idx 
)

Gets the list of computational environments.

Parameters
envStorage for the environment name (valid while the library remains loaded)
env_idxThe index of the computational environment information (0 to ailiaLLMGetBackendCount() -1)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetContextSize()

AILIA_LLM_API int ailiaLLMGetContextSize ( struct AILIALLM *  llm,
unsigned int *  context_size 
)

Gets the size of context.

Parameters
llmA LLM instance pointer
lenThe length of context
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetDeltaText()

AILIA_LLM_API int ailiaLLMGetDeltaText ( struct AILIALLM *  llm,
char *  text,
unsigned int  buf_size 
)

Gets the decoded text.

Parameters
llmA LLM instance pointer
textText(UTF8)
buf_sizeBuffer size
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

If ailiaLLMGenerate() is not run at all, the function returns AILIA_LLM_STATUS_INVALID_STATE .

◆ ailiaLLMGetDeltaTextSize()

AILIA_LLM_API int ailiaLLMGetDeltaTextSize ( struct AILIALLM *  llm,
unsigned int *  buf_size 
)

Gets the size of text. (Include null)

Parameters
llmA LLM instance pointer
buf_sizeThe length of text
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMGetErrorDetail()

AILIA_LLM_API const char* ailiaLLMGetErrorDetail ( struct AILIALLM *  llm)

Returns details of the last model-open, prompt-setup or generation error.

Returns
Object-owned UTF-8 string, empty after a successful operation.
Remarks
Valid until the next model-open, prompt-setup, generation call or Destroy. QNN failures include the original code, failing API and object name when available. Concurrent calls on the same object are not supported. A null handle returns a static diagnostic string. This getter does not load the QNN plug-in.

◆ ailiaLLMGetGeneratedTokenCount()

AILIA_LLM_API int ailiaLLMGetGeneratedTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt 
)

Gets the count of prompt token.

Parameters
llmA LLM instance pointer
cntThe count of generated token
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

It can be called after calling ailiaLLMGenerate.

◆ ailiaLLMGetMultimodalCapabilities()

AILIA_LLM_API int ailiaLLMGetMultimodalCapabilities ( struct AILIALLM *  llm,
unsigned int *  vision_support,
unsigned int *  audio_support 
)

Check if multimodal features are supported.

Parameters
llmA LLM instance pointer
vision_supportWhether image input (VLM) is supported
audio_supportWhether audio input (ALM) is supported
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Can be called after ailiaLLMOpenMultimodalProjectorFile.

◆ ailiaLLMGetPromptTokenCount()

AILIA_LLM_API int ailiaLLMGetPromptTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt 
)

Gets the count of prompt token.

Parameters
llmA LLM instance pointer
cntThe count of prompt token
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

It can be called after calling ailiaLLMSetPrompt.

◆ ailiaLLMGetQNNModelName()

AILIA_LLM_API int ailiaLLMGetQNNModelName ( const char **  model_name)

Gets the QNN model name for the current device.

Parameters
model_nameStorage for the QNN model name (for example, "sm8475"). The library owns the returned string; the caller must not free it.
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS, or an error code otherwise.

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.

◆ ailiaLLMGetResponseJson()

AILIA_LLM_API int ailiaLLMGetResponseJson ( struct AILIALLM *  llm,
char *  json,
unsigned int  buf_size 
)

Gets structured assistant JSON from output accumulated inside the SDK.

Parameters
llmLLM instance
jsonUTF-8 output buffer
buf_sizeBuffer 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.

◆ ailiaLLMGetResponseJsonSize()

AILIA_LLM_API int ailiaLLMGetResponseJsonSize ( struct AILIALLM *  llm,
unsigned int *  buf_size 
)

Gets the buffered assistant JSON size, including the terminating null.

Parameters
llmLLM instance
buf_sizeRequired buffer size

Same parsing rules and errors as ailiaLLMGetResponseJson.

◆ ailiaLLMGetTokenCount()

AILIA_LLM_API int ailiaLLMGetTokenCount ( struct AILIALLM *  llm,
unsigned int *  cnt,
const char *  text 
)

Gets the count of token.

Parameters
llmA LLM instance pointer
cntThe count of token
textText(UTF8)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

◆ ailiaLLMOpenModelFileA()

AILIA_LLM_API int ailiaLLMOpenModelFileA ( struct AILIALLM *  llm,
const char *  path,
unsigned int  n_ctx 
)

Open model file.

Parameters
llmA pointer to the LLM instance pointer
pathPath to a GGUF or self-contained .qnn model
n_ctxContext length for model (0 is model default)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

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.

◆ ailiaLLMOpenModelFileW()

AILIA_LLM_API int ailiaLLMOpenModelFileW ( struct AILIALLM *  llm,
const wchar_t *  path,
unsigned int  n_ctx 
)

◆ ailiaLLMOpenMtpModelFileA()

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.

Parameters
llmLLM instance
pathPath to a matching Gemma 4 assistant GGUF, or assistant .qnn for a QNN target (A: UTF-8, W: UTF-16)
n_draftNumber 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.

◆ ailiaLLMOpenMtpModelFileW()

AILIA_LLM_API int ailiaLLMOpenMtpModelFileW ( struct AILIALLM *  llm,
const wchar_t *  path,
unsigned int  n_draft 
)

◆ ailiaLLMOpenMultimodalProjectorFileA()

AILIA_LLM_API int ailiaLLMOpenMultimodalProjectorFileA ( struct AILIALLM *  llm,
const char *  mmproj_path 
)

Load multimodal projector file.

Parameters
llmA LLM instance pointer
mmproj_pathPath to an MMPROJ GGUF or self-contained ailia QNN projector (.qnn)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

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.

◆ ailiaLLMOpenMultimodalProjectorFileW()

AILIA_LLM_API int ailiaLLMOpenMultimodalProjectorFileW ( struct AILIALLM *  llm,
const wchar_t *  mmproj_path 
)

◆ ailiaLLMSetBackend()

AILIA_LLM_API int ailiaLLMSetBackend ( struct AILIALLM *  llm,
unsigned int  backend_idx 
)

Selects the backend for this instance before opening a model.

Parameters
llmLLM instance
backend_idxDevice index returned by ailiaLLMGetBackendCount/Name
Returns
AILIA_LLM_STATUS_SUCCESS on success, or AILIA_LLM_STATUS_INVALID_STATE after a model is opened.

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.

◆ ailiaLLMSetMultimodalPrompt()

AILIA_LLM_API int ailiaLLMSetMultimodalPrompt ( struct AILIALLM *  llm,
const AILIALLMMultimodalChatMessage *  message,
unsigned int  message_cnt 
)

Set multimodal prompt.

Parameters
llmA LLM instance pointer
messageArray of multimodal messages
message_cntNumber of messages
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

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.

Note
With tools configured, returns INVALID_STATE. Use ailiaLLMSetPromptJson for tool use with media.

◆ ailiaLLMSetNumThreads()

AILIA_LLM_API int ailiaLLMSetNumThreads ( struct AILIALLM *  llm,
unsigned int  n_threads 
)

Sets the CPU thread count before opening the model.

Parameters
llmLLM instance
n_threadsThread count (0 selects automatically).
Returns
AILIA_LLM_STATUS_SUCCESS on success, or AILIA_LLM_STATUS_INVALID_STATE after a model is opened.

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.

◆ ailiaLLMSetPrompt()

AILIA_LLM_API int ailiaLLMSetPrompt ( struct AILIALLM *  llm,
const AILIALLMChatMessage *  message,
unsigned int  message_cnt 
)

Set the prompt.

Parameters
llmA pointer to the LLM instance pointer
messageArray of messages
message_cntNumber of messages
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

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.

◆ ailiaLLMSetPromptJson()

AILIA_LLM_API int ailiaLLMSetPromptJson ( struct AILIALLM *  llm,
const char *  messages_json 
)

Sets a prompt from structured JSON conversation history.

Parameters
llmA LLM instance pointer
messages_jsonNonempty 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.

Returns
SUCCESS on success; INVALID_ARGUMENT for invalid JSON, types, ID associations or arguments; INVALID_STATE if no model is loaded. Other prompt preparation errors are the same as ailiaLLMSetPrompt.

◆ ailiaLLMSetSamplingParams()

AILIA_LLM_API int ailiaLLMSetSamplingParams ( struct AILIALLM *  llm,
unsigned int  top_k,
float  top_p,
float  temp,
unsigned int  dist 
)

Set the sampling parameter.

Parameters
llmA pointer to the LLM instance pointer
top_kSampling probability value's top number, default 40
top_pSampling probability value range, default 0.9 (0.9 to 1.0)
tempTemperature parameter, default 0.4
distSeed, default 1234
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Set LLM sampling parameters. Must be run before ailiaLLMSetPrompt.

◆ ailiaLLMSetThinking()

AILIA_LLM_API int ailiaLLMSetThinking ( struct AILIALLM *  llm,
unsigned int  enable 
)

Enable or disable thinking (reasoning output).

Parameters
llmA pointer to the LLM instance pointer
enable0 to disable, non-zero to enable (default: disabled)
Returns
If this function is successful, it returns AILIA_LLM_STATUS_SUCCESS , or an error code otherwise.

Controls whether thinking models (e.g. Gemma4) output their reasoning process. Must be called before ailiaLLMSetPrompt.

◆ ailiaLLMSetTools()

AILIA_LLM_API int ailiaLLMSetTools ( struct AILIALLM *  llm,
const char *  tools_json 
)

Sets OpenAI-compatible tool definitions. NULL, empty string or array clears tools.

Parameters
llmLLM instance
tools_jsonUTF-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.