C# API
ONNX Runtime generate() C# API
Section titled “ONNX Runtime generate() C# API”注意: この API はプレビュー版であり、変更される可能性があります。
このドキュメントでは、ONNX Runtime GenAI の C# API について説明します。 以下に、主要なクラスとメソッド、およびそれぞれのコードスニペットと説明を示します。
Model クラス
Section titled “Model クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルパスから新しいモデルを初期化します。
public Model(string modelPath)Generate
Section titled “Generate”指定されたジェネレータパラメータを使用して出力シーケンスを生成します。
public Sequences Generate(GeneratorParams generatorParams)Config クラス
Section titled “Config クラス”コンストラクタ
Section titled “コンストラクタ”設定パスから新しい設定オブジェクトを初期化します。
public Config(string configPath)ClearProviders
Section titled “ClearProviders”設定からすべてのプロバイダーをクリアします。
public void ClearProviders()AppendProvider
Section titled “AppendProvider”設定にプロバイダーを追加します。
public void AppendProvider(string provider)SetProviderOption
Section titled “SetProviderOption”設定でプロバイダーオプションを設定します。
public void SetProviderOption(string provider, string name, string value)Overlay
Section titled “Overlay”設定に JSON 文字列を重ね合わせます。
public void Overlay(string json)Tokenizer クラス
Section titled “Tokenizer クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルのトークナイザーを初期化します。
public Tokenizer(Model model)Encode
Section titled “Encode”文字列をエンコードし、エンコードされたシーケンスを返します。
public Sequences Encode(string str)EncodeBatch
Section titled “EncodeBatch”文字列のバッチをエンコードし、エンコードされたシーケンスを返します。
public Sequences EncodeBatch(string[] strings)Decode
Section titled “Decode”トークンのシーケンスを文字列にデコードします。
public string Decode(ReadOnlySpan<int> sequence)DecodeBatch
Section titled “DecodeBatch”シーケンスのバッチを文字列の配列にデコードします。
public string[] DecodeBatch(Sequences sequences)ApplyChatTemplate
Section titled “ApplyChatTemplate”チャットテンプレートをメッセージとツールに適用します。
public string ApplyChatTemplate(string template, string messages, string tools, bool addGenerationPrompt)CreateStream
Section titled “CreateStream”増分デコード用のトークナイザーストリームを作成します。
public TokenizerStream CreateStream()TokenizerStream クラス
Section titled “TokenizerStream クラス”Decode
Section titled “Decode”ストリーム内の単一のトークンをデコードし、生成された文字列チャンクを返します。
public string Decode(int token)GeneratorParams クラス
Section titled “GeneratorParams クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルのジェネレータパラメータを初期化します。
public GeneratorParams(Model model)SetSearchOption (double)
Section titled “SetSearchOption (double)”数値検索オプションを設定します。
public void SetSearchOption(string searchOption, double value)SetSearchOption (bool)
Section titled “SetSearchOption (bool)”ブール値の検索オプションを設定します。
public void SetSearchOption(string searchOption, bool value)TryGraphCaptureWithMaxBatchSize
Section titled “TryGraphCaptureWithMaxBatchSize”最大バッチサイズでグラフキャプチャモードを有効にしようとします。
public void TryGraphCaptureWithMaxBatchSize(int maxBatchSize)SetInputIDs
Section titled “SetInputIDs”ジェネレータパラメータの入力 ID を設定します。
public void SetInputIDs(ReadOnlySpan<int> inputIDs, ulong sequenceLength, ulong batchSize)SetInputSequences
Section titled “SetInputSequences”ジェネレータパラメータの入力シーケンスを設定します。
public void SetInputSequences(Sequences sequences)SetModelInput
Section titled “SetModelInput”追加のモデル入力を設定します。
public void SetModelInput(string name, Tensor value)Generator クラス
Section titled “Generator クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルとジェネレータパラメータからジェネレータを初期化します。
public Generator(Model model, GeneratorParams generatorParams)IsDone
Section titled “IsDone”生成が完了したかどうかを確認します。
public bool IsDone()ComputeLogits
Section titled “ComputeLogits”現在の状態のロジットを計算します。
public void ComputeLogits()GenerateNextToken
Section titled “GenerateNextToken”次のトークンを生成します。
public void GenerateNextToken()GetSequence
Section titled “GetSequence”指定されたインデックスで生成されたシーケンスを返します。
public ReadOnlySpan<int> GetSequence(ulong index)SetActiveAdapter
Section titled “SetActiveAdapter”この Generator インスタンスでアクティブなアダプターを設定します。
public void SetActiveAdapter(Adapters adapters, string adapterName)パラメータ
adapters: 以前に作成されたAdaptersオブジェクトadapterName: アクティブにするアダプターの名前
戻り値
void
例外
エラー時にスローします。
Result クラス
Section titled “Result クラス”失敗した操作のエラーメッセージを取得します。
public string Error { get; }Success
Section titled “Success”操作が成功したかどうかを示します。
public bool Success { get; }Sequences クラス
Section titled “Sequences クラス”NumSequences
Section titled “NumSequences”シーケンスの数を取得します。
public ulong NumSequences { get; }Indexer
Section titled “Indexer”指定されたインデックスのシーケンスを取得します。
public ReadOnlySpan<int> this[ulong sequenceIndex]Tensor クラス
Section titled “Tensor クラス”コンストラクタ
Section titled “コンストラクタ”バッファからテンソルを初期化します。
public Tensor(Array data, long[] shape, ElementType elementType)基になるデータバッファを取得します。
public Array Data { get; }テンソルの形状を取得します。
public long[] Shape { get; }ElementType
Section titled “ElementType”テンソルの要素タイプを取得します。
public ElementType ElementType { get; }Utils クラス
Section titled “Utils クラス”SetLogBool
Section titled “SetLogBool”ブール値のログオプションを設定します。
public static void SetLogBool(string name, bool value)SetLogString
Section titled “SetLogString”文字列のログオプションを設定します。
public static void SetLogString(string name, string value)SetCurrentGpuDeviceId
Section titled “SetCurrentGpuDeviceId”現在の GPU デバイス ID を設定します。
public static void SetCurrentGpuDeviceId(int deviceId)GetCurrentGpuDeviceId
Section titled “GetCurrentGpuDeviceId”現在の GPU デバイス ID を取得します。
public static int GetCurrentGpuDeviceId()