パブリックRESTプロバイダを使用したテキストの生成

Cohere、生成AI、Google AI、Hugging Face、OpenAIまたはVertex AIによってパブリックにホストされたサードパーティのテキスト生成モデルを使用して、テキストからテキストへの変換を実行します。入力はテキスト・プロンプトで、生成される出力は、そのプロンプトで指定したタスクに基づくテキスト形式の回答または説明です。

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

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

警告:

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

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

外部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. プロキシを設定します(存在する場合)。
    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. アクセスするRESTプロバイダの資格証明を設定し、UTL_TO_GENERATE_TEXTをコールします。
    • 生成AIの使用:

      1. CREATE_CREDENTIALをコールして、OCI資格証明(OCI_CRED)を作成および格納します。

        生成AIには、次の認証パラメータが必要です。
        { 
        "user_ocid"       : "<user ocid>",
        "tenancy_ocid"    : "<tenancy ocid>",
        "compartment_ocid": "<compartment ocid>",
        "private_key"     : "<private key>",
        "fingerprint"     : "<fingerprint>" 
        }

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

        ノート:

        生成された秘密キーは次のように表示されます。
        -----BEGIN RSA PRIVATE KEY-----
        <private key string>
        -----END RSA PRIVATE KEY-----
        (BEGINENDの行を除く) <private key string>値を単一行または複数行として渡します。
        exec dbms_vector_chain.drop_credential('OCI_CRED');
        declare
          jo json_object_t;
        begin
          jo := json_object_t();
          jo.put('user_ocid','<user ocid>');
          jo.put('tenancy_ocid','<tenancy ocid>');
          jo.put('compartment_ocid','<compartment ocid>');
          jo.put('private_key','<private key>');
          jo.put('fingerprint','<fingerprint>');
          dbms_vector_chain.create_credential(
            credential_name   => 'OCI_CRED',
            params            => json(jo.to_string));
        end;
        /

        すべての認証パラメータ値を置換します。次に例を示します。

        declare
          jo json_object_t;
        begin
        
          -- create an OCI credential
          jo := json_object_t();
          jo.put('user_ocid','ocid1.user.oc1..aabbalbbaa1112233aabbaabb1111222aa1111bb');
          jo.put('tenancy_ocid','ocid1.tenancy.oc1..aaaaalbbbb1112233aaaabbaa1111222aaa111a');
          jo.put('compartment_ocid','ocid1.compartment.oc1..ababalabab1112233abababab1111222aba11ab');
          jo.put('private_key','AAAaaaBBB11112222333...AAA111AAABBB222aaa1a/+');
          jo.put('fingerprint','01:1a:a1:aa:12:a1:12:1a:ab:12:01:ab:a1:12:ab:1a');
          dbms_vector_chain.create_credential(
            credential_name   => 'OCI_CRED',
            params            => json(jo.to_string));
        end;
        /
      2. UTL_TO_GENERATE_TEXTをコールします。

        ここでは、cohere.command-r-16kモデルが使用されます。必要に応じて、modelを独自の値に置き換えることができます。

        ノート:

        生成AIでの使用がサポートされているすべてのRESTエンドポイントURLおよびモデルのリストは、「サポートされるサードパーティ・プロバイダの操作およびエンドポイント」を参照してください。
        -- select example
        
        var params clob;
        exec :params := '
        {
          "provider"       : "ocigenai",
          "credential_name": "OCI_CRED",
          "url"            : "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat",
          "model"          : "cohere.command-r-16k",
          "chatRequest"    : {
                              "maxTokens": 256
                             }
        }';
        
        select dbms_vector_chain.utl_to_generate_text(
         'What is Oracle Text?',json(:params)) from dual;
        
        -- PL/SQL example
        
        declare
          input clob;
          params clob;
          output clob;
        begin
          input := 'What is Oracle Text?';
        
          params := '
        {
          "provider"       : "ocigenai",
          "credential_name": "OCI_CRED",
          "url"            : "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/chat",
          "model"          : "cohere.command-r-16k",
          "chatRequest"    : {
                              "maxTokens": 256
                             }
        }';
        
          output := dbms_vector_chain.utl_to_generate_text(input, json(params));
          dbms_output.put_line(output);
          if output is not null then
            dbms_lob.freetemporary(output);
          end if;
        exception
          when OTHERS THEN
            DBMS_OUTPUT.PUT_LINE (SQLERRM);
            DBMS_OUTPUT.PUT_LINE (SQLCODE);
        end;
        /

        オプションで、RESTプロバイダ固有の追加パラメータを指定できます。

        ノート:

        追加のRESTプロバイダ固有のパラメータを渡す場合は、chatRequestでこれらを囲んで指定する必要があります。
    • Cohere、Google AI、Hugging Face、OpenAIおよびVertex AIの使用:

      1. CREATE_CREDENTIALをコールして、資格証明を作成および格納します。

        Cohere、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 params clob;
        exec :params := '
        {
          "provider"       : "<REST provider>",
          "credential_name": "<credential name>",
          "url"            : "<REST endpoint URL for text generation service>",
          "model"          : "<REST provider text generation model name>"
        }';
        
        select dbms_vector_chain.utl_to_generate_text(
         'What is Oracle Text?',json(:params)) from dual;
        
        -- PL/SQL example
        
        declare
          input clob;
          params clob;
          output clob;
        begin
          input := 'What is Oracle Text?';
        
          params := '
        {
          "provider"       : "<REST provider>",
          "credential_name": "<credential name>",
          "url"            : "<REST endpoint URL for text generation service>",
          "model"          : "<REST provider text generation model name>"
        }';
        
          output := dbms_vector_chain.utl_to_generate_text(input, json(params));
          dbms_output.put_line(output);
          if output is not null then
            dbms_lob.freetemporary(output);
          end if;
        exception
          when OTHERS THEN
            DBMS_OUTPUT.PUT_LINE (SQLERRM);
            DBMS_OUTPUT.PUT_LINE (SQLCODE);
        end;
        /

        ノート:

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

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

        Cohereの例:
        {
          "provider"       : "Cohere", 
          "credential_name": "COHERE_CRED",
          "url"            : "https://api.cohere.ai/v1/chat",
          "model"          : "command"
        }
        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"
        }
        OpenAIの例:
        {
          "provider"       : "openai",
          "credential_name": "OPENAI_CRED",
          "url"            : "https://api.openai.com/v1/chat/completions",
          "model"          : "gpt-4o-mini",
          "max_tokens"     : 60,
          "temperature"    : 1.0
        }
        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
                               }
        }

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

    BMS_VECTOR_CHAIN.UTL_TO_GENERATE_TEXT(:INPUT,JSON(:PARAMS))
    --------------------------------------------------------------------------------
    Oracle Text is a powerful tool that enhances Oracle Database with integrated 
    text mining and text analytics capabilities.
    
    It enables users to extract valuable insights and make informed decisions by 
    analyzing unstructured text data stored within the database.
    
    Here are some enhanced capabilities offered by Oracle Text:
    
    1. Full-Text Search: Enables powerful and rapid full-text searches across large
    collections of documents. This helps users find relevant information quickly
    and effectively, even within massive datasets.
    
    2. Natural Language Processing: Implements advanced language processing techn
    iques to analyze text and extract meaningful information. This includes capabilities
    like tokenization, stemming, lemmatization, and part-of-speech tagging, 
    which collectively facilitate efficient text processing and understanding.
    
    3. Sentiment Analysis: Provides a deeper understanding of sentiment expressed
     in text. It enables businesses to automatically analyze customer opinions, feed
    back, and reviews, helping them gain valuable insights into customer sentiment,
    satisfaction levels, and potential trends.
    
    4. Entity Recognition: Automatically identifies and categorizes entities with
    in text, such as names of people, organizations, locations, or any other specific
    terms of interest. This is useful in applications like customer relationship 
    management, where linking relevant information to individuals or organizations is
    crucial.
    
    5. Contextual Analysis: Delivers insights into the context and relationships
    between entities and concepts in textual data. It helps organizations better und
    erstand the broader implications and associations between entities, facilitating
     a deeper understanding of their data.
    
    These features collectively empower various applications, enhancing the function
    ality of the Oracle Database platform to allow businesses and organizations to 
    derive maximum value from their unstructured text data.
    
    Let me know if you'd like to dive deeper into any of these specific capabilities
    , or if there are other aspects of Oracle Text you'd like to explore further.
この例では、各プロバイダのデフォルト設定を使用しています。追加パラメータの詳細は、サードパーティ・プロバイダのドキュメントを参照してください。