AI inference functions
======================

AIDBは、クエリで直接AI推論を実行するためのSQLファンクションセットを提供します。これらの機能を使用すると、パイプラインを設定せずにデータをベクトル表現にエンコードし、言語モデルを使用してテキストを生成し、推論動作を制御できます。

quietb.encode_text()
--------------------

登録された埋め込みモデルを使用して、単一のテキスト文字列をベクトルにエンコードします。

.. code:: sql

   SELECT aidb.encode_text(
       input   => What is pgvector?,
       options => {"model": "my_embedder"}
   );

   __OUTPUT__
                            encode_text
   - -------------------------------------------------------------
    [0.021,-0.043,0.117, ...]
   (1 row)

完全なパラメーターのリファレンスについては、
:ref:`Models reference <Models reference>` を参照してください。

--------------

promoteb.encode_text_batch()
----------------------------

単一のバッチ要求で複数のテキスト文字列をベクターにエンコードします。多くの入力を処理する場合、ループで\ ``encode_text()``
を呼び出すよりも効率的です。

.. code:: sql

   SELECT * FROM aidb.encode_text_batch(
       my_model_name,
       ARRAY[What is pgvector?, How does RAG work?, Explain embeddings.]
   );

   __OUTPUT__
     encode_text_batch
   - ------------------------
    [0.021,-0.043,0.117,...]
    [0.008, 0.091,-0.034,...]
    [0.055,-0.012, 0.076,...]
   (3 rows)

完全なパラメーターのリファレンスについては、
:ref:`Models reference <Models reference>` を参照してください。

--------------

quietb.encode_image()
---------------------

登録されたマルチモーダル埋め込みモデルCLIPモデルなどを使用して、単一の画像をベクトルにエンコードします。

.. code:: sql

   SELECT aidb.encode_image(
       my_model_name,
       pg_read_binary_file(/path/to/image.png)::BYTEA
   );

   __OUTPUT__
                            encode_image
   - -------------------------------------------------------------
    [-0.012, 0.084, 0.031, ...]
   (1 row)

完全なパラメーターのリファレンスについては、
:ref:`Models reference <Models reference>` を参照してください。

--------------

quietb.decode_text()
--------------------

指定されたプロンプトに応じて言語モデルLLMからテキストを生成します。これは、AIDBのLLM推論のコア機能です。

.. code:: sql

   SELECT aidb.decode_text(
       model_name => my_model_name,
       input   => Explain what a knowledge base is in two sentences.,
       inference_config => NULL
   );

   __OUTPUT__
                                           decode_text
   - -------------------------------------------------------------------------------------------
    A knowledge base is a structured repository of information used to answer queries or ...
   (1 row)

生成動作を制御するには、オプションで\ ``inference_config``
オブジェクトを渡します。

.. code:: sql

   SELECT aidb.decode_text(
                  model_name => my_model_name,
                  input   => Summarize the following text: ...,
                  inference_config => aidb.inference_config(
                                  system_prompt => You are a concise technical writer.,
                                  temperature   => 0.2,
                                  max_tokens    => 256
                                              )
                             
          );

完全なパラメーターのリファレンスについては、
:ref:`Models reference <Models reference>` を参照してください。

--------------

quietb.decode_text_batch()
--------------------------

単一のバッチ要求で複数のプロンプトのテキスト応答を生成します。

.. code:: sql

   SELECT * FROM aidb.decode_text_batch(
       model_name => my_model_name,
       input  => ARRAY[
           What is a vector database?,
           What is retrieval-augmented generation?
       ]
   );

   __OUTPUT__
    id |                          value
   - ---+----------------------------------------------------------
     0 | A vector database stores high-dimensional vectors ...
     1 | Retrieval-augmented generation (RAG) combines a ...
   (2 rows)

完全なパラメーターのリファレンスについては、
:ref:`Models reference <Models reference>` を参照してください。

--------------

quietb.inference_config()
-------------------------

推論構成オブジェクトをビルドして、言語モデル生成動作を制御します。結果を\ ``decode_text()``
、\ ``decode_text_batch()`` 、または\ ``summarize_text()``
の\ ``options`` JSONB内の\ ``"inference_config"`` キーとして渡します。

.. code:: sql

   SELECT aidb.decode_text(
       input   => Write a haiku about PostgreSQL.,
       options => jsonb_build_object(
           model,            my_llm,
           inference_config, aidb.inference_config(
               temperature => 0.9,
               max_tokens  => 64,
               seed        => 42
           )
       )
   );

完全なパラメーターのリファレンスについては、 :ref:`Models reference <Models reference>` 
を参照してください。

!!!注
これらのファンクションをパイプライン内のステップとして使用するには、
:ref:`Pipeline steps <Pipeline steps>` を参照してください。
