ailia_llm  1.4.1.0
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

bool Create ()
 Create a instance. More...
 
bool Open (string model_path, uint n_ctx=0)
 Open a model. 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 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...
 

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.

184  {
185  if (net != IntPtr.Zero){
186  AiliaLLM.ailiaLLMDestroy(net);
187  net = IntPtr.Zero;
188  }
189  }

◆ ContextFull()

bool ailiaLLM.AiliaLLMModel.ContextFull ( )
inline

Check if the context length limit has been reached.

Returns
True if the limit is reached, false otherwise.
617  {
618  return context_full;
619  }

◆ Create()

bool ailiaLLM.AiliaLLMModel.Create ( )
inline

Create a instance.

Returns
If this function is successful, it returns true , or false otherwise.
56  {
57  if (net != IntPtr.Zero){
58  Close();
59  }
60 
61  int status = AiliaLLM.ailiaLLMCreate(ref net);
62  if (status != 0){
63  if (logging)
64  {
65  Debug.Log("ailiaLLMCreate failed " + status);
66  }
67  return false;
68  }
69 
70  return true;
71  }
virtual void Close()
Destroys instance.
Definition: AiliaLLMModel.cs:183

◆ Dispose() [1/2]

virtual void ailiaLLM.AiliaLLMModel.Dispose ( )
inlinevirtual

Release resources.

199  {
200  Dispose(true);
201  }
virtual void Dispose()
Release resources.
Definition: AiliaLLMModel.cs:198

◆ Dispose() [2/2]

virtual void ailiaLLM.AiliaLLMModel.Dispose ( bool  disposing)
inlineprotectedvirtual
204  {
205  if (disposing){
206  // release managed resource
207  }
208  Close(); // release unmanaged resource
209  }

◆ 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.
534  {
535  uint done_uint = 0;
536  int status = AiliaLLM.ailiaLLMGenerate(net, ref done_uint);
537  context_full = false;
538  done = (done_uint == 1);
539  if (status != 0){
540  if (logging)
541  {
542  Debug.Log("ailiaLLMGenerate failed " + status);
543  }
544  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
545  context_full = true;
546  }
547  return false;
548  }
549  return true;
550  }

◆ GeneratedTokenCount()

uint ailiaLLM.AiliaLLMModel.GeneratedTokenCount ( )
inline

Gets the number of tokens generated.

Returns
Number of tokens generated. 0 if failed.
659  {
660  if (net == IntPtr.Zero){
661  return 0;
662  }
663  uint count = 0;
664  int status = AiliaLLM.ailiaLLMGetGeneratedTokenCount(net, ref count);
665  if (status != 0){
666  if (logging)
667  {
668  Debug.Log("ailiaLLMGetGeneratedTokenCount failed " + status);
669  }
670  return 0;
671  }
672  return count;
673  }

◆ GetDeltaText()

string ailiaLLM.AiliaLLMModel.GetDeltaText ( )
inline

Set prompt messages.

Returns
It returns text.
564  {
565  uint len = 0;
566  int status = AiliaLLM.ailiaLLMGetDeltaTextSize(net, ref len);
567  if (status != 0){
568  if (logging)
569  {
570  Debug.Log("ailiaLLMGetDeltaTextSize failed " + status);
571  }
572  return "";
573  }
574  byte[] text = new byte [len];
575  GCHandle handle = GCHandle.Alloc(text, GCHandleType.Pinned);
576  IntPtr output = handle.AddrOfPinnedObject();
577  status = AiliaLLM.ailiaLLMGetDeltaText(net, output, len);
578  handle.Free();
579  if (status != 0){
580  if (logging)
581  {
582  Debug.Log("ailiaLLMGetDeltaText failed " + status);
583  }
584  return "";
585  }
586 
587  byte[] new_buf = new byte [buf.Length + len - 1];
588  for (int i = 0; i < buf.Length; i++){
589  new_buf[i] = buf[i];
590  }
591  for (int i = 0; i < len - 1; i++){ // NULLの削除
592  new_buf[buf.Length + i] = text[i];
593  }
594  buf = new_buf;
595 
596  string decoded_text = System.Text.Encoding.UTF8.GetString(buf); // Unicode Decode Errorは発生しない
597  string delta_text = "";
598  if (decoded_text.Length > before_text.Length){
599  delta_text = decoded_text.Substring(before_text.Length);
600  }
601  before_text = decoded_text;
602  return delta_text;
603  }

◆ GetMultimodalCapabilities()

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

Check if multimodal features are supported.

Parameters
vision_supportWhether image processing is supported
audio_supportWhether audio processing is supported
Returns
If this function is successful, it returns true , or false otherwise.
152  {
153  uint vision_uint = 0;
154  uint audio_uint = 0;
155  int status = AiliaLLM.ailiaLLMGetMultimodalCapabilities(net, ref vision_uint, ref audio_uint);
156  if (status != 0){
157  if (logging)
158  {
159  Debug.Log("ailiaLLMGetMultimodalCapabilities failed " + status);
160  }
161  return false;
162  }
163  vision_support = (vision_uint == 1);
164  audio_support = (audio_uint == 1);
165  return true;
166  }

◆ 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.
88  {
89  if (net == IntPtr.Zero){
90  return false;
91  }
92 
93  int status = 0;
94 
95  status = AiliaLLM.ailiaLLMOpenModelFile(net, model_path, n_ctx);
96  if (status != 0){
97  if (logging)
98  {
99  Debug.Log("ailiaLLMOpenModelFile failed " + status);
100  }
101  return false;
102  }
103 
104  return true;
105  }

◆ OpenMultimodalProjector()

bool ailiaLLM.AiliaLLMModel.OpenMultimodalProjector ( string  mmproj_path)
inline

Open a multimodal projector file.

Parameters
mmproj_pathPath for MMPROJ file
Returns
If this function is successful, it returns true , or false otherwise.
120  {
121  if (net == IntPtr.Zero){
122  return false;
123  }
124 
125  int status = AiliaLLM.ailiaLLMOpenMultimodalProjectorFile(net, mmproj_path);
126  if (status != 0){
127  if (logging)
128  {
129  Debug.Log("ailiaLLMOpenMultimodalProjectorFile failed " + status);
130  }
131  return false;
132  }
133 
134  return true;
135  }

◆ PromptTokenCount()

uint ailiaLLM.AiliaLLMModel.PromptTokenCount ( )
inline

Gets the number of prompt tokens.

Returns
Number of prompt tokens. 0 if failed.
632  {
633  if (net == IntPtr.Zero){
634  return 0;
635  }
636  uint count = 0;
637  int status = AiliaLLM.ailiaLLMGetPromptTokenCount(net, ref count);
638  if (status != 0){
639  if (logging)
640  {
641  Debug.Log("ailiaLLMGetPromptTokenCount failed " + status);
642  }
643  return 0;
644  }
645  return count;
646  }

◆ 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.
375  {
376  List<GCHandle> handle_list = new List<GCHandle>();
377  int len = messages.Count;
378  byte[][] role_text_list = new byte [len][];
379  byte[][] content_text_list = new byte [len][];
380  AiliaLLM.AILIALLMMultimodalChatMessage [] message_list = new AiliaLLM.AILIALLMMultimodalChatMessage[len];
381 
382  // Prepare media data arrays
383  List<byte[][]> media_type_lists = new List<byte[][]>();
384  List<byte[][]> media_path_lists = new List<byte[][]>();
385  List<AiliaLLM.AILIALLMMediaData[]> media_data_arrays = new List<AiliaLLM.AILIALLMMediaData[]>();
386 
387  for (int i = 0; i < len; i++){
388  AiliaLLM.AILIALLMMultimodalChatMessage message = new AiliaLLM.AILIALLMMultimodalChatMessage();
389 
390  // Set role and content
391  role_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].role+"\u0000");
392  GCHandle role_handle = GCHandle.Alloc(role_text_list[i], GCHandleType.Pinned);
393  IntPtr role_input = role_handle.AddrOfPinnedObject();
394 
395  content_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].content+"\u0000");
396  GCHandle content_handle = GCHandle.Alloc(content_text_list[i], GCHandleType.Pinned);
397  IntPtr content_input = content_handle.AddrOfPinnedObject();
398 
399  message.role = role_input;
400  message.content = content_input;
401 
402  handle_list.Add(role_handle);
403  handle_list.Add(content_handle);
404 
405  // Handle media data
406  if (messages[i].media_data != null && messages[i].media_data.Count > 0){
407  int media_count = messages[i].media_data.Count;
408  message.media_count = (uint)media_count;
409 
410  byte[][] media_type_list = new byte[media_count][];
411  byte[][] media_path_list = new byte[media_count][];
412  AiliaLLM.AILIALLMMediaData[] media_array = new AiliaLLM.AILIALLMMediaData[media_count];
413 
414  for (int j = 0; j < media_count; j++){
415  AiliaLLM.AILIALLMMediaData media = new AiliaLLM.AILIALLMMediaData();
416 
417  // Media type
418  media_type_list[j] = System.Text.Encoding.UTF8.GetBytes(messages[i].media_data[j].media_type+"\u0000");
419  GCHandle type_handle = GCHandle.Alloc(media_type_list[j], GCHandleType.Pinned);
420  media.media_type = type_handle.AddrOfPinnedObject();
421  handle_list.Add(type_handle);
422 
423  // File path
424  if (!string.IsNullOrEmpty(messages[i].media_data[j].file_path)){
425  media_path_list[j] = System.Text.Encoding.UTF8.GetBytes(messages[i].media_data[j].file_path+"\u0000");
426  GCHandle path_handle = GCHandle.Alloc(media_path_list[j], GCHandleType.Pinned);
427  media.file_path = path_handle.AddrOfPinnedObject();
428  handle_list.Add(path_handle);
429  } else {
430  media.file_path = IntPtr.Zero;
431  }
432 
433  // Raw data
434  if (messages[i].media_data[j].data != null && messages[i].media_data[j].data.Length > 0){
435  GCHandle data_handle = GCHandle.Alloc(messages[i].media_data[j].data, GCHandleType.Pinned);
436  media.data = data_handle.AddrOfPinnedObject();
437  media.data_size = (uint)messages[i].media_data[j].data.Length;
438  handle_list.Add(data_handle);
439  } else {
440  media.data = IntPtr.Zero;
441  media.data_size = 0;
442  }
443 
444  media.width = messages[i].media_data[j].width;
445  media.height = messages[i].media_data[j].height;
446 
447  media_array[j] = media;
448  }
449 
450  media_type_lists.Add(media_type_list);
451  media_path_lists.Add(media_path_list);
452  media_data_arrays.Add(media_array);
453 
454  // Allocate and set media_data pointer
455  int media_size = Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMediaData)) * media_count;
456  IntPtr media_ptr = Marshal.AllocHGlobal(media_size);
457 
458  for (int j = 0; j < media_count; j++){
459  IntPtr offset = new IntPtr(media_ptr.ToInt64() + j * Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMediaData)));
460  Marshal.StructureToPtr(media_array[j], offset, false);
461  }
462 
463  message.media_data = media_ptr;
464  } else {
465  message.media_count = 0;
466  message.media_data = IntPtr.Zero;
467  }
468 
469  message_list[i] = message;
470  }
471 
472  int size = Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMultimodalChatMessage)) * message_list.Length;
473  IntPtr ptr = Marshal.AllocHGlobal(size);
474 
475  int status = 0;
476 
477  try
478  {
479  for (int i = 0; i < message_list.Length; i++)
480  {
481  IntPtr offset = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(AiliaLLM.AILIALLMMultimodalChatMessage)));
482  Marshal.StructureToPtr(message_list[i], offset, false);
483  }
484 
485  status = AiliaLLM.ailiaLLMSetMultimodalPrompt(net, ptr, (uint)len);
486  }
487  finally
488  {
489  // Free allocated memory
490  Marshal.FreeHGlobal(ptr);
491  for (int i = 0; i < message_list.Length; i++){
492  if (message_list[i].media_data != IntPtr.Zero){
493  Marshal.FreeHGlobal(message_list[i].media_data);
494  }
495  }
496  }
497 
498  for (int i = 0; i < handle_list.Count; i++){
499  handle_list[i].Free();
500  }
501 
502  context_full = false;
503  buf = new byte[0];
504  before_text = "";
505 
506  if (status != 0){
507  if (logging)
508  {
509  Debug.Log("ailiaLLMSetMultimodalPrompt failed " + status);
510  }
511  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
512  context_full = true;
513  }
514  return false;
515  }
516 
517  return true;
518  }

◆ 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.
294  {
295  List<GCHandle> handle_list = new List<GCHandle>();
296  int len = messages.Count;
297  byte[][] role_text_list = new byte [len][];
298  byte[][] conntent_text_list = new byte [len][];
299  AiliaLLM.AILIAChatMessage [] message_list = new AiliaLLM.AILIAChatMessage[len];
300  for (int i = 0; i< len; i++){
301  AiliaLLM.AILIAChatMessage message = new AiliaLLM.AILIAChatMessage();
302 
303  role_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].role+"\u0000");
304  GCHandle role_handle = GCHandle.Alloc(role_text_list[i], GCHandleType.Pinned);
305  IntPtr role_input = role_handle.AddrOfPinnedObject();
306 
307  conntent_text_list[i] = System.Text.Encoding.UTF8.GetBytes(messages[i].content+"\u0000");
308  GCHandle content_handle = GCHandle.Alloc(conntent_text_list[i], GCHandleType.Pinned);
309  IntPtr content_input = content_handle.AddrOfPinnedObject();
310 
311  message.role = role_input;
312  message.content = content_input;
313  message_list[i] = message;
314 
315  handle_list.Add(role_handle);
316  handle_list.Add(content_handle);
317  }
318 
319  int size = Marshal.SizeOf(typeof(AiliaLLM.AILIAChatMessage)) * message_list.Length;
320  IntPtr ptr = Marshal.AllocHGlobal(size);
321 
322  int status = 0;
323 
324  try
325  {
326  for (int i = 0; i < message_list.Length; i++)
327  {
328  IntPtr offset = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(AiliaLLM.AILIAChatMessage)));
329  Marshal.StructureToPtr(message_list[i], offset, false);
330  }
331 
332  status = AiliaLLM.ailiaLLMSetPrompt(net, ptr, (uint)len);
333  }
334  finally
335  {
336  Marshal.FreeHGlobal(ptr);
337  }
338 
339  for (int i = 0; i < handle_list.Count; i++){
340  handle_list[i].Free();
341  }
342 
343  context_full = false;
344  buf = new byte[0];
345  before_text = "";
346 
347  if (status != 0){
348  if (logging)
349  {
350  Debug.Log("ailiaLLMSetPrompt failed " + status);
351  }
352  if (status == AiliaLLM.AILIA_LLM_STATUS_CONTEXT_FULL){
353  context_full = true;
354  }
355  return false;
356  }
357 
358  return true;
359  }

◆ 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.
238  {
239  int status = AiliaLLM.ailiaLLMSetSamplingParams(net, top_k, top_p, temp, dist);
240  if (status != 0){
241  if (logging)
242  {
243  Debug.Log("ailiaLLMSetSamplingParams failed " + status);
244  }
245  return false;
246  }
247  return true;
248  }

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

268  {
269  int status = AiliaLLM.ailiaLLMSetThinking(net, enable ? 1u : 0u);
270  if (status != 0){
271  if (logging)
272  {
273  Debug.Log("ailiaLLMSetThinking failed " + status);
274  }
275  return false;
276  }
277  return true;
278  }

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