|
ailia_llm
1.5.1.0
|
This is an example of using ailia LLM.
Use a vision-capable text model and its matching projector. Call ailiaLLMCreate, ailiaLLMOpenModelFileA, then ailiaLLMOpenMultimodalProjectorFileA; check vision_support with ailiaLLMGetMultimodalCapabilities. Each <__media__> marker must match one entry in media_data, in order.
Generate text with the same APIs as the basic example. Besides a file path, images accept encoded bytes (data / data_size, width and height zero) or raw RGB bytes (width * height * 3).
Use an audio-capable text model and its matching mmproj containing an audio encoder. A vision-only projector cannot process audio. Tasks and languages depend on the model. Audio synthesis/output is not supported.
ailiaLLMOpenModelFileA.ailiaLLMOpenMultimodalProjectorFileA.ailiaLLMGetMultimodalCapabilities reports nonzero audio support.ailiaLLMGenerate, ailiaLLMGetDeltaTextSize and ailiaLLMGetDeltaText as in the basic example. Check every return value; use ailiaLLMGetErrorDetail to inspect failures.Input is a WAV/MP3/FLAC file path or an encoded file buffer. For buffer input, set file_path=NULL, data and data_size; data takes precedence over a path. The size must fit an unsigned int. Keep the buffer and strings alive until the call returns. Set both width and height to zero. Raw PCM and live microphone streaming are not supported by this API. Media markers and array entries must match in count and order.
The same open APIs accept a precompiled text .qnn and an mmproj .qnn containing audio contexts and CPU weights. Source GGUF files are not required on the device. Use the correct SoC target, QNN libraries and matching base/plugin builds. Old vision-only packages do not acquire audio capability automatically.
Feature extraction and subsampling remain on CPU; all 12 Conformer blocks and the final projection execute in one HTP graph. Padding and history-preserving chunking support short/long inputs. Regenerate older linear audio packages with the current compiler. Mixed image/audio in one prompt is unsupported; use separate prompts. Audio prompts do not reuse previous-prompt KV. Audio tokens are causal and may cross AR256 boundaries, but the resulting token sequence must fit the model context. QNN-owned sparse linear weights cannot fall back to CPU.
With a QNN-enabled build, open a device-specific, precompiled text .qnn using ailiaLLMOpenModelFileA. For VLM/ALM, open its matching projector .qnn using ailiaLLMOpenMultimodalProjectorFileA. Unlike GGUF files, these packages are SoC-specific. Match the text/projector pair, QNN runtime and plug-in versions. Without an explicit backend selection, a .qnn package selects HTP (QNN). Explicit CPU/GPU selection rejects .qnn, and explicit HTP (QNN) rejects GGUF.
Skip the projector for text-only use. n_ctx=0 selects the package context length. Model-name lookup returns UNIMPLEMENTED for a non-QNN build or absent plug-in, and OTHER_ERROR on unsupported SoCs. Inspect failures with ailiaLLMGetErrorDetail.
Use ailiaLLMSetPromptJson from the first user message while tools are configured. The legacy ailiaLLMSetPrompt and ailiaLLMSetMultimodalPrompt return INVALID_STATE until tools are cleared. Define tools with ailiaLLMSetTools, set the JSON messages array, and call ailiaLLMGenerate until done. Deltas remain available through ailiaLLMGetDeltaText for previews; retrieving or concatenating them is optional. Retrieve assistant JSON with ailiaLLMGetResponseJsonSize / ailiaLLMGetResponseJson, append the object to history, execute complete calls, and append tool results with matching tool_call_id. Set the next JSON prompt to continue.
The SDK accumulates generated output. Reads do not consume it; a new prompt resets it.
For example, set the following tool definition and initial message. The application executes tools, validating the returned function name and arguments.
Include <stdlib.h> and pass a valid char **response to this example.
Content is not reparsed as tool syntax. Supply assistant thinking in reasoning_content. Arguments must be a string containing a JSON object. Call IDs/names must be nonempty; IDs are unique per assistant, but may be reused in later turns. Consecutive tool results match the preceding assistant by ID, not order. Unknown IDs, duplicate results and conflicting names are rejected. Tool content must be a string; serialize objects first.
Load a compatible projector with ailiaLLMOpenMultimodalProjectorFileA and use ordered user content parts:
Each image/audio part specifies exactly one of file_path or data. Data is standard padded Base64 containing encoded file bytes (JPEG/PNG, WAV/MP3/FLAC, etc.). No URL fetching, video or raw RGB/PCM is supported. Media works with or without tools and is re-evaluated each prompt. An unloaded/incompatible projector returns INVALID_STATE. Query image/audio support with ailiaLLMGetMultimodalCapabilities.
GetResponseJson rejects unfinished calls/thinking with PARSE_ERROR. It never repairs arguments or returns executable partial results. SetPromptJson rejects incomplete arguments, invalid JSON/types/ID associations and empty arrays with INVALID_ARGUMENT. If GetResponseJson returns PARSE_ERROR, do not append the failed assistant. Append a retry user message to the existing history; no partial content/reasoning retrieval is needed. Do not invent tool results.
After clearing tools with SetTools(NULL), SetPromptJson still accepts historical tool calls and results for summaries or final answers. Retrieve response JSON before clearing tools, which invalidates the output parser. Use finally to clear tools even when an exception occurs. An unset prompt or changed tools/thinking returns INVALID_STATE; before generation an initialized prompt returns an empty assistant. File failures return ERROR_FILE_API, invalid Base64 returns INVALID_ARGUMENT, and undecodable media buffers return ERROR_BUFFER_API.