ローカルRESTプロバイダOllamaを使用したサマリーの生成

ローカル・ホストのRESTエンドポイント・プロバイダOllamaを使用して、オープンLLMにアクセスして、テキストからサマリーへの変換を実行します。

Ollamaは、Linux、WindowsまたはmacOSシステムでオープンLLM (Llama 3、Phi 3、Mistral、Gemma 2など)をローカルおよびプライベートで実行できる、無料でオープンソースのコマンドライン・インタフェース・ツールです。サービスとしてのOllamaには、SQLおよびPL/SQLコマンドを使用してアクセスできます。

ここでは、連鎖可能ユーティリティ関数UTL_TO_SUMMARYをコールします。

警告:

データベースの特定の機能により、たとえば、REST APIへのアクセスを容易にするJSON仕様を使用して、第三者によって個別に提供されるサービスにアクセスできる場合があります。

お客様によるこれらの機能の使用は、お客様自身の責任においてのみ行われ、お客様は、当該第三者サービスの使用に関連するあらゆる条件を遵守する責任を負います。第三者のサービスに関するその他の条件にかかわらず、お客様は、かかるデータベース機能の使用によって、そのリスクを受諾し、当該アクセスにより生じた一切の損害について、Oracleの責任または法的責任を明示的に除外することになります。

Ollamaを使用してローカルLLMをコールし、テキスト抜粋の簡潔で情報の多いサマリーを生成するには:

  1. ローカル・ユーザーとしてOracle Databaseに接続します。
    1. SQL*PlusにSYSユーザーとしてログインし、SYSDBAとして接続します。
      conn sys/password as sysdba
      CREATE TABLESPACE tbs1
      DATAFILE 'tbs5.dbf' SIZE 20G AUTOEXTEND ON
      EXTENT MANAGEMENT LOCAL
      SEGMENT SPACE MANAGEMENT AUTO;
      SET ECHO ON
      SET FEEDBACK 1
      SET NUMWIDTH 10
      SET LINESIZE 80
      SET TRIMSPOOL ON
      SET TAB OFF
      SET PAGESIZE 10000
      SET LONG 10000
    2. ローカル・ユーザー(docuser)を作成し、必要な権限を付与します。
      DROP USER docuser cascade;
      CREATE USER docuser identified by docuser DEFAULT TABLESPACE tbs1 quota unlimited on tbs1;
      GRANT DB_DEVELOPER_ROLE, create credential to docuser;
    3. ローカル・ユーザー(docuser)として接続します:
      CONN docuser/password
  2. Ollamaをインストールし、モデルをローカルで実行します。
    1. https://ollama.com/downloadからOllamaアプリケーションをダウンロードして実行します。

      Ollamaは、バックグラウンドで実行されるサービスとしてインストールすることも、手動インストールによるスタンドアロン・バイナリとしてインストールすることもできます。インストール固有のステップの詳細は、Ollamaドキュメントのクイック・スタートを参照してください。

      次の点に注意してください:

      • Ollamaサーバーは、モデルをダウンロードできるようにインターネットに接続できる必要があります。インターネットへのアクセスにプロキシ・サーバーが必要な場合は、Ollamaサーバーを実行する前に、必ず適切な環境変数を設定してください。たとえば、Linux用に設定するには:

        -- set a proxy if you require one
        
        export https_proxy=<proxy-hostname>:<proxy-port>
        export http_proxy=<proxy-hostname>:<proxy-port>
        export no_proxy=localhost,127.0.0.1,.example.com
        export ftp_proxy=<proxy-hostname>:<proxy-port>
      • Ollamaとデータベースを異なるマシンで実行している場合は、データベースのマシンで、ローカル・ホストではなくOllamaを実行しているホスト名またはIPアドレスを参照するようにURLを変更する必要があります。

      • ポートを経由できるように、Ollamaを実行しているマシンでファイアウォール設定を変更する必要がある場合があります。

    2. 手動インストールからスタンドアロン・バイナリとしてOllamaを実行している場合は、サーバーを起動します:
      ollama serve
    3. ollama run <model_name>コマンドを使用してモデルを実行します。

      たとえば、Llama 3モデルをコールには:

      ollama run llama3

      この手順の詳細は、OllamaのReadmeを参照してください。

    4. cURLコマンドを使用して、Ollamaがローカルで実行されていることを確認します。

      次に例を示します。

      -- get summary 
      
      curl http://localhost:11434/api/generate -d '{
        "model" : "llama3",
        "prompt": "What is Oracle AI Vector Search?"
        "stream": false}'
  3. プロキシを設定します(存在する場合)。
    EXEC UTL_HTTP.SET_PROXY('<proxy-hostname>:<proxy-port>');
  4. DBMS_NETWORK_ACL_ADMINプロシージャを使用して、ホストへの接続を許可するための接続権限をdocuserに付与します。

    この例では、*を使用して任意のホストを許可します。ただし、接続するホストを明示的に指定できます。

    BEGIN
      DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
        host => '*',
        ace => xs$ace_type(privilege_list => xs$name_list('connect'),
                           principal_name => 'docuser',
                           principal_type => xs_acl.ptype_db));
    END;
    /
  5. UTL_TO_SUMMARYをコールします。

    Ollamaサービスには、テキストを要約するためのREST APIエンドポイントがあります。JSONオブジェクトにURLおよびその他の構成パラメータを指定します。

    var gent_ollama_params clob;
    exec :gent_ollama_params := '{
      "provider": "ollama",
      "host"    : "local",
      "url"     : "http://localhost:11434/api/generate", 
      "model"   : "llama3"
    }';
    
    select dbms_vector_chain.utl_to_summary(
      'A transaction is a logical, atomic unit of work that contains one or more SQL
        statements.
        An RDBMS must be able to group SQL statements so that they are either all
        committed, which means they are applied to the database, or all rolled back, which
        means they are undone.
        An illustration of the need for transactions is a funds transfer from a savings account to
        a checking account. The transfer consists of the following separate operations:
        1. Decrease the savings account.
        2. Increase the checking account.
        3. Record the transaction in the transaction journal.
        Oracle Database guarantees that all three operations succeed or fail as a unit. For
        example, if a hardware failure prevents a statement in the transaction from executing,
        then the other statements must be rolled back.
        Transactions set Oracle Database apart from a file system. If you
        perform an atomic operation that updates several files, and if the system fails halfway
        through, then the files will not be consistent. In contrast, a transaction moves an
        Oracle database from one consistent state to another. The basic principle of a
        transaction is "all or nothing": an atomic operation succeeds or fails as a whole.', 
      json(:gent_ollama_params)) from dual;

    必要に応じて、urlおよびmodelを独自の値に置き換えることができます。

    ノート:

    サポートされているすべてのRESTエンドポイントURLの完全なリストは、「サポートされているサードパーティ・プロバイダの操作およびエンドポイント」を参照してください。

    生成されたサマリーは次のように表示されます:

    A transaction in an RDBMS (Relational Database Management System) is a self-contained unit of work that 
    consists of one or more SQL statements. This ensures that all changes made by the transaction are either
    committed (applied to the database) or rolled back (undone). Oracle Database is specifically designed to
    manage transactions, ensuring database consistency and integrity. 
    Transactions differ from file systems in that they maintain atomicity, ensuring that all related operations
    succeed or fail as a whole, maintaining database consistency regardless of intermittent failures. 
    Transactions move a database from one consistent state to another, and the fundamental principle is that a 
    transaction is committed or rolled back as a whole, upholding the "all or nothing" principle.