Java API
ONNX Runtime generate() Java API
Section titled “ONNX Runtime generate() Java API”注意: この API はプレビュー版であり、変更される可能性があります。
このドキュメントでは、ONNX Runtime GenAI の Java API について説明します。 以下に、主要なクラスとメソッド、およびそれぞれのコードスニペットと説明を示します。
インストールとインポート
Section titled “インストールとインポート”Java API は ai.onnxruntime.genai Java パッケージによって提供されます。パッケージの公開は保留中です。ソースからパッケージをビルドするには、ソースからのビルドガイドを参照してください。
import ai.onnxruntime.genai.*;Model クラス
Section titled “Model クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルパスから新しいモデルを初期化します。
public Model(String modelPath) throws GenAIExceptioncreateGeneratorParams
Section titled “createGeneratorParams”モデルを実行するための GeneratorParams インスタンスを作成します。
public GeneratorParams createGeneratorParams() throws GenAIExceptioncreateTokenizer
Section titled “createTokenizer”このモデルの Tokenizer インスタンスを作成します。
public Tokenizer createTokenizer() throws GenAIExceptiongenerate
Section titled “generate”指定されたジェネレータパラメータを使用して出力シーケンスを生成します。
public Sequences generate(GeneratorParams generatorParams) throws GenAIExceptionConfig クラス
Section titled “Config クラス”コンストラクタ
Section titled “コンストラクタ”設定パスから新しい設定オブジェクトを初期化します。
public Config(String configPath) throws GenAIExceptionclearProviders
Section titled “clearProviders”設定からすべてのプロバイダーをクリアします。
public void clearProviders() throws GenAIExceptionappendProvider
Section titled “appendProvider”設定にプロバイダーを追加します。
public void appendProvider(String provider) throws GenAIExceptionsetProviderOption
Section titled “setProviderOption”設定でプロバイダーオプションを設定します。
public void setProviderOption(String provider, String name, String value) throws GenAIExceptionoverlay
Section titled “overlay”設定に JSON 文字列を重ね合わせます。
public void overlay(String json) throws GenAIExceptionTokenizer クラス
Section titled “Tokenizer クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルのトークナイザーを初期化します。
public Tokenizer(Model model) throws GenAIExceptionencode
Section titled “encode”文字列をトークンIDのシーケンスにエンコードします。
public Sequences encode(String string) throws GenAIExceptionencodeBatch
Section titled “encodeBatch”文字列の配列を、各入力のトークンIDのシーケンスにエンコードします。
public Sequences encodeBatch(String[] strings) throws GenAIExceptiondecode
Section titled “decode”トークンIDのシーケンスをテキストにデコードします。
public String decode(int[] sequence) throws GenAIExceptiondecodeBatch
Section titled “decodeBatch”トークンIDのシーケンスのバッチをテキストにデコードします。
public String[] decodeBatch(Sequences sequences) throws GenAIExceptioncreateStream
Section titled “createStream”ストリーミングトークン化のための TokenizerStream オブジェクトを作成します。
public TokenizerStream createStream() throws GenAIExceptionTokenizerStream クラス
Section titled “TokenizerStream クラス”decode
Section titled “decode”ストリーム内の単一のトークンをデコードし、生成された文字列チャンクを返します。
public String decode(int token) throws GenAIExceptionGeneratorParams クラス
Section titled “GeneratorParams クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルのジェネレータパラメータを初期化します。
public GeneratorParams(Model model) throws GenAIExceptionsetSearchOption (double)
Section titled “setSearchOption (double)”数値検索オプションを設定します。
public void setSearchOption(String optionName, double value) throws GenAIExceptionsetSearchOption (boolean)
Section titled “setSearchOption (boolean)”ブール値の検索オプションを設定します。
public void setSearchOption(String optionName, boolean value) throws GenAIExceptionsetInput (Sequences)
Section titled “setInput (Sequences)”シーケンスを使用してモデル実行のプロンプトを設定します。
public void setInput(Sequences sequences) throws GenAIExceptionsetInput (int[])
Section titled “setInput (int[])”モデル実行のプロンプトトークンIDを設定します。
public void setInput(int[] tokenIds, int sequenceLength, int batchSize) throws GenAIExceptionGenerator クラス
Section titled “Generator クラス”コンストラクタ
Section titled “コンストラクタ”指定されたモデルとジェネレータパラメータで Generator オブジェクトを構築します。
public Generator(Model model, GeneratorParams generatorParams) throws GenAIExceptionisDone
Section titled “isDone”生成プロセスが完了したかどうかを確認します。
public boolean isDone()computeLogits
Section titled “computeLogits”シーケンス内の次のトークンのロジットを計算します。
public void computeLogits() throws GenAIExceptiongenerateNextToken
Section titled “generateNextToken”シーケンス内の次のトークンを生成します。
public void generateNextToken() throws GenAIExceptiongetSequence
Section titled “getSequence”指定されたシーケンスインデックスのトークンIDのシーケンスを取得します。
public int[] getSequence(long sequenceIndex) throws GenAIExceptiongetLastTokenInSequence
Section titled “getLastTokenInSequence”指定されたシーケンスインデックスのシーケンス内の最後のトークンを取得します。
public int getLastTokenInSequence(long sequenceIndex) throws GenAIExceptionSequences クラス
Section titled “Sequences クラス”numSequences
Section titled “numSequences”コレクション内のシーケンスの数を取得します。
public long numSequences()getSequence
Section titled “getSequence”指定されたインデックスのシーケンスを取得します。
public int[] getSequence(long sequenceIndex)Tensor クラス
Section titled “Tensor クラス”コンストラクタ
Section titled “コンストラクタ”指定されたデータ、形状、および要素タイプで Tensor を構築します。
public Tensor(ByteBuffer data, long[] shape, ElementType elementType) throws GenAIExceptionResult クラス
Section titled “Result クラス”isSuccess
Section titled “isSuccess”操作が成功したかどうかを示します。
public boolean isSuccess()getError
Section titled “getError”失敗した操作のエラーメッセージを取得します。
public String getError()Utils クラス
Section titled “Utils クラス”setLogBool
Section titled “setLogBool”ブール値のログオプションを設定します。
public static void setLogBool(String name, boolean 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()