Multi-pipeline knowledge bases
==============================

ナレッジベースKBには、複数のパイプラインを書き込むことができます。各パイプラインは、独自のソース、処理チェーン、および自動処理モードを維持しますが、それらはすべて同じベクトルテーブルにエンベディングを保存します。取得はKBを1回照会し、接続されているすべてのパイプラインにわたって結果を集計します。

これは、次のような場合に役立ちます。

- 複数のソーステーブルを単一の検索可能なインデックスに結合します。

- 混合データ形式 — たとえば、1つのパイプラインは\ ``Text``
  行をインデックスし、別のパイプラインは :ref:`Configuring external storage <Configuring external storage>`  を介して\ ``Pdf``
  ファイルをインデックスします。

- ソースごとに異なる前処理を適用します —
  1つのパイプラインは埋め込む前に\ ``ChunkText``
  を実行し、別のパイプラインは短いテキストを直接埋め込みます。

既存のKBへのパイプラインのアタッチ
----------------------------------

最初のパイプラインはKBを作成します。後続のパイプラインは、同じ\ ``destination``
に書き込むことにより接続します。後続のパイプラインに :ref:`aidb.knowledge_base_config_from_kb() <Pipelines API reference>` を使用して、KBのモデル、距離演算子、ベクトルインデックスを継承します。

.. code:: sql

   - - Pipeline 1 — creates the KB from the North America customer feedback table
   SELECT aidb.create_pipeline(
       name => feedback_na_pipe,
       source => customers_na,
       source_key_column => id,
       source_data_column => feedback,
       destination => customer_feedback_kb,
       step_1 => KnowledgeBase,
       step_1_options => aidb.knowledge_base_config(model => bert, data_format => Text)
   );

   - - Pipeline 2 — attaches the Europe customer feedback table to the same KB
   SELECT aidb.create_pipeline(
       name => feedback_eu_pipe,
       source => customers_eu,
       source_key_column => id,
       source_data_column => feedback,
       destination => customer_feedback_kb,
       step_1 => KnowledgeBase,
       step_1_options => aidb.knowledge_base_config_from_kb(data_format => Text)
   );

   SELECT aidb.run_pipeline(feedback_na_pipe);
   SELECT aidb.run_pipeline(feedback_eu_pipe);

クエリーは両方のパイプラインから行を返し、 ``pipeline_name``
がソースを識別します。

.. code:: sql

   SELECT key, value, pipeline_name
   FROM aidb.retrieve_text(public.customer_feedback_kb, shipping was slow, topk => 3);
   __OUTPUT__
    key  |                       value                        |   pipeline_name
   - -----+----------------------------------------------------+------------------
    1042 | Delivery was held up in customs for over a week    | feedback_eu_pipe
    318  | Order took twice as long to arrive as estimated    | feedback_na_pipe
    1057 | Tracking number never updated until it arrived     | feedback_eu_pipe

さまざまなステップチェーンが1つのKBを共有できます。たとえば、あるパイプラインは\ ``KnowledgeBase``
ステップの前に\ ``ChunkText``
で長いドキュメントをチャンクし、別のパイプラインは短い行を直接埋め込みます。
``aidb.retrieve_text()`` の\ ``intermediate_steps``
JSONB列は、 :ref:`エンドツーエンドの例 中間ストレージを使用したマルチステップパイプライン <エンドツーエンドの例 中間ストレージを使用したマルチステップパイプライン>` が有効になっているステップについて、パイプラインごとの中間結果を結果ます。
``deduplicate``
デフォルトでonは、KB全体ではなく、パイプラインごとにスコープされます。
:ref:`重複排除と中間結果 <重複排除と中間結果>` を参照してください。

互換性ルール
------------

KBに接続されているすべてのパイプラインは、埋め込み形状について一致する必要があります。既存のKBをターゲットとするパイプラインを作成すると、AIDBは以下を検証します。

.. csv-table::
  :header: Constraint,Error if violated
  :widths: 25,20
  :align: left
  :class: longtable

  Model,`Model mismatch: existing KB uses '<X>' but pipeline specifies '<Y>'`
  Distance operator,`Distance operator mismatch: ...`
  Vector index config,`Vector index mismatch: ...`
  Embedding dimensions,`Embedding dimension mismatch: existing KB vector column has N dimensions but model '<X>' produces M`

``aidb.knowledge_base_config_from_kb()``
は、既存のKBからモデル、距離演算子、およびベクトルインデックスを継承するため、最初の3つのチェックに常にパスします。寸法チェックは引き続き実行され、モデルが同じ名前で異なる出力サイズで再作成されるエッジケースをキャッチします。

削除セマンティクス
------------------

.. csv-table::
  :header: Action,Effect
  :widths: 20,25
  :align: left
  :class: longtable

  `aidb.delete_pipeline('one_of_many')`,Removes that pipeline and only its rows from the shared vector table. KB and other pipelines remain.
  `aidb.delete_pipeline('the_last_one')`,Drops the KB registry entry and the vector table along with the pipeline.
  `aidb.delete_knowledge_base('schema.table')`,"Cascades — deletes every attached pipeline, the KB, and the vector table."

これらの操作は、ソーステーブルまたはソースボリュームに触れることはありません。

接続されたパイプラインの検査
----------------------------

.. code:: sql

   SELECT name, model_name, distance_operator, pipeline_names
   FROM aidb.knowledge_bases
   WHERE name = public.customer_feedback_kb;
   __OUTPUT__
               name             | model_name | distance_operator |          pipeline_names
   - ----------------------------+------------+-------------------+------------------------------------
    public.customer_feedback_kb | bert       | L2                | {feedback_eu_pipe,feedback_na_pipe}

集計数とステータスについては、
:ref:`helpb.Knowledge_base_metrics <helpb.Knowledge_base_metrics>` を照会します。パイプラインごとの進行状況については、
:ref:`promoteb.pipeline_metrics <promoteb.pipeline_metrics>`  を照会してください。

ベクターテーブルスキーマ
------------------------

共有ベクトルテーブルには\ ``pipeline_id``
列が含まれるため、各行を元のパイプラインまでトレースできます。主キーは\ ``(pipeline_id, source_id, part_ids)``
です。

.. code:: sql

   SELECT pipeline_id, source_id, part_ids
   FROM customer_feedback_kb
   ORDER BY pipeline_id, source_id;
   __OUTPUT__
    pipeline_id | source_id | part_ids
   - ------------+-----------+----------
              1 | 318       | {0}
              1 | 412       | {0}
              2 | 1042      | {0}
              2 | 1057      | {0}
