ailia_llm  1.5.1.0
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
ailiaLLM.AiliaLLMModel Class Reference
Inheritance diagram for ailiaLLM.AiliaLLMModel:
Inheritance graph
[legend]
Collaboration diagram for ailiaLLM.AiliaLLMModel:
Collaboration graph
[legend]

Public Member Functions

string GetErrorDetail ()
 Gets details of the last error. More...
 
bool Create ()
 Create a instance. More...
 
bool SetNumThreads (uint n_threads)
 Sets the CPU thread count before Open. More...
 
bool SetBackend (uint backend_idx)
 Selects a CPU, GPU, or HTP (QNN) backend before Open. More...
 
bool Open (string model_path, uint n_ctx=0)
 Open a model. More...
 
bool OpenMtpModel (string model_path, uint n_draft)
 Opens an MTP assistant and enables draft/target verification decoding. More...
 
bool OpenMultimodalProjector (string mmproj_path)
 Open a multimodal projector file. More...
 
bool GetMultimodalCapabilities (ref bool vision_support, ref bool audio_support)
 Check if multimodal features are supported. More...
 
virtual void Close ()
 Destroys instance. More...
 
virtual void Dispose ()
 Release resources. More...
 
bool SetSamplingParam (uint top_k, float top_p, float temp, uint dist)
 Set the sampling parameter. More...
 
bool SetThinking (bool enable)
 Enable or disable thinking (reasoning output). More...
 
bool SetPrompt (List< AiliaLLMChatMessage > messages)
 Set prompt messages. More...
 
bool SetPromptJson (string messagesJson)
 Sets structured JSON conversation history. More...
 
string GetResponseJson ()
 Gets the buffered assistant response as JSON without collecting deltas. More...
 
bool SetTools (string tools_json)
 Sets the available tools in JSON format. More...
 
bool SetMultimodalPrompt (List< AiliaLLMMultimodalChatMessage > messages)
 Set multimodal prompt messages. More...
 
bool Generate (ref bool done)
 Perform encode. More...
 
string GetDeltaText ()
 Set prompt messages. More...
 
bool ContextFull ()
 Check if the context length limit has been reached. More...
 
uint PromptTokenCount ()
 Gets the number of prompt tokens. More...
 
uint GeneratedTokenCount ()
 Gets the number of tokens generated. More...
 

Static Public Member Functions

static List< AiliaLLMBackendInfo > GetBackendList ()
 Gets the available backends. More...
 
static string GetQNNModelName ()
 Gets the device QNN artifact stem without creating a model. More...
 

Protected Member Functions

virtual void Dispose (bool disposing)
 

Member Function Documentation

◆ Close()

virtual void ailiaLLM.AiliaLLMModel.Close ( )
inlinevirtual

Destroys instance.

Destroys and initializes the instance.

Remarks
When using QNN, this method must be called on the same thread that called Create. When using QNN, moving only resource release to another thread, such as the UI thread, may cause QnnDevice_create failed: code 14001 when QNN is subsequently used on a different thread.
367  {
368  if (net != IntPtr.Zero){
369  AiliaLLM.ailiaLLMDestroy(net);
370  net = IntPtr.Zero;
371  }
372  }

◆ ContextFull()

bool ailiaLLM.AiliaLLMModel.ContextFull ( )
inline

Check if the context length limit has been reached.

Returns
True if the limit is reached, false otherwise.
885  {
886  return context_full;
887  }

◆ Create()

bool ailiaLLM.AiliaLLMModel.Create ( )
inline

Create a instance.

Returns
If this function is successful, it returns true , or false otherwise.
Remarks
When using QNN, call Close or Dispose on the same thread that called this function to release the instance.
163  {
164  if (net != IntPtr.Zero){
165  Close();
166  }
167 
168  int status = AiliaLLM.ailiaLLMCreate(ref net);
169  if (status != 0){
170  if (logging)
171  {
172  Debug.Log("ailiaLLMCreate failed " + status);
173  }
174  return false;
175  }
176 
177  return true;
178  }
virtual void Close()
Destroys instance.
Definition: AiliaLLMModel.cs:366

◆ Dispose() [1/2]

virtual void ailiaLLM.AiliaLLMModel.Dispose ( )
inlinevirtual

Release resources.

Remarks
When using QNN, this method must be called on the same thread that called Create. When using QNN, explicitly call Close or Dispose instead of relying on garbage collection, because finalization does not guarantee execution on that thread.
388  {
389  Dispose(true);
390  }
virtual void Dispose()
Release resources.
Definition: AiliaLLMModel.cs:387

◆ Dispose() [2/2]

virtual void ailiaLLM.AiliaLLMModel.Dispose ( bool  disposing)
inlineprotectedvirtual
393  {
394  if (disposing){
395  // release managed resource
396  }
397  Close(); // release unmanaged resource
398  }

◆ Generate()

bool ailiaLLM.AiliaLLMModel.Generate ( ref bool  done)
inline

Perform encode.

Parameters
doneIs done generation
Returns
If this function is successful, it returns array of tokens , or empty array otherwise.
802  {
803  uint done_uint = 0;
804  int status = AiliaLLM.ailiaLLMGenerate(net, ref done_uint);
805  context_full = false;
806  done = (done_uint == 1);
807  if (status != 0){
808  if (logging)
809  {
810  Debug.Log("ailiaLLMGenerate failed " + status);
811  }
812  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
813  context_full = true;
814  }
815  return false;
816  }
817  return true;
818  }

◆ GeneratedTokenCount()

uint ailiaLLM.AiliaLLMModel.GeneratedTokenCount ( )
inline

Gets the number of tokens generated.

Returns
Number of tokens generated. 0 if failed.
927  {
928  if (net == IntPtr.Zero){
929  return 0;
930  }
931  uint count = 0;
932  int status = AiliaLLM.ailiaLLMGetGeneratedTokenCount(net, ref count);
933  if (status != 0){
934  if (logging)
935  {
936  Debug.Log("ailiaLLMGetGeneratedTokenCount failed " + status);
937  }
938  return 0;
939  }
940  return count;
941  }

◆ GetBackendList()

static List<AiliaLLMBackendInfo> ailiaLLM.AiliaLLMModel.GetBackendList ( )
inlinestatic

Gets the available backends.

Returns
Backend names, device names, and indices for selection
Exceptions
InvalidOperationExceptionIf backend enumeration fails

This can be called before creating a model. To select a backend, pass its idx to SetBackend after Create and before Open.

71  {
72  uint count = 0;
73  int status = AiliaLLM.ailiaLLMGetBackendCount(ref count);
74  if (status != AiliaLLM.AILIA_LLM_STATUS_SUCCESS) {
75  throw new InvalidOperationException("Failed to get backend count. Status: " + status);
76  }
77 
78  var backends = new List<AiliaLLMBackendInfo>();
79  for (uint i = 0; i < count; i++) {
80  IntPtr backendName = IntPtr.Zero;
81  status = AiliaLLM.ailiaLLMGetBackendName(ref backendName, i);
82  if (status != AiliaLLM.AILIA_LLM_STATUS_SUCCESS || backendName == IntPtr.Zero) {
83  throw new InvalidOperationException("Failed to get backend name at index " + i + ". Status: " + status);
84  }
85 
86  IntPtr deviceName = IntPtr.Zero;
87  status = AiliaLLM.ailiaLLMGetBackendDeviceName(ref deviceName, i);
88  if (status != AiliaLLM.AILIA_LLM_STATUS_SUCCESS || deviceName == IntPtr.Zero) {
89  throw new InvalidOperationException("Failed to get backend device name at index " + i + ". Status: " + status);
90  }
91 
92  backends.Add(new AiliaLLMBackendInfo {
93  backend_name = Marshal.PtrToStringAnsi(backendName),
94  device_name = Marshal.PtrToStringAnsi(deviceName),
95  idx = i
96  });
97  }
98  return backends;
99  }

◆ GetDeltaText()

string ailiaLLM.AiliaLLMModel.GetDeltaText ( )
inline

Set prompt messages.

Returns
It returns text.
832  {
833  uint len = 0;
834  int status = AiliaLLM.ailiaLLMGetDeltaTextSize(net, ref len);
835  if (status != 0){
836  if (logging)
837  {
838  Debug.Log("ailiaLLMGetDeltaTextSize failed " + status);
839  }
840  return "";
841  }
842  byte[] text = new byte [len];
843  GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
844  IntPtr output = handle.AddrOfPinnedObject();
845  status = AiliaLLM.ailiaLLMGetDeltaText(net, output, len);
846  handle.Free();
847  if (status != 0){
848  if (logging)
849  {
850  Debug.Log("ailiaLLMGetDeltaText failed " + status);
851  }
852  return "";
853  }
854 
855  byte[] new_buf = new byte [buf.Length + len - 1];
856  for (int i = 0; i < buf.Length; i++){
857  new_buf[i] = buf[i];
858  }
859  for (int i = 0; i < len - 1; i++){ // NULLの削除
860  new_buf[buf.Length + i] = text[i];
861  }
862  buf = new_buf;
863 
864  string decoded_text = System.Text.Encoding.UTF8.GetString(buf); // Unicode Decode Errorは発生しない
865  string delta_text = "";
866  if (decoded_text.Length > before_text.Length){
867  delta_text = decoded_text.Substring(before_text.Length);
868  }
869  before_text = decoded_text;
870  return delta_text;
871  }

◆ GetErrorDetail()

string ailiaLLM.AiliaLLMModel.GetErrorDetail ( )
inline

Gets details of the last error.

Returns
Error details, or an empty string if unavailable.
135  {
136  IntPtr detail = AiliaLLM.ailiaLLMGetErrorDetail(net);
137  return detail == IntPtr.Zero ? "" : Marshal.PtrToStringAnsi(detail);
138  }

◆ GetMultimodalCapabilities()

bool ailiaLLM.AiliaLLMModel.GetMultimodalCapabilities ( ref bool  vision_support,
ref bool  audio_support 
)
inline

Check if multimodal features are supported.

Parameters
vision_supportWhether image input (VLM) is supported
audio_supportWhether audio input (ALM) is supported
Returns
If this function is successful, it returns true , or false otherwise.
327  {
328  uint vision_uint = 0;
329  uint audio_uint = 0;
330  int status = AiliaLLM.ailiaLLMGetMultimodalCapabilities(net, ref vision_uint, ref audio_uint);
331  if (status != 0){
332  if (logging)
333  {
334  Debug.Log("ailiaLLMGetMultimodalCapabilities failed " + status);
335  }
336  return false;
337  }
338  vision_support = (vision_uint == 1);
339  audio_support = (audio_uint == 1);
340  return true;
341  }

◆ GetQNNModelName()

static string ailiaLLM.AiliaLLMModel.GetQNNModelName ( )
inlinestatic

Gets the device QNN artifact stem without creating a model.

Returns
QNN artifact stem
Exceptions
InvalidOperationExceptionIf QNN is unavailable or the device is unsupported

QNN is supported on Android arm64 and Windows ARM64.

114  {
115  IntPtr name = IntPtr.Zero;
116  int status = AiliaLLM.ailiaLLMGetQNNModelName(ref name);
117  if (status != AiliaLLM.AILIA_LLM_STATUS_SUCCESS || name == IntPtr.Zero) {
118  throw new InvalidOperationException("Failed to get QNN model name. Status: " + status);
119  }
120  // Model names are ASCII. Copy the library-owned string; never free it.
121  return Marshal.PtrToStringAnsi(name);
122  }

◆ GetResponseJson()

string ailiaLLM.AiliaLLMModel.GetResponseJson ( )
inline

Gets the buffered assistant response as JSON without collecting deltas.

Returns
Assistant response in JSON format, or an empty string on error.
585  {
586  uint size = 0;
587  int status = AiliaLLM.ailiaLLMGetResponseJsonSize(net, ref size);
588  if (status != 0) { if (logging) Debug.Log("GetResponseJsonSize failed " + status); return ""; }
589  var bytes = new byte[size];
590  var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
591  try { status = AiliaLLM.ailiaLLMGetResponseJson(net, handle.AddrOfPinnedObject(), size); }
592  finally { handle.Free(); }
593  if (status != 0) { if (logging) Debug.Log("GetResponseJson failed " + status); return ""; }
594  return System.Text.Encoding.UTF8.GetString(bytes, 0, (int)size - 1);
595  }

◆ Open()

bool ailiaLLM.AiliaLLMModel.Open ( string  model_path,
uint  n_ctx = 0 
)
inline

Open a model.

Parameters
model_pathPath for model
n_ctxContext length for model (0 is model default)
Returns
If this function is successful, it returns true , or false otherwise.
237  {
238  if (net == IntPtr.Zero){
239  return false;
240  }
241 
242  int status = 0;
243 
244  status = AiliaLLM.ailiaLLMOpenModelFile(net, model_path, n_ctx);
245  if (status != 0){
246  if (logging)
247  {
248  Debug.Log("ailiaLLMOpenModelFile failed " + status);
249  }
250  return false;
251  }
252 
253  return true;
254  }

◆ OpenMtpModel()

bool ailiaLLM.AiliaLLMModel.OpenMtpModel ( string  model_path,
uint  n_draft 
)
inline

Opens an MTP assistant and enables draft/target verification decoding.

Parameters
model_pathPath to a matching Gemma 4 assistant GGUF, or assistant .qnn for a QNN target.
n_draftNumber of draft tokens (at least 1). QNN supports up to 3.
Returns
True on success, or false on failure.

Call once after Open and before setting a prompt. Supports CPU/GPU Gemma 4 GGUF and QNN text generation.

271  {
272  if (net == IntPtr.Zero) return false;
273  int status = AiliaLLM.ailiaLLMOpenMtpModelFile(net, model_path, n_draft);
274  if (status != 0 && logging) Debug.Log("ailiaLLMOpenMtpModelFile failed " + status);
275  return status == AiliaLLM.AILIA_LLM_STATUS_SUCCESS;
276  }

◆ OpenMultimodalProjector()

bool ailiaLLM.AiliaLLMModel.OpenMultimodalProjector ( string  mmproj_path)
inline

Open a multimodal projector file.

Parameters
mmproj_pathPath to an MMPROJ GGUF or self-contained ailia QNN projector (.qnn)
Returns
If this function is successful, it returns true , or false otherwise.

Image input (VLM) requires a vision-capable projector, while audio input (ALM) requires an audio-capable projector. QNN projectors support Android arm64 and Windows ARM64.

295  {
296  if (net == IntPtr.Zero){
297  return false;
298  }
299 
300  int status = AiliaLLM.ailiaLLMOpenMultimodalProjectorFile(net, mmproj_path);
301  if (status != 0){
302  if (logging)
303  {
304  Debug.Log("ailiaLLMOpenMultimodalProjectorFile failed " + status);
305  }
306  return false;
307  }
308 
309  return true;
310  }

◆ PromptTokenCount()

uint ailiaLLM.AiliaLLMModel.PromptTokenCount ( )
inline

Gets the number of prompt tokens.

Returns
Number of prompt tokens. 0 if failed.
900  {
901  if (net == IntPtr.Zero){
902  return 0;
903  }
904  uint count = 0;
905  int status = AiliaLLM.ailiaLLMGetPromptTokenCount(net, ref count);
906  if (status != 0){
907  if (logging)
908  {
909  Debug.Log("ailiaLLMGetPromptTokenCount failed " + status);
910  }
911  return 0;
912  }
913  return count;
914  }

◆ SetBackend()

bool ailiaLLM.AiliaLLMModel.SetBackend ( uint  backend_idx)
inline

Selects a CPU, GPU, or HTP (QNN) backend before Open.

Parameters
backend_idxBackend index obtained from GetBackendList.
Returns
True on success, or false on failure.

Without a selection, .qnn selects HTP automatically. An explicit CPU/GPU selection rejects .qnn; an explicit HTP selection rejects GGUF.

215  {
216  if (net == IntPtr.Zero){
217  return false;
218  }
219  return AiliaLLM.ailiaLLMSetBackend(net, backend_idx) == AiliaLLM.AILIA_LLM_STATUS_SUCCESS;
220  }

◆ SetMultimodalPrompt()

bool ailiaLLM.AiliaLLMModel.SetMultimodalPrompt ( List< AiliaLLMMultimodalChatMessage >  messages)
inline

Set multimodal prompt messages.

Parameters
messagesMultimodal prompt messages
Returns
If this function is successful, it returns true , or false otherwise.
643  {
644  List<GCHandle> handle_list = new List<GCHandle>();
645  int len = messages.Count;
646  byte[][] role_text_list = new byte [len][];
647  byte[][] content_text_list = new byte [len][];
648  AiliaLLM.AILIALLMMultimodalChatMessage [] message_list = new AiliaLLM.AILIALLMMultimodalChatMessage[len];
649 
650  // Prepare media data arrays
651  List<byte[][]> media_type_lists = new List<byte[][]>();
652  List<byte[][]> media_path_lists = new List<byte[][]>();
653  List<AiliaLLM.AILIALLMMediaData[]> media_data_arrays = new List<AiliaLLM.AILIALLMMediaData[]>();
654 
655  for (int i = 0; i < len; i++){
656  AiliaLLM.AILIALLMMultimodalChatMessage message = new AiliaLLM.AILIALLMMultimodalChatMessage();
657 
658  // Set role and content
659  role_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].role+"\u0000");
660  GCHandle role_handle = GCHandle.Alloc(role_text_list[i], GCHandleType.Pinned);
661  IntPtr role_input = role_handle.AddrOfPinnedObject();
662 
663  content_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].content+"\u0000");
664  GCHandle content_handle = GCHandle.Alloc(content_text_list[i], GCHandleType.Pinned);
665  IntPtr content_input = content_handle.AddrOfPinnedObject();
666 
667  message.role = role_input;
668  message.content = content_input;
669 
670  handle_list.Add(role_handle);
671  handle_list.Add(content_handle);
672 
673  // Handle media data
674  if (messages[i].media_data != null && messages[i].media_data.Count > 0){
675  int media_count = messages[i].media_data.Count;
676  message.media_count = (uint)media_count;
677 
678  byte[][] media_type_list = new byte[media_count][];
679  byte[][] media_path_list = new byte[media_count][];
680  AiliaLLM.AILIALLMMediaData[] media_array = new AiliaLLM.AILIALLMMediaData[media_count];
681 
682  for (int j = 0; j < media_count; j++){
683  AiliaLLM.AILIALLMMediaData media = new AiliaLLM.AILIALLMMediaData();
684 
685  // Media type
686  media_type_list[j] = System.Text.Encoding.UTF8.GetBytes(messages[i].media_data[j].media_type+"\u0000");
687  GCHandle type_handle = GCHandle.Alloc(media_type_list[j], GCHandleType.Pinned);
688  media.media_type = type_handle.AddrOfPinnedObject();
689  handle_list.Add(type_handle);
690 
691  // File path
692  if (!string.IsNullOrEmpty(messages[i].media_data[j].file_path)){
693  media_path_list[j] = System.Text.Encoding.UTF8.GetBytes(messages[i].media_data[j].file_path+"\u0000");
694  GCHandle path_handle = GCHandle.Alloc(media_path_list[j], GCHandleType.Pinned);
695  media.file_path = path_handle.AddrOfPinnedObject();
696  handle_list.Add(path_handle);
697  } else {
698  media.file_path = IntPtr.Zero;
699  }
700 
701  // Raw data
702  if (messages[i].media_data[j].data != null && messages[i].media_data[j].data.Length > 0){
703  GCHandle data_handle = GCHandle.Alloc(messages[i].media_data[j].data, GCHandleType.Pinned);
704  media.data = data_handle.AddrOfPinnedObject();
705  media.data_size = (uint)messages[i].media_data[j].data.Length;
706  handle_list.Add(data_handle);
707  } else {
708  media.data = IntPtr.Zero;
709  media.data_size = 0;
710  }
711 
712  media.width = messages[i].media_data[j].width;
713  media.height = messages[i].media_data[j].height;
714 
715  media_array[j] = media;
716  }
717 
718  media_type_lists.Add(media_type_list);
719  media_path_lists.Add(media_path_list);
720  media_data_arrays.Add(media_array);
721 
722  // Allocate and set media_data pointer
723  int media_size = Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMediaData)) * media_count;
724  IntPtr media_ptr = Marshal.AllocHGlobal(media_size);
725 
726  for (int j = 0; j < media_count; j++){
727  IntPtr offset = new IntPtr(media_ptr.ToInt64() + j * Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMediaData)));
728  Marshal.StructureToPtr(media_array[j], offset, false);
729  }
730 
731  message.media_data = media_ptr;
732  } else {
733  message.media_count = 0;
734  message.media_data = IntPtr.Zero;
735  }
736 
737  message_list[i] = message;
738  }
739 
740  int size = Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMultimodalChatMessage)) * message_list.Length;
741  IntPtr ptr = Marshal.AllocHGlobal(size);
742 
743  int status = 0;
744 
745  try
746  {
747  for (int i = 0; i < message_list.Length; i++)
748  {
749  IntPtr offset = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMultimodalChatMessage)));
750  Marshal.StructureToPtr(message_list[i], offset, false);
751  }
752 
753  status = AiliaLLM.ailiaLLMSetMultimodalPrompt(net, ptr, (uint)len);
754  }
755  finally
756  {
757  // Free allocated memory
758  Marshal.FreeHGlobal(ptr);
759  for (int i = 0; i < message_list.Length; i++){
760  if (message_list[i].media_data != IntPtr.Zero){
761  Marshal.FreeHGlobal(message_list[i].media_data);
762  }
763  }
764  }
765 
766  for (int i = 0; i < handle_list.Count; i++){
767  handle_list[i].Free();
768  }
769 
770  context_full = false;
771  buf = new byte[0];
772  before_text = "";
773 
774  if (status != 0){
775  if (logging)
776  {
777  Debug.Log("ailiaLLMSetMultimodalPrompt failed " + status);
778  }
779  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
780  context_full = true;
781  }
782  return false;
783  }
784 
785  return true;
786  }

◆ SetNumThreads()

bool ailiaLLM.AiliaLLMModel.SetNumThreads ( uint  n_threads)
inline

Sets the CPU thread count before Open.

Parameters
n_threadsThread count (0 selects automatically).
Returns
True on success, or false on failure.

The default is 0 when this method 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.

195  {
196  if (net == IntPtr.Zero){
197  return false;
198  }
199  return AiliaLLM.ailiaLLMSetNumThreads(net, n_threads) == AiliaLLM.AILIA_LLM_STATUS_SUCCESS;
200  }

◆ SetPrompt()

bool ailiaLLM.AiliaLLMModel.SetPrompt ( List< AiliaLLMChatMessage >  messages)
inline

Set prompt messages.

Parameters
messagesPrompt messages
Returns
If this function is successful, it returns true , or false otherwise.
483  {
484  List<GCHandle> handle_list = new List<GCHandle>();
485  int len = messages.Count;
486  byte[][] role_text_list = new byte [len][];
487  byte[][] conntent_text_list = new byte [len][];
488  AiliaLLM.AILIAChatMessage [] message_list = new AiliaLLM.AILIAChatMessage[len];
489  for (int i = 0; i< len; i++){
490  AiliaLLM.AILIAChatMessage message = new AiliaLLM.AILIAChatMessage();
491 
492  role_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].role+"\u0000");
493  GCHandle role_handle = GCHandle.Alloc(role_text_list[i], GCHandleType.Pinned);
494  IntPtr role_input = role_handle.AddrOfPinnedObject();
495 
496  conntent_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].content+"\u0000");
497  GCHandle content_handle = GCHandle.Alloc(conntent_text_list[i], GCHandleType.Pinned);
498  IntPtr content_input = content_handle.AddrOfPinnedObject();
499 
500  message.role = role_input;
501  message.content = content_input;
502  message_list[i] = message;
503 
504  handle_list.Add(role_handle);
505  handle_list.Add(content_handle);
506  }
507 
508  int size = Marshal.SizeOf(typeof(AiliaLLM.AILIAChatMessage)) * message_list.Length;
509  IntPtr ptr = Marshal.AllocHGlobal(size);
510 
511  int status = 0;
512 
513  try
514  {
515  for (int i = 0; i < message_list.Length; i++)
516  {
517  IntPtr offset = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(AiliaLLM.AILIAChatMessage)));
518  Marshal.StructureToPtr(message_list[i], offset, false);
519  }
520 
521  status = AiliaLLM.ailiaLLMSetPrompt(net, ptr, (uint)len);
522  }
523  finally
524  {
525  Marshal.FreeHGlobal(ptr);
526  }
527 
528  for (int i = 0; i < handle_list.Count; i++){
529  handle_list[i].Free();
530  }
531 
532  context_full = false;
533  buf = new byte[0];
534  before_text = "";
535 
536  if (status != 0){
537  if (logging)
538  {
539  Debug.Log("ailiaLLMSetPrompt failed " + status);
540  }
541  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
542  context_full = true;
543  }
544  return false;
545  }
546 
547  return true;
548  }

◆ SetPromptJson()

bool ailiaLLM.AiliaLLMModel.SetPromptJson ( string  messagesJson)
inline

Sets structured JSON conversation history.

Parameters
messagesJsonConversation history in JSON format.
Returns
True on success, or false on failure.

Required when using tools. Media inputs require a compatible projector.

563  {
564  if (messagesJson == null) return false;
565  var bytes = System.Text.Encoding.UTF8.GetBytes(messagesJson + "\u0000");
566  var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
567  int status;
568  try { status = AiliaLLM.ailiaLLMSetPromptJson(net, handle.AddrOfPinnedObject()); }
569  finally { handle.Free(); }
570  context_full = status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL;
571  if (status != 0) { if (logging) Debug.Log("SetPromptJson failed " + status); return false; }
572  buf = new byte[0]; before_text = "";
573  return true;
574  }

◆ SetSamplingParam()

bool ailiaLLM.AiliaLLMModel.SetSamplingParam ( uint  top_k,
float  top_p,
float  temp,
uint  dist 
)
inline

Set the sampling parameter.

Parameters
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 true , or false otherwise.
427  {
428  int status = AiliaLLM.ailiaLLMSetSamplingParams(net, top_k, top_p, temp, dist);
429  if (status != 0){
430  if (logging)
431  {
432  Debug.Log("ailiaLLMSetSamplingParams failed " + status);
433  }
434  return false;
435  }
436  return true;
437  }

◆ SetThinking()

bool ailiaLLM.AiliaLLMModel.SetThinking ( bool  enable)
inline

Enable or disable thinking (reasoning output).

Parameters
enabletrue to enable, false to disable (default: disabled)
Returns
If this function is successful, it returns true , or false otherwise.

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

457  {
458  int status = AiliaLLM.ailiaLLMSetThinking(net, enable ? 1u : 0u);
459  if (status != 0){
460  if (logging)
461  {
462  Debug.Log("ailiaLLMSetThinking failed " + status);
463  }
464  return false;
465  }
466  return true;
467  }

◆ SetTools()

bool ailiaLLM.AiliaLLMModel.SetTools ( string  tools_json)
inline

Sets the available tools in JSON format.

Parameters
tools_jsonTool definitions in JSON. Null or an empty string disables tools.
Returns
True on success, or false on failure.
609  {
610  int status = 0;
611  if (string.IsNullOrEmpty(tools_json)){
612  status = AiliaLLM.ailiaLLMSetTools(net, IntPtr.Zero);
613  } else {
614  byte[] tools_bytes = System.Text.Encoding.UTF8.GetBytes(tools_json + "\u0000");
615  GCHandle tools_handle = GCHandle.Alloc(tools_bytes, GCHandleType.Pinned);
616  status = AiliaLLM.ailiaLLMSetTools(net, tools_handle.AddrOfPinnedObject());
617  tools_handle.Free();
618  }
619  if (status != 0){
620  if (logging)
621  {
622  Debug.Log("ailiaLLMSetTools failed " + status);
623  }
624  return false;
625  }
626  return true;
627  }

The documentation for this class was generated from the following file: