パブリックRESTプロバイダを使用したイメージの説明

Google AI、Hugging Face、OpenAIまたはVertex AIによってパブリックにホストされたサードパーティのLLMを使用して、プロンプトとしてイメージをテキストの質問とともに指定することで、イメージからテキストへの変換を実行します。

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

警告:

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

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

外部LLMを使用して、次のイメージおよびテキストの質問「Describe this image?」をプロンプトに指定してテキスト応答を生成するには:

bird.jpgの説明が続きます
図bird.jpgの説明
  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. HTTPプロキシ・サーバーを設定します(構成されている場合)
    EXEC UTL_HTTP.SET_PROXY('<proxy-hostname>:<proxy-port>');
  3. 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;
    /
  4. イメージ・ファイルを格納するローカル・ディレクトリ(DEMO_DIR)を作成します:
    create or replace directory DEMO_DIR as '/my_local_dir/';
    
    create or replace function load_blob_from_file(directoryname varchar2, filename varchar2)
      return blob
    is
      filecontent blob := null;
      src_file bfile := bfilename(directoryname, filename);
      offset number := 1;
    begin
      dbms_lob.createtemporary(filecontent, true, dbms_lob.session);
      dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
      dbms_lob.loadblobfromfile(filecontent, src_file,
        dbms_lob.getlength(src_file), offset, offset);
      dbms_lob.fileclose(src_file);
      return filecontent;
    end;
    /

    イメージ・ファイル(bird.jpgなど)をディレクトリにアップロードします。

  5. アクセスするRESTプロバイダの資格証明を設定し、UTL_TO_GENERATE_TEXTをコールします。
    1. CREATE_CREDENTIALをコールして、資格証明を作成および格納します。

      Google AI、Hugging Face、OpenAIおよびVertex AIには、次の認証パラメータが必要です:

      { "access_token": "<access token>" }

      後で、UTL_TO_GENERATE_TEXTコールのJSONパラメータを宣言するときに、この資格証明名を参照します。

      exec dbms_vector_chain.drop_credential('<credential name>');
      declare
        jo json_object_t;
      begin
        jo := json_object_t();
        jo.put('access_token', '<access token>');
        dbms_vector_chain.create_credential(
          credential_name   => '<credential name>',
          params            => json(jo.to_string));
      end;
      /

      access_tokenおよびcredential_nameを独自の値に置き換えます。次に例を示します。

      declare
        jo json_object_t;
      begin
        jo := json_object_t();
        jo.put('access_token', 'AbabA1B123aBc123AbabAb123a1a2ab');
        dbms_vector_chain.create_credential(
          credential_name   => 'HF_CRED',
          params            => json(jo.to_string));
      end;
      /
    2. UTL_TO_GENERATE_TEXTをコールします。

      -- select example
      
      var input clob;
      var media_data blob;
      var media_type clob;
      var params clob;
      
      begin
        :input := 'Describe this image';
        :media_data := load_blob_from_file('DEMO_DIR', 'bird.jpg');
        :media_type := 'image/jpeg';
        :params := '
      {
        "provider"       : "<REST provider>",
        "credential_name": "<credential name>",
        "url"            : "<REST endpoint URL for text generation service>",
        "model"          : "<REST provider text generation model name>",
        "max_tokens"     : <maximum number of tokens in the output text>
      }';
      end;
      /
      
      select dbms_vector_chain.utl_to_generate_text(:input, :media_data, :media_type, json(:params));
      
      -- PL/SQL example
      
      declare
        input clob;
        media_data blob;
        media_type varchar2(32);
        params clob;
        output clob;
      begin
        input := 'Describe this image';
        media_data := load_blob_from_file('DEMO_DIR', 'bird.jpg');
        media_type := 'image/jpeg';
        params := '
      {
        "provider"       : "<REST provider>",
        "credential_name": "<credential name>",
        "url"            : "<REST endpoint URL for text generation service>",
        "model"          : "<REST provider text generation model name>",
        "max_tokens"     : <maximum number of tokens in the output text>
      }';
      
        output := dbms_vector_chain.utl_to_generate_text(
          input, media_data, media_type, json(params));
        dbms_output.put_line(output);
      
        if output is not null then
          dbms_lob.freetemporary(output);
        end if;
        if media_data is not null then
          dbms_lob.freetemporary(media_data);
        end if;
      exception
        when OTHERS THEN
          DBMS_OUTPUT.PUT_LINE (SQLERRM);
          DBMS_OUTPUT.PUT_LINE (SQLCODE);
      end;
      /

      ノート:

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

      providercredential_nameurlおよびmodelを独自の値に置き換えます。オプションで、追加のRESTプロバイダ・パラメータを指定できます。これを次の例に示します:

      Google AIの例:
      {
        "provider"       : "googleai",
        "credential_name": "GOOGLEAI_CRED",
        "url"            : "https://generativelanguage.googleapis.com/v1beta/models/",
        "model"          : "gemini-pro:generateContent"
      }
      Hugging Faceの例:
      {
        "provider"       : "huggingface",
        "credential_name": "HF_CRED",
        "url"            : "https://api-inference.huggingface.co/models/",
        "model"          : "gpt2"
      }

      ノート:

      Hugging Faceでは、プロンプトを必要としないイメージ・キャプション・モデルが使用されます。イメージとともにプロンプトを入力した場合、プロンプトは無視されます。
      OpenAIの例:
      {
        "provider"       : "openai",
        "credential_name": "OPENAI_CRED",
        "url"            : "https://api.openai.com/v1/chat/completions",
        "model"          : "gpt-4o-mini",
        "max_tokens"     : 60
      }
      Vertex AIの例:
      {
        "provider"         : "vertexai",
        "credential_name"  : "VERTEXAI_CRED",
        "url"              : "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT/locations/LOCATION/publishers/google/models/",
        "model"            : "gemini-1.0-pro:generateContent",
        "generation_config": {
                              "temperature"    : 0.9,
                              "topP"           : 1,
                              "candidateCount" : 1,
                              "maxOutputTokens": 256
                             }
      }

    質問に対して生成されたテキスト応答は次のように表示されます:

    This image showcases a stylized, artistic depiction of a hummingbird in
    mid-flight, set against a vibrant red background. The bird is illustrated with a
    mix of striking colors and details - its head and belly are shown in white, with
    a black patterned detailing that resembles stripes or scales. Its long, slender
    beak is depicted in a darker color, extending forwards. The hummingbird's wings
    and tail are rendered in eye-catching shades of orange, purple, and red, with
    texture that suggests a rough, perhaps brush-like stroke.
    
    The background features abstract shapes resembling clouds or wind currents in a white line
    pattern, which adds a sense of motion or air dynamics around the bird. The
    overall use of vivid colors and dynamic patterns gives the image an energetic
    and modern feel.
この例では、各プロバイダのデフォルト設定を使用しています。追加パラメータの詳細は、サードパーティ・プロバイダのドキュメントを参照してください。