ailia_llm  1.5.1.0
公開メンバ関数 | 静的公開メンバ関数 | 限定公開メンバ関数 | 全メンバ一覧
ailiaLLM.AiliaLLMModel クラス
ailiaLLM.AiliaLLMModel の継承関係図
Inheritance graph
[凡例]
ailiaLLM.AiliaLLMModel 連携図
Collaboration graph
[凡例]

公開メンバ関数

string GetErrorDetail ()
 最後のエラーの詳細を取得します。 [詳解]
 
bool Create ()
 インスタンスを作成します。 [詳解]
 
bool SetNumThreads (uint n_threads)
 Openの前にCPUスレッド数を設定します。 [詳解]
 
bool SetBackend (uint backend_idx)
 Openの前にCPU、GPUまたはHTP(QNN)バックエンドを選択します。 [詳解]
 
bool Open (string model_path, uint n_ctx=0)
 モデルファイルを開きます。 [詳解]
 
bool OpenMtpModel (string model_path, uint n_draft)
 MTPドラフトモデルを開き、生成をドラフト・本体検証経路に切り替えます。 [詳解]
 
bool OpenMultimodalProjector (string mmproj_path)
 マルチモーダルプロジェクタファイルを開きます。 [詳解]
 
bool GetMultimodalCapabilities (ref bool vision_support, ref bool audio_support)
 マルチモーダル機能がサポートされているかを確認します。 [詳解]
 
virtual void Close ()
 インスタンスを破棄します。 [詳解]
 
virtual void Dispose ()
 リソースを解放します。 [詳解]
 
bool SetSamplingParam (uint top_k, float top_p, float temp, uint dist)
 サンプリングパラメータを設定します。 [詳解]
 
bool SetThinking (bool enable)
 Thinking(推論過程の出力)を有効または無効にします。 [詳解]
 
bool SetPrompt (List< AiliaLLMChatMessage > messages)
 プロンプトを設定します。 [詳解]
 
bool SetPromptJson (string messagesJson)
 構造化されたJSON形式の会話履歴を設定します。 [詳解]
 
string GetResponseJson ()
 差分テキストを収集せずに、保持されたassistant応答をJSONで取得します。 [詳解]
 
bool SetTools (string tools_json)
 利用可能なツールをJSONで設定します。 [詳解]
 
bool SetMultimodalPrompt (List< AiliaLLMMultimodalChatMessage > messages)
 マルチモーダルプロンプトを設定します。 [詳解]
 
bool Generate (ref bool done)
 生成を実行します。 [詳解]
 
string GetDeltaText ()
 生成結果のテキストを取得します。 [詳解]
 
bool ContextFull ()
 コンテキスト長の上限に達したかどうかを取得します。 [詳解]
 
uint PromptTokenCount ()
 プロンプトのトークンの数を取得します。 [詳解]
 
uint GeneratedTokenCount ()
 生成したトークンの数を取得します。 [詳解]
 

静的公開メンバ関数

static List< AiliaLLMBackendInfo > GetBackendList ()
 利用可能なバックエンドの一覧を取得します。 [詳解]
 
static string GetQNNModelName ()
 モデルを作成せずにデバイスのQNN成果物名を取得します。 [詳解]
 

限定公開メンバ関数

virtual void Dispose (bool disposing)
 

関数詳解

◆ Close()

virtual void ailiaLLM.AiliaLLMModel.Close ( )
inlinevirtual

インスタンスを破棄します。

インスタンスを破棄し、初期化します。

注釈
QNNを使用する場合は、Createを呼び出したスレッドで呼び出してください。 QNN使用時に解放だけをUIスレッドなどの別スレッドへ移すと、その後、別のスレッドでQNNを使用する際に QnnDevice_create failed: code 14001が発生することがあります。
367  {
368  if (net != IntPtr.Zero){
369  AiliaLLM.ailiaLLMDestroy(net);
370  net = IntPtr.Zero;
371  }
372  }

◆ ContextFull()

bool ailiaLLM.AiliaLLMModel.ContextFull ( )
inline

コンテキスト長の上限に達したかどうかを取得します。

戻り値
上限に達した場合はtrue、達していない場合はfalse。
885  {
886  return context_full;
887  }

◆ Create()

bool ailiaLLM.AiliaLLMModel.Create ( )
inline

インスタンスを作成します。

戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
注釈
QNNを使用する場合は、この関数を呼び出したスレッドでCloseまたはDisposeを呼び出して解放してください。
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()
インスタンスを破棄します。
Definition: AiliaLLMModel.cs:366

◆ Dispose() [1/2]

virtual void ailiaLLM.AiliaLLMModel.Dispose ( )
inlinevirtual

リソースを解放します。

注釈
QNNを使用する場合は、Createを呼び出したスレッドで呼び出してください。 QNN使用時は、ファイナライザーが同じスレッドでの実行を保証しないため、GCに頼らずCloseまたはDisposeで明示的に解放してください。
388  {
389  Dispose(true);
390  }
virtual void Dispose()
リソースを解放します。
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

生成を実行します。

引数
done生成が終了したかどうか。
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

生成したトークンの数を取得します。

戻り値
生成したトークン数。失敗時は0。
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

利用可能なバックエンドの一覧を取得します。

戻り値
バックエンド名、デバイス名、選択用インデックスの一覧
例外
InvalidOperationExceptionバックエンド情報の取得に失敗した場合

モデルの作成前にも呼び出せます。選択する場合はCreateの後、Openの前に 一覧のidxをSetBackendに渡してください。

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

生成結果のテキストを取得します。

戻り値
テキストを返します。
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

最後のエラーの詳細を取得します。

戻り値
エラー詳細。詳細がない場合は空文字列。
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

マルチモーダル機能がサポートされているかを確認します。

引数
vision_support画像入力(VLM)をサポートしているか
audio_support音声入力(ALM)をサポートしているか
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

モデルを作成せずにデバイスのQNN成果物名を取得します。

戻り値
QNN成果物名
例外
InvalidOperationExceptionQNN非対応時、または未対応デバイスの場合

QNNはAndroid arm64と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

差分テキストを収集せずに、保持されたassistant応答をJSONで取得します。

戻り値
JSON形式のassistant応答。エラー時は空文字列。
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

モデルファイルを開きます。

引数
model_pathモデルファイルへのパス。
n_ctxコンテキスト長(0でモデルのデフォルト)
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

MTPドラフトモデルを開き、生成をドラフト・本体検証経路に切り替えます。

引数
model_path対応するGemma 4 assistant GGUF、またはQNN本体用assistant .qnnへのパス。
n_draftドラフトトークン数(1以上)。QNNでは最大3。
戻り値
成功した場合はtrue、失敗した場合はfalse。

Openの後、プロンプト設定前に一度呼び出します。CPU/GPU Gemma 4 GGUFとQNNのテキスト生成に対応します。

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

マルチモーダルプロジェクタファイルを開きます。

引数
mmproj_pathMMPROJ GGUFまたは自己完結ailia QNN projector(.qnn)へのパス。
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。

画像入力(VLM)には画像対応、音声入力(ALM)には音声対応のprojectorが必要です。 QNN projectorはAndroid arm64と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

プロンプトのトークンの数を取得します。

戻り値
プロンプトのトークン数。失敗時は0。
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

Openの前にCPU、GPUまたはHTP(QNN)バックエンドを選択します。

引数
backend_idxGetBackendListで取得したバックエンドのインデックス。
戻り値
成功した場合はtrue、失敗した場合はfalse。

未選択の場合、.qnnはHTPを自動選択します。明示的なCPU/GPU選択では.qnnを、HTP選択では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

マルチモーダルプロンプトを設定します。

引数
messagesマルチモーダルプロンプトメッセージ。
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

Openの前にCPUスレッド数を設定します。

引数
n_threadsスレッド数(0は自動設定)。
戻り値
成功した場合はtrue、失敗した場合はfalse。

未指定の場合も0として扱います。Androidでは高効率コア(高性能側のbig/prime)の数を使用します。 取得できない場合は既存のコア数判定を使用します。本体のDecode/Prefill、assistant、VLM/ALMのCPU推論スレッド数に同じ値を使用します。

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

プロンプトを設定します。

引数
messagesプロンプトメッセージ。
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

構造化されたJSON形式の会話履歴を設定します。

引数
messagesJsonJSON形式の会話履歴。
戻り値
成功した場合はtrue、失敗した場合はfalse。

ツール使用時に必要です。メディア入力には対応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

サンプリングパラメータを設定します。

引数
top_kサンプリングする確率値の上位個数、デフォルト40
top_pサンプリングする確率値の範囲、デフォルト0.9(0.9〜1.0)
temp温度パラメータ、デフォルト0.4
distシード、デフォルト1234
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。
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

Thinking(推論過程の出力)を有効または無効にします。

引数
enabletrueで有効、falseで無効(デフォルト:無効)
戻り値
成功した場合はtrue、失敗した場合はfalseを返す。

Thinkingモデル(Gemma4等)の推論過程の出力を制御します。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

利用可能なツールをJSONで設定します。

引数
tools_jsonツール定義のJSON。nullまたは空文字列でツールを無効にします。
戻り値
成功した場合はtrue、失敗した場合はfalse。
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  }

このクラス詳解は次のファイルから抽出されました: