ローカルRESTプロバイダOllamaを使用したテキストの生成

ローカル・ホストのRESTエンドポイント・プロバイダOllamaを使用して、オープンLLMにアクセスし、テキストからテキストへの変換を実行します。入力はテキスト・プロンプトで、生成される出力は、そのプロンプトで指定したタスクに基づくテキスト形式の回答または説明です。

プロンプトには、テキスト文字列(LLMに尋ねる質問、コマンドなど)を指定でき、検索の結果を含めることができます。

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

ここでは、ユースケースに応じて、DBMS_VECTORまたはDBMS_VECTOR_CHAINパッケージのUTL_TO_GENERATE_TEXT関数を使用できます。

警告:

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

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

Ollamaを使用してローカルLLMをコールし、プロンプト「What is Oracle Text?」の説明的な回答を生成するには:

  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がローカルで実行されていることを確認します。

      次に例を示します。

      -- generate text
      
      curl -X POST http://localhost:11434/api/generate -d '{
        "model" : "llama3",
        "prompt": "Why is the sky blue?",
        "stream": false }'
  3. HTTPプロキシ・サーバーを設定します(構成されている場合)
    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_GENERATE_TEXTをコールします。

    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.utl_to_generate_text('What is Oracle Text?', json(:gent_ollama_params)) from dual;

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

    ノート:

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

    プロンプトに対する応答は次のように表示されます:

    Oracle Text (formerly known as Oracle InterMedia) is a suite of text search and retrieval tools within Oracle Database. It allows you to index and query 
    unstructured text data, such as documents, emails, and other text-based content.
    
    With Oracle Text, you can:
    
    1. Index text: Create indexes on text columns or external files, making it possible to efficiently search and retrieve relevant text data.
    2. Query text: Use SQL syntax to query the indexed text data, allowing you to find specific words, phrases, or patterns within large volumes of 
    text.
    3. Full-text search: Perform full-text searches on unstructured text data, returning relevant results based on keyword matches, proximity, and 
    relevance.
    
    Oracle Text supports various indexing schemes, including:
    
    1. Basic Indexing: A simple, fast index for searching exact keywords.
    2. Phrase Indexing: An index that allows you to search for phrases (e.g., "John Smith").
    3. Thesaurus Indexing: An index that enables searches based on synonyms and related words.
    
    Oracle Text also includes various text analysis and processing features, such as:
    
    1. Tokenization: Breaking down text into individual words or tokens.
    2. Stemming: Reducing words to their base form (e.g., "running" becomes "run").
    3. Stopword removal: Eliminating common words like "the," "and," and "a" that don't add much value to the search.
    
    Oracle Text is particularly useful in scenarios where you need to search, analyze, or retrieve unstructured text data, such as:
    
    1. Content management: Searching and retrieving documents, articles, or other content.
    2. Email archiving: Indexing and searching email messages.
    3. Search engines: Building custom search solutions for specific domains or industries.
    
    In summary, Oracle Text is a powerful tool within the Oracle Database that enables you to index, query, and retrieve unstructured text data with ease.