OCI Generative AIコネクタを使用した会話型検索ウォークスルー

このウォークスルーのステップを使用して、OCI生成AIコネクタを使用して、OpenSearchを使用してOCI SearchでエンドツーエンドのRetrieval-Augmented Generation (RAG)パイプラインを設定および使用します。

コネクタは、生成AIがホストするCohere埋込みモデルを使用します。生成AIでは、Llma2モデルもサポートされており、これも試すことができます。

ステップ1: 前提条件

開始する前に、次のことを行う必要があります。

  • OCI生成AIを使用するには、テナンシがシカゴ・リージョンにサブスクライブされている必要があります。シカゴ・リージョンにクラスタを作成する必要はありません。テナンシがこのリージョンにサブスクライブされていることを確認してください。
  • OpenSearchクラスタがバージョン2.11であることを確認します。OCI SearchでOpenSearchを使用してOCI生成AIコネクタを使用するには、OpenSearchバージョン2.11を使用するように構成されたクラスタが必要です。デフォルトでは、新しいクラスタはバージョン2.11を使用するように構成されています。クラスタを作成するには、「OpenSearchクラスタの作成」を参照してください。

    バージョン2.3に構成された既存のクラスタでは、バージョン2.11へのインライン・アップグレードを実行できます。詳細は、OpenSearchクラスタ・ソフトウェアのアップグレードを参照してください。

    バージョン1.2.3用に構成された既存のクラスタを2.11にアップグレードするには、OpenSearchクラスタ・ソフトウェアのアップグレードで説明されているアップグレード・プロセスを使用する必要があります。

  • 生成AIリソースへのアクセス権を付与するポリシーを作成します。次のポリシーの例には、必要な権限が含まれています。

    ALLOW ANY-USER to manage generative-ai-family in tenancy WHERE ALL {request.principal.type='opensearchcluster', request.resource.compartment.id='<cluster_compartment_id>'}

    ポリシーを初めて使用する場合は、ポリシーの開始および共通ポリシーを参照してください。

  • クラスタ設定を更新して、コネクタを作成および使用します。次のコマンド例では、適用可能な設定を更新します。

    PUT _cluster/settings
    {
      "persistent": {
        "plugins": {
          "ml_commons": {
            "only_run_on_ml_node": "false",
            "model_access_control_enabled": "true",
            "native_memory_threshold": "99",
            "rag_pipeline_feature_enabled": "true",
            "memory_feature_enabled": "true",
            "allow_registering_model_via_local_file": "true",
            "allow_registering_model_via_url": "true",
            "model_auto_redeploy.enable":"true",
            "model_auto_redeploy.lifetime_retry_times": 10
          }
        }
      }
    }

ステップ2: モデル・グループの登録

次の例に示すように、モデル・グループAPI登録操作を使用してモデル・グループを登録します。

POST /_plugins/_ml/model_groups/_register
{
   "name": "public OCI GenAI model group",
   "description": "OCI GenAI group for remote models"
}

レスポンスで返されたmodel_group_idを書き留めます。

{
  "model_group_id": "<model_group_ID>",
  "status": "CREATED"
}

ステップ3: コネクタの作成

生成AIコネクタを作成します。actions/generateTextまたはactions/chatの2つのエンドポイント・オプションがあります。actions/chatモデルを使用することをお薦めします。

かわりにデータ・サイエンス・コネクタを使用するには、データ・サイエンス・コネクタの構成方法に関するドキュメントに従ってください。完了したら、このウォークスルーで「ステップ7: 検索索引の作成」に進んでください。

actions/chatエンドポイントオプション

  • cohere.command-r-plus:

    POST _plugins/_ml/connectors/_create
    {
         "name": "Cohere Commar-R-Plus Chat Connector",
         "description": "Check errors in logs",
         "version": 2,
         "protocol": "oci_sigv1",
         "parameters": {
             "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
             "auth_type": "resource_principal"
         },
         "credential": {
         },
         "actions": [
             {
                 "action_type": "predict",
                 "method": "POST",
                 "url": "https://${parameters.endpoint}/20231130/actions/chat",
                 "request_body": "{\"compartmentId\":\"<cluster_compartment_id>\",\"servingMode\":{\"modelId\":\"cohere.command-r-plus\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"message\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":0,\"isStream\":false,\"chatHistory\":[],\"apiFormat\":\"COHERE\"}}",
                 "post_process_function": "def text = params['chatResponse']['text'].replace('\n', '\\\\n').replace('\"','');\n return '{\"name\":\"response\",\"dataAsMap\":{\"inferenceResponse\":{\"generatedTexts\":[{\"text\":\"' + text + '\"}]}}}'"
     
             }
         ]
     }
  • cohere.command-r-16kモデル:

    POST _plugins/_ml/connectors/_create
    {
         "name": "Cohere Chat Connector",
         "description": "Check errors in logs",
         "version": 2,
         "protocol": "oci_sigv1",
         "parameters": {
             "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
             "auth_type": "resource_principal"
         },
         "credential": {
         },
         "actions": [
             {
                 "action_type": "predict",
                 "method": "POST",
                 "url": "https://${parameters.endpoint}/20231130/actions/chat",
                 "request_body": "{\"compartmentId\":\"<cluster_compartment_id>\",\"servingMode\":{\"modelId\":\"cohere.command-r-16k\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"message\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":0,\"isStream\":false,\"chatHistory\":[],\"apiFormat\":\"COHERE\"}}",
                 "post_process_function": "def text = params['chatResponse']['text'].replace('\n', '\\\\n').replace('\"','');\n return '{\"name\":\"response\",\"dataAsMap\":{\"inferenceResponse\":{\"generatedTexts\":[{\"text\":\"' + text + '\"}]}}}'"
     
             }
         ]
     }
  • meta.llama-3-70b-instructモデル:

    POST _plugins/_ml/connectors/_create
    {
         "name": "Llama3 Chat Connector",
         "description": "Check errors in logs",
         "version": 2,
         "protocol": "oci_sigv1",
         "parameters": {
             "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
             "auth_type": "resource_principal"
         },
         "credential": {
         },
         "actions": [
             {
                 "action_type": "predict",
                 "method": "POST",
                 "url": "https://${parameters.endpoint}/20231130/actions/chat",
                 "request_body": "{\"compartmentId\":\<cluster_compartment_id>\",\"servingMode\":{\"modelId\":\"meta.llama-3-70b-instruct\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":-1,\"isStream\":false,\"apiFormat\":\"GENERIC\",\"messages\":[{\"role\":\"USER\",\"content\":[{\"type\":\"TEXT\",\"text\":\"${parameters.prompt}\"}]}]}}",
                 
                  "post_process_function": "def text = params['chatResponse']['choices'][0]['message']['content'][0]['text'].replace('\n', '\\\\n').replace('\"','');\n return '{\"name\":\"response\",\"dataAsMap\":{\"inferenceResponse\":{\"generatedTexts\":[{\"text\":\"' + text + '\"}]}}}'"
    
    
    
             }
         ]
     }

認証はリソース・プリンシパルを使用して行われます。クラスタのコンパートメントIDをrequest_bodyに指定します。

レスポンスで返されたconnector_idを書き留めます。

{
  "connector_id": "<connector_ID>",
}

actions/generateText Endpointオプション

  • cohere.commandモデル:

    POST _plugins/_ml/connectors/_create
    {
         "name": "OpenAI Chat Connector",
         "description": "when did us pass espio",
         "version": 2,
         "protocol": "oci_sigv1",
         "parameters": {
             "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
             "auth_type": "resource_principal"
         },
         "credential": {
         },
         "actions": [
             {
                 "action_type": "predict",
                 "method": "POST",
                 "url": "https://${parameters.endpoint}/20231130/actions/generateText",
                 "request_body": "{\"compartmentId\":\"<cluster_compartment_id>\",\"servingMode\":{\"modelId\":\"cohere.command\",\"servingType\":\"ON_DEMAND\"},\"inferenceRequest\":{\"prompt\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":0,\"returnLikelihoods\":\"GENERATION\",\"isStream\":false ,\"stopSequences\":[],\"runtimeType\":\"COHERE\"}}"
             }
         ]
     }
  • meta.llama-2-70b-chatモデル

    POST _plugins/_ml/connectors/_create
    {
         "name": "OpenAI Chat Connector",
         "description": "testing genAI connector",
         "version": 2,
         "protocol": "oci_sigv1",
         "parameters": {
             "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
             "auth_type": "resource_principal"
         },
         "credential": {
         },
         "actions": [
             {
                 "action_type": "predict",
                 "method": "POST",
                 "url": "https://${parameters.endpoint}/20231130/actions/generateText",
                 "request_body": "{\"compartmentId\":\"<cluster_compartment_id>\",\"servingMode\":{\"modelId\":\"meta.llama-2-70b-chat\",\"servingType\":\"ON_DEMAND\"},\"inferenceRequest\":{\"prompt\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":-1,\"isStream\":false,\"numGenerations\":1,\"stop\":[],\"runtimeType\":\"LLAMA\"}}",
                            "post_process_function": "def text = params['inferenceResponse']['choices'][0]['text'].replace('\n', '\\\\n').replace('\"','');\n return '{\"name\":\"response\",\"dataAsMap\":{\"inferenceResponse\":{\"generatedTexts\":[{\"text\":\"' + text + '\"}]}}}'"
                              
                   }
         ]
     }

認証はリソース・プリンシパルを使用して行われます。クラスタのコンパートメントIDをrequest_bodyに指定します。

レスポンスで返されたconnector_idを書き留めます。

{
  "connector_id": "<connector_ID>",
}

専用生成AIモデル・エンドポイント・オプション

専用の生成AIモデル・エンドポイントを使用するには、次の変更でコネクタ・ペイロードを再構成します。

  1. modelIdのかわりにendpointIdを使用し、モデル名のかわりに専用モデル・エンドポイントのOCIDを指定します。たとえば、次のように変更します。
    \"modelId\":\"meta.llama-2-70b-chat\"
    変更後:
    \"endpointId\":\"<dedicated_model_enpoint_OCID>\"
  2. servingTypeON_DEMANDからDEDICATEDに変更します。たとえば、次のように変更します。

    \"servingType\":\"ON_DEMAND\"
    変更後:
    \"servingType\":\"DEDICATED\"

次に、専用モデル・エンドポイントを使用してコネクタを作成する方法を示す完全な例を示します。

POST _plugins/_ml/connectors/_create
{
     "name": "Cohere Commar-R-Plus Chat Connector",
     "description": "Check errors in logs",
     "version": 2,
     "protocol": "oci_sigv1",
     "parameters": {
         "endpoint": "inference.generativeai.us-chicago-1.oci.oraclecloud.com",
         "auth_type": "resource_principal"
     },
     "credential": {
     },
     "actions": [
         {
             "action_type": "predict",
             "method": "POST",
             "url": "https://${parameters.endpoint}/20231130/actions/chat",
             "request_body": "{\"compartmentId\":\"<cluster_compartment_id>\",\"servingMode\":{\"endpointId\":\"<dedicated_model_enpoint_OCID>\",\"servingType\":\"DEDICATED\"},\"chatRequest\":{\"message\":\"${parameters.prompt}\",\"maxTokens\":600,\"temperature\":1,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":0.75,\"topK\":0,\"isStream\":false,\"chatHistory\":[],\"apiFormat\":\"COHERE\"}}",
             "post_process_function": "def text = params['chatResponse']['text'].replace('\n', '\\\\n').replace('\"','');\n return '{\"name\":\"response\",\"dataAsMap\":{\"inferenceResponse\":{\"generatedTexts\":[{\"text\":\"' + text + '\"}]}}}'"
 
         }
     ]
 }

ステップ4: モデルの登録

次の例に示すように、生成AIコネクタを使用して、前のステップのコネクタIDおよびモデル・グループIDでリモート・モデルを登録します。

POST /_plugins/_ml/models/_register
{
   "name": "oci-genai-conversation",
   "function_name": "remote",
   "model_group_id": "<model_group_ID>",
   "description": "test semantic",
   "connector_id": "<connector_ID>"
 }

レスポンスで返されたmodel_idを書き留めます。

{
  "task_id": "<task_ID>",
  "status": "CREATED",
  "model_id": "<llm_model_ID>"
}

ステップ5: モデルのデプロイ

次の例に示すように、前のステップのモデルIDを使用してモデルをクラスタにデプロイし、任意のパイプラインで使用できるようにします。

POST /_plugins/_ml/models/<llm_model_ID>/_deploy

次のようなレスポンスが返されます。

{
  "task_id": "<task_ID>",
  "task_type": "DEPLOY_MODEL",
  "status": "COMPLETED"
}

ステップ6: RAGパイプラインの作成

次の例に示すように、前のステップのmodel_idを使用してRAGパイプラインを作成します。

PUT /_search/pipeline/demo_rag_pipeline
{
  "response_processors": [
    {
      "retrieval_augmented_generation": {
        "tag": "genai_conversational_search_demo",
        "description": "Demo pipeline for conversational search Using Genai Connector",
        "model_id": "<llm_model_ID>",
        "conversation_id": "<conversation_ID>",
        "context_field_list": ["<text_field_name>"],
        "system_prompt":"hepfull assistant",
        "user_instructions":"generate concise answer"
      }
    }
  ]
}

ステップ7: 検索索引の作成

RAGパイプラインの作成後、任意の索引に対してRAGおよび会話型検索を実行できます。また、事前トレーニング済モデルでデータ取込みパイプラインを活用して、取得の一部としてハイブリッド検索を使用することもできます。

k-NNプラグインなしで検索インデックスを作成

この項では、取込みパイプラインを使用せずに索引を作成するステップについて説明します。

このオプションを使用して、埋込みせずにデータを作成します。

  1. 次の例に示すように、検索索引を作成します。

    PUT /conversation-demo-index
    {
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 0
        }
      },
      "mappings": {
        "properties": {
          "title": {
            "type": "text"
          },
          "text": {
            "type": "text"
          }
        }
      }
    }
  2. 次の例に示すように、データを索引に収集します。

    PUT /conversation-demo-index/_doc/1
    {
        "text": "The emergence of resistance of bacteria to antibiotics is a common phenomenon. Emergence of resistance often reflects evolutionary processes that take place during antibiotic therapy. The antibiotic treatment may select for bacterial strains with physiologically or genetically enhanced capacity to survive high doses of antibiotics. Under certain conditions, it may result in preferential growth of resistant bacteria, while growth of susceptible bacteria is inhibited by the drug. For example, antibacterial selection for strains having previously acquired antibacterial-resistance genes was demonstrated in 1943 by the Luria–Delbrück experiment. Antibiotics such as penicillin and erythromycin, which used to have a high efficacy against many bacterial species and strains, have become less effective, due to the increased resistance of many bacterial strains."
       
    }
    GET /conversation-demo-index/_doc/1
    PUT /conversation-demo-index/_doc/2
    {
      "text": "The successful outcome of antimicrobial therapy with antibacterial compounds depends on several factors. These include host defense mechanisms, the location of infection, and the pharmacokinetic and pharmacodynamic properties of the antibacterial. A bactericidal activity of antibacterials may depend on the bacterial growth phase, and it often requires ongoing metabolic activity and division of bacterial cells. These findings are based on laboratory studies, and in clinical settings have also been shown to eliminate bacterial infection. Since the activity of antibacterials depends frequently on its concentration, in vitro characterization of antibacterial activity commonly includes the determination of the minimum inhibitory concentration and minimum bactericidal concentration of an antibacterial. To predict clinical outcome, the antimicrobial activity of an antibacterial is usually combined with its pharmacokinetic profile, and several pharmacological parameters are used as markers of drug efficacy."
    }
     
    PUT /conversation-demo-index/_doc/3
    {
      "text": "Antibacterial antibiotics are commonly classified based on their mechanism of action, chemical structure, or spectrum of activity. Most target bacterial functions or growth processes. Those that target the bacterial cell wall (penicillins and cephalosporins) or the cell membrane (polymyxins), or interfere with essential bacterial enzymes (rifamycins, lipiarmycins, quinolones, and sulfonamides) have bactericidal activities. Those that target protein synthesis (macrolides, lincosamides and tetracyclines) are usually bacteriostatic (with the exception of bactericidal aminoglycosides). Further categorization is based on their target specificity. Narrow-spectrum antibacterial antibiotics target specific types of bacteria, such as Gram-negative or Gram-positive bacteria, whereas broad-spectrum antibiotics affect a wide range of bacteria. Following a 40-year hiatus in discovering new classes of antibacterial compounds, four new classes of antibacterial antibiotics have been brought into clinical use in the late 2000s and early 2010s: cyclic lipopeptides (such as daptomycin), glycylcyclines (such as tigecycline), oxazolidinones (such as linezolid), and lipiarmycins (such as fidaxomicin)"
    }
     
     
    PUT /conversation-demo-index/_doc/4
    {
      "text": "The Desert Land Act of 1877 was passed to allow settlement of arid lands in the west and allotted 640 acres (2.6 km2) to settlers for a fee of $.25 per acre and a promise to irrigate the land. After three years, a fee of one dollar per acre would be paid and the land would be owned by the settler. This act brought mostly cattle and sheep ranchers into Montana, many of whom grazed their herds on the Montana prairie for three years, did little to irrigate the land and then abandoned it without paying the final fees. Some farmers came with the arrival of the Great Northern and Northern Pacific Railroads throughout the 1880s and 1890s, though in relatively small numbers"
    }
     
    PUT /conversation-demo-index/_doc/5
    {
      "text": "In the early 1900s, James J. Hill of the Great Northern began promoting settlement in the Montana prairie to fill his trains with settlers and goods. Other railroads followed suit. In 1902, the Reclamation Act was passed, allowing irrigation projects to be built in Montana's eastern river valleys. In 1909, Congress passed the Enlarged Homestead Act that expanded the amount of free land from 160 to 320 acres (0.6 to 1.3 km2) per family and in 1912 reduced the time to prove up on a claim to three years. In 1916, the Stock-Raising Homestead Act allowed homesteads of 640 acres in areas unsuitable for irrigation.  This combination of advertising and changes in the Homestead Act drew tens of thousands of homesteaders, lured by free land, with World War I bringing particularly high wheat prices. In addition, Montana was going through a temporary period of higher-than-average precipitation. Homesteaders arriving in this period were known as Honyockers, or scissorbills. Though the word honyocker, possibly derived from the ethnic slur hunyak, was applied in a derisive manner at homesteaders as being greenhorns, new at his business or unprepared, the reality was that a majority of these new settlers had previous farming experience, though there were also many who did not"
    }
     
    PUT /conversation-demo-index/_doc/6
    {
      "text": "In June 1917, the U.S. Congress passed the Espionage Act of 1917 which was later extended by the Sedition Act of 1918, enacted in May 1918. In February 1918, the Montana legislature had passed the Montana Sedition Act, which was a model for the federal version. In combination, these laws criminalized criticism of the U.S. government, military, or symbols through speech or other means. The Montana Act led to the arrest of over 200 individuals and the conviction of 78, mostly of German or Austrian descent. Over 40 spent time in prison. In May 2006, then-Governor Brian Schweitzer posthumously issued full pardons for all those convicted of violating the Montana Sedition Act."
    }
     
    PUT /conversation-demo-index/_doc/7
    {
      "text": "When the U.S. entered World War II on December 8, 1941, many Montanans already had enlisted in the military to escape the poor national economy of the previous decade. Another 40,000-plus Montanans entered the armed forces in the first year following the declaration of war, and over 57,000 joined up before the war ended. These numbers constituted about 10 percent of the state's total population, and Montana again contributed one of the highest numbers of soldiers per capita of any state. Many Native Americans were among those who served, including soldiers from the Crow Nation who became Code Talkers. At least 1500 Montanans died in the war. Montana also was the training ground for the First Special Service Force or Devil's Brigade a joint U.S-Canadian commando-style force that trained at Fort William Henry Harrison for experience in mountainous and winter conditions before deployment. Air bases were built in Great Falls, Lewistown, Cut Bank and Glasgow, some of which were used as staging areas to prepare planes to be sent to allied forces in the Soviet Union. During the war, about 30 Japanese balloon bombs were documented to have landed in Montana, though no casualties nor major forest fires were attributed to them"
    }

このプロシージャでは、この索引で取込みパイプラインが使用されないため、取込み中にテキスト・ドキュメントに対して埋込みは生成されません。これは、関連するドキュメントを取得するためにBM25検索のみが使用されることを意味します。

k-NNプラグインなしで検索索引を作成した後、次のコマンドを実行して索引付きドキュメントをチェックすると、次のようにテキストのみが返されます。

要求:

GET /conversation-demo-index/_doc/1

レスポンス:

{
  "_index": "conversation-demo-index",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "text": "The emergence of resistance of bacteria to antibiotics is a common phenomenon. Emergence of resistance often reflects evolutionary processes that take place during antibiotic therapy. The antibiotic treatment may select for bacterial strains with physiologically or genetically enhanced capacity to survive high doses of antibiotics. Under certain conditions, it may result in preferential growth of resistant bacteria, while growth of susceptible bacteria is inhibited by the drug. For example, antibacterial selection for strains having previously acquired antibacterial-resistance genes was demonstrated in 1943 by the Luria–Delbrück experiment. Antibiotics such as penicillin and erythromycin, which used to have a high efficacy against many bacterial species and strains, have become less effective, due to the increased resistance of many bacterial strains."
  }
}

KNNプラグインで検索インデックスを作成

BM25のかわりにハイブリッド検索を利用してRAGパイプラインのリトリバー部分をエンリッチするには、取込みパイプラインを設定し、インポートされた事前トレーニング済モデルを使用して、取込み時にドキュメント埋込みが作成されるようにする必要があります。

このオプションを使用して、取込みパイプラインを含む索引を作成し、取込み中にデータの埋込みを自動的に生成します。

  1. このウォークスルーでは、次のいずれかのオプションを使用します。

    • オプション1: 「OpenSearch事前トレーニング済モデルの使用」で説明されているステップを使用して、OCI Searchでホストされている事前トレーニング済モデルをOpenSearchに登録してデプロイします。このオプションは最も簡単に使用でき、追加のIAMポリシーを構成する必要はなく、ペイロードは次のオプションのペイロードほど複雑ではありません。

    • オプション2: カスタム・モデルで説明されているステップを使用して、OpenSearch事前トレーニング済モデルをインポート、登録およびデプロイします。これには、オブジェクト・ストレージ・バケットへのモデル・ファイルのアップロード、およびモデルの登録時にモデル・ファイルのオブジェクト・ストレージURLの指定が含まれます。

    • オプション3: 生成AIコネクタを作成して、cohere.embed-english-v3.0 modelなどのリモート埋込みモデルを登録します。詳細は、OCI生成AIによる会話型検索を参照してください。

    モデルの登録およびデプロイ時に返されたモデルIDを書き留めます。

  2. 次の例に示すように、前のステップのmodel idを使用して取込みパイプラインを作成します。

    PUT _ingest/pipeline/minil12-test-pipeline
    {
      "description": "pipeline for RAG demo index",
      "processors" : [
        {
          "text_embedding": {
            "model_id": "<embedding_model_ID>",
            "field_map": {
               "text": "passage_embedding"
            }
          }
        }
      ]
    }
  3. 次の例に示すように、取込みパイプラインを使用して検索索引を作成します。

    PUT /conversation-demo-index-knn
    {
        "settings": {
            "index.knn": true,
            "default_pipeline": "minil12-test-pipeline"
        },
        "mappings": {
            "properties": {
                "passage_embedding": {
                    "type": "knn_vector",
                    "dimension": <model_dimension>,
                    "method": {
                        "name":"hnsw",
                        "engine":"lucene",
                        "space_type": "l2",
                        "parameters":{
                            "m":512,
                            "ef_construction": 245
                        }
                    }
                },
                "text": {
                    "type": "text"
                }
            }
        }
    }
  4. 次の例に示すように、前のステップの取込みパイプラインを使用してデータを取り込みます。

    PUT /conversation-demo-index-knn/_doc/1
    {
        "text": "The emergence of resistance of bacteria to antibiotics is a common phenomenon. Emergence of resistance often reflects evolutionary processes that take place during antibiotic therapy. The antibiotic treatment may select for bacterial strains with physiologically or genetically enhanced capacity to survive high doses of antibiotics. Under certain conditions, it may result in preferential growth of resistant bacteria, while growth of susceptible bacteria is inhibited by the drug. For example, antibacterial selection for strains having previously acquired antibacterial-resistance genes was demonstrated in 1943 by the Luria–Delbrück experiment. Antibiotics such as penicillin and erythromycin, which used to have a high efficacy against many bacterial species and strains, have become less effective, due to the increased resistance of many bacterial strains."
       
    }
    GET /conversation-demo-index-knn/_doc/1
    PUT /conversation-demo-index-knn/_doc/2
    {
      "text": "The successful outcome of antimicrobial therapy with antibacterial compounds depends on several factors. These include host defense mechanisms, the location of infection, and the pharmacokinetic and pharmacodynamic properties of the antibacterial. A bactericidal activity of antibacterials may depend on the bacterial growth phase, and it often requires ongoing metabolic activity and division of bacterial cells. These findings are based on laboratory studies, and in clinical settings have also been shown to eliminate bacterial infection. Since the activity of antibacterials depends frequently on its concentration, in vitro characterization of antibacterial activity commonly includes the determination of the minimum inhibitory concentration and minimum bactericidal concentration of an antibacterial. To predict clinical outcome, the antimicrobial activity of an antibacterial is usually combined with its pharmacokinetic profile, and several pharmacological parameters are used as markers of drug efficacy."
    }
     
    PUT /conversation-demo-index-knn/_doc/3
    {
      "text": "Antibacterial antibiotics are commonly classified based on their mechanism of action, chemical structure, or spectrum of activity. Most target bacterial functions or growth processes. Those that target the bacterial cell wall (penicillins and cephalosporins) or the cell membrane (polymyxins), or interfere with essential bacterial enzymes (rifamycins, lipiarmycins, quinolones, and sulfonamides) have bactericidal activities. Those that target protein synthesis (macrolides, lincosamides and tetracyclines) are usually bacteriostatic (with the exception of bactericidal aminoglycosides). Further categorization is based on their target specificity. Narrow-spectrum antibacterial antibiotics target specific types of bacteria, such as Gram-negative or Gram-positive bacteria, whereas broad-spectrum antibiotics affect a wide range of bacteria. Following a 40-year hiatus in discovering new classes of antibacterial compounds, four new classes of antibacterial antibiotics have been brought into clinical use in the late 2000s and early 2010s: cyclic lipopeptides (such as daptomycin), glycylcyclines (such as tigecycline), oxazolidinones (such as linezolid), and lipiarmycins (such as fidaxomicin)"
    }
     
     
    PUT /conversation-demo-index-knn/_doc/4
    {
      "text": "The Desert Land Act of 1877 was passed to allow settlement of arid lands in the west and allotted 640 acres (2.6 km2) to settlers for a fee of $.25 per acre and a promise to irrigate the land. After three years, a fee of one dollar per acre would be paid and the land would be owned by the settler. This act brought mostly cattle and sheep ranchers into Montana, many of whom grazed their herds on the Montana prairie for three years, did little to irrigate the land and then abandoned it without paying the final fees. Some farmers came with the arrival of the Great Northern and Northern Pacific Railroads throughout the 1880s and 1890s, though in relatively small numbers"
    }
     
    PUT /conversation-demo-index-knn/_doc/5
    {
      "text": "In the early 1900s, James J. Hill of the Great Northern began promoting settlement in the Montana prairie to fill his trains with settlers and goods. Other railroads followed suit. In 1902, the Reclamation Act was passed, allowing irrigation projects to be built in Montana's eastern river valleys. In 1909, Congress passed the Enlarged Homestead Act that expanded the amount of free land from 160 to 320 acres (0.6 to 1.3 km2) per family and in 1912 reduced the time to prove up on a claim to three years. In 1916, the Stock-Raising Homestead Act allowed homesteads of 640 acres in areas unsuitable for irrigation.  This combination of advertising and changes in the Homestead Act drew tens of thousands of homesteaders, lured by free land, with World War I bringing particularly high wheat prices. In addition, Montana was going through a temporary period of higher-than-average precipitation. Homesteaders arriving in this period were known as Honyockers, or scissorbills. Though the word honyocker, possibly derived from the ethnic slur hunyak, was applied in a derisive manner at homesteaders as being greenhorns, new at his business or unprepared, the reality was that a majority of these new settlers had previous farming experience, though there were also many who did not"
    }
     
    PUT /conversation-demo-index-knn/_doc/6
    {
      "text": "In June 1917, the U.S. Congress passed the Espionage Act of 1917 which was later extended by the Sedition Act of 1918, enacted in May 1918. In February 1918, the Montana legislature had passed the Montana Sedition Act, which was a model for the federal version. In combination, these laws criminalized criticism of the U.S. government, military, or symbols through speech or other means. The Montana Act led to the arrest of over 200 individuals and the conviction of 78, mostly of German or Austrian descent. Over 40 spent time in prison. In May 2006, then-Governor Brian Schweitzer posthumously issued full pardons for all those convicted of violating the Montana Sedition Act."
    }
     
    PUT /conversation-demo-index-knn/_doc/7
    {
      "text": "When the U.S. entered World War II on December 8, 1941, many Montanans already had enlisted in the military to escape the poor national economy of the previous decade. Another 40,000-plus Montanans entered the armed forces in the first year following the declaration of war, and over 57,000 joined up before the war ended. These numbers constituted about 10 percent of the state's total population, and Montana again contributed one of the highest numbers of soldiers per capita of any state. Many Native Americans were among those who served, including soldiers from the Crow Nation who became Code Talkers. At least 1500 Montanans died in the war. Montana also was the training ground for the First Special Service Force or Devil's Brigade a joint U.S-Canadian commando-style force that trained at Fort William Henry Harrison for experience in mountainous and winter conditions before deployment. Air bases were built in Great Falls, Lewistown, Cut Bank and Glasgow, some of which were used as staging areas to prepare planes to be sent to allied forces in the Soviet Union. During the war, about 30 Japanese balloon bombs were documented to have landed in Montana, though no casualties nor major forest fires were attributed to them"
    }

k-NNプラグインを使用して検索索引を作成した後、次のコマンドを実行して索引付きドキュメントをチェックすると、レスポンスに次のように埋込みが含まれます。

要求:

GET /conversation-demo-index-knn/_doc/1

レスポンス:

{
  "_index": "conversation-demo-index-knn",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "passage_embedding": [
      -0.02929831,
      -0.04421867,
      -0.10647401,
      0.07105031,
      0.004921746,
      -0.04529944,
      -0.092778176,
      0.14189903,
      -0.0016610072,
      0.08001712,
      0.053442925,
      -0.022703059,
      0.039608333,
      0.042299673,
      ..............

会話IDの作成

次のコマンドを実行して、会話IDを作成します:

POST /_plugins/_ml/memory/conversation
{
  "name": "rag-conversation"
}

レスポンス:

{
  "conversation_id": "<conversation_ID>"
}

BM25を使用してRAGを実行します

ドキュメントが索引付けされたら、次の例に示すように、「ステップ6: RAGパイプラインの作成」で指定されたRAGパイプラインの名前を使用してRAGを実行できます。

データ・サイエンス・コネクタを使用している場合は、"llm_model"値を"oci_datascience/<your_llm_model_name>"に変更する必要があります。

GET /conversation-demo-index/_search?search_pipeline=<pipeline_name>
{
    "query": {
        "match": {
            "text": {
                "query": "when did us pass espionage act?"
            }
        }
    },
    "ext": {
        "generative_qa_parameters": {
            "llm_model": "oci_genai/<your_llm_model_name>",
            "llm_question": "when did us pass espionage act? answer only in two sentences using provided context.",
            "conversation_id": "<conversation_ID>",
            "context_size": 2,
            "interaction_size": 1,
            "timeout": 15
        }
    }
}

最初の部分は、ユーザーの問合せに基づいて最も関連性の高いドキュメントを見つけるための索引への問合せによる取得です。2番目の部分では、より多くの命令でユーザー問合せが拡張され、取得部分からのレスポンスがコンテキストとして使用されます。これらはすべて、ユースケース・データのナレッジのみを使用して回答を生成するために、大規模言語モデルに渡されます。

<your_llm_model_name>「ステップ3: コネクタの作成」から、ペイロードで使用したモデル名を指定します。次に、モデル名の例を示します。

  • oci_genai/cohere.command-r-plus
  • oci_genai/cohere.command
  • oci_genai/cohere.command-r-16k
  • oci_genai/meta.llama-2-70b-chat

次のレスポンスでは、最初の部分は、リトリーバ(OpenSearchの検索エンジン)によって返される一連のドキュメントです。この後に、大規模言語モデル(LLM)の回答が続きます。これは、LLMのナレッジ・ベースを強化するために、テキスト生成とコンテキスト内学習を使用したLLMモデルからの実際のレスポンスです。ここで使用される検索エンジンはBM25です。これは、conversation-demo-index索引が、取込み中にデプロイされたモデルを使用してドキュメント埋込みを生成しないためです。

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 3.6364775,
    "hits": [
      {
        "_index": "conversation-demo-index",
        "_id": "6",
        "_score": 3.6364775,
        "_source": {
          "text": "In June 1917, the U.S. Congress passed the Espionage Act of 1917 which was later extended by the Sedition Act of 1918, enacted in May 1918. In February 1918, the Montana legislature had passed the Montana Sedition Act, which was a model for the federal version. In combination, these laws criminalized criticism of the U.S. government, military, or symbols through speech or other means. The Montana Act led to the arrest of over 200 individuals and the conviction of 78, mostly of German or Austrian descent. Over 40 spent time in prison. In May 2006, then-Governor Brian Schweitzer posthumously issued full pardons for all those convicted of violating the Montana Sedition Act."
        }
      },
      {
        "_index": "conversation-demo-index",
        "_id": "4",
        "_score": 2.675274,
        "_source": {
          "text": "The Desert Land Act of 1877 was passed to allow settlement of arid lands in the west and allotted 640 acres (2.6 km2) to settlers for a fee of $.25 per acre and a promise to irrigate the land. After three years, a fee of one dollar per acre would be paid and the land would be owned by the settler. This act brought mostly cattle and sheep ranchers into Montana, many of whom grazed their herds on the Montana prairie for three years, did little to irrigate the land and then abandoned it without paying the final fees. Some farmers came with the arrival of the Great Northern and Northern Pacific Railroads throughout the 1880s and 1890s, though in relatively small numbers"
        }
      },
      {
        "_index": "conversation-demo-index",
        "_id": "5",
        "_score": 2.5380564,
        "_source": {
          "text": "In the early 1900s, James J. Hill of the Great Northern began promoting settlement in the Montana prairie to fill his trains with settlers and goods. Other railroads followed suit. In 1902, the Reclamation Act was passed, allowing irrigation projects to be built in Montana's eastern river valleys. In 1909, Congress passed the Enlarged Homestead Act that expanded the amount of free land from 160 to 320 acres (0.6 to 1.3 km2) per family and in 1912 reduced the time to prove up on a claim to three years. In 1916, the Stock-Raising Homestead Act allowed homesteads of 640 acres in areas unsuitable for irrigation.  This combination of advertising and changes in the Homestead Act drew tens of thousands of homesteaders, lured by free land, with World War I bringing particularly high wheat prices. In addition, Montana was going through a temporary period of higher-than-average precipitation. Homesteaders arriving in this period were known as Honyockers, or scissorbills. Though the word honyocker, possibly derived from the ethnic slur hunyak, was applied in a derisive manner at homesteaders as being greenhorns, new at his business or unprepared, the reality was that a majority of these new settlers had previous farming experience, though there were also many who did not"
        }
      },
      {
        "_index": "conversation-demo-index",
        "_id": "7",
        "_score": 1.4905708,
        "_source": {
          "text": "When the U.S. entered World War II on December 8, 1941, many Montanans already had enlisted in the military to escape the poor national economy of the previous decade. Another 40,000-plus Montanans entered the armed forces in the first year following the declaration of war, and over 57,000 joined up before the war ended. These numbers constituted about 10 percent of the state's total population, and Montana again contributed one of the highest numbers of soldiers per capita of any state. Many Native Americans were among those who served, including soldiers from the Crow Nation who became Code Talkers. At least 1500 Montanans died in the war. Montana also was the training ground for the First Special Service Force or Devil's Brigade a joint U.S-Canadian commando-style force that trained at Fort William Henry Harrison for experience in mountainous and winter conditions before deployment. Air bases were built in Great Falls, Lewistown, Cut Bank and Glasgow, some of which were used as staging areas to prepare planes to be sent to allied forces in the Soviet Union. During the war, about 30 Japanese balloon bombs were documented to have landed in Montana, though no casualties nor major forest fires were attributed to them"
        }
      }
    ]
  },
  "ext": {
    "retrieval_augmented_generation": {
      "answer": " Through the Espionage Act of 1917 and the Sedition Act of 1918, passed in May of 1918, the U.S. Congress outlawed criticism of the military, government, or symbols through speech or other means. The Montana Sedition Act was enacted in February 1918, serving as a model for the federal version, and resulting in the arrest of over 200 people, chiefly of German or Austrian descent, with over 40 imprisoned. "
    }
  }
}

ハイブリッド検索を使用したRAGの実行

また、レトリバーとしてハイブリッド検索でRAGを実行することもできます。BM25のかわりにハイブリッド検索を使用すると、リトリーバの品質を大幅に改善できます。これは、ハイブリッド検索リトリーバがデプロイされたモデルを使用して、ユーザー問合せを索引付けされたドキュメントと同じハイパースペースに埋め込み、純粋なセマンティック検索を実行して最も関連性の高いドキュメントを取得し、LLMの知識を強化するためです。リトライバーがLLMに最も関連性の高いコンテキストを取得して提供するのに良い仕事をしない場合、LLMモデルの応答はそれほど正確ではありません。

デプロイ済の事前トレーニング済センテンス・トランスフォーマ・モデルを含む取込みパイプラインをすでに使用しているKNプラグインを使用した検索索引の作成conversation-demo-index-knn索引を使用して、次の例に示すように、RAG問合せではBM5検索のかわりにハイブリッド検索が使用されます。

GET /conversation-demo-index-knn/_search?search_pipeline=<pipeline_name>
{
  "query": {
    "bool" : {
      "should" : [
        {
          "script_score": {
            "query": {
              "neural": {
                "passage_embedding": {
                  "query_text": "when did us pass espionage act?",
                  "model_id": "<embedding_model_ID>",
                  "k": 3
                }
              }
            },
            "script": {
              "source": "_score * 1.5"
            }
          }
        }
      ]
    }
  },
    "ext": {
        "generative_qa_parameters": {
            "llm_model": "oci_genai/<your_llm_model_name>",
            "llm_question": "when did us pass espionage act? answer only in two sentences using provided context.",
            "context_size": 2,
            "interaction_size": 1,
            "timeout": 15
        }
    }
}

<your_llm_model_name>「ステップ3: コネクタの作成」から、ペイロードで使用したモデル名を指定します。モデル名の例を次に示します。

  • oci_genai/cohere.command-r-plus
  • oci_genai/cohere.command
  • oci_genai/cohere.command-r-16k
  • oci_genai/meta.llama-2-70b-chat

データ・サイエンス・コネクタを使用している場合は、"llm_model"値を"oci_datascience/<your_llm_model_name>"に変更する必要があります。

レスポンス:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0.80985546,
    "hits": [
      {
        "_index": "conversation-demo-index-knn",
        "_id": "6",
        "_score": 0.80985546,
        "_source": {
          "passage_embedding": [
            -0.015252565,
            0.023013491,
            -0.023333456,
            -0.088787265,
            0.03142115,
            0.053571254,
            0.067729644,
            -0.018526044,
            -0.02262757,
            0.054774728,
            0.095119946,
           .......
          ],
          "text": "In June 1917, the U.S. Congress passed the Espionage Act of 1917 which was later extended by the Sedition Act of 1918, enacted in May 1918. In February 1918, the Montana legislature had passed the Montana Sedition Act, which was a model for the federal version. In combination, these laws criminalized criticism of the U.S. government, military, or symbols through speech or other means. The Montana Act led to the arrest of over 200 individuals and the conviction of 78, mostly of German or Austrian descent. Over 40 spent time in prison. In May 2006, then-Governor Brian Schweitzer posthumously issued full pardons for all those convicted of violating the Montana Sedition Act."
        }
      },
      {
        "_index": "conversation-demo-index-knn",
        "_id": "7",
        "_score": 0.5822973,
        "_source": {
          "passage_embedding": [
            0.016897075,
            -0.027237555,
            -0.026178025,
            -0.041597113,
            -0.07700658,
            0.02490874,
            0.009785392,
            ........
          ],
          "text": "When the U.S. entered World War II on December 8, 1941, many Montanans already had enlisted in the military to escape the poor national economy of the previous decade. Another 40,000-plus Montanans entered the armed forces in the first year following the declaration of war, and over 57,000 joined up before the war ended. These numbers constituted about 10 percent of the state's total population, and Montana again contributed one of the highest numbers of soldiers per capita of any state. Many Native Americans were among those who served, including soldiers from the Crow Nation who became Code Talkers. At least 1500 Montanans died in the war. Montana also was the training ground for the First Special Service Force or Devil's Brigade a joint U.S-Canadian commando-style force that trained at Fort William Henry Harrison for experience in mountainous and winter conditions before deployment. Air bases were built in Great Falls, Lewistown, Cut Bank and Glasgow, some of which were used as staging areas to prepare planes to be sent to allied forces in the Soviet Union. During the war, about 30 Japanese balloon bombs were documented to have landed in Montana, though no casualties nor major forest fires were attributed to them"
        }
      },
      {
        "_index": "conversation-demo-index-knn",
        "_id": "4",
        "_score": 0.58108574,
        "_source": {
          "passage_embedding": [
            0.017924132,
            0.03570767,
            0.024848921,
            -0.023073182,
            -0.0023820316,
            0.009969,
            0.076653704,
            -0.10182037,
            .......
          ],
          "text": "The Desert Land Act of 1877 was passed to allow settlement of arid lands in the west and allotted 640 acres (2.6 km2) to settlers for a fee of $.25 per acre and a promise to irrigate the land. After three years, a fee of one dollar per acre would be paid and the land would be owned by the settler. This act brought mostly cattle and sheep ranchers into Montana, many of whom grazed their herds on the Montana prairie for three years, did little to irrigate the land and then abandoned it without paying the final fees. Some farmers came with the arrival of the Great Northern and Northern Pacific Railroads throughout the 1880s and 1890s, though in relatively small numbers"
        }
      }
    ]
  },
  "ext": {
    "retrieval_augmented_generation": {
      "answer": """ The United States passed the Espionage Act in 1917, with the Sedition Act being enacted in 1918. This was preceded by the Montana Sedition act in 1918, which served as a model for the federal version, and resulted in the arrest of over 200 people, many of German or Austrian descent.
 
Would you like to know more about the Espionage act or any other events that occurred during that period? """
    }
  }
}

会話検索の実行

会話型検索を実行するには、会話メモリーを作成し、返された会話IDをRAG APIコールに渡します。これらの例のモデル名について、「ステップ3: コネクタの作成」のペイロードで使用したモデル名を指定し、<llm_model_name>をモデル名に置き換えます。モデル名の例を次に示します。

  • oci_genai/cohere.command-r-plus
  • oci_genai/cohere.command
  • oci_genai/cohere.command-r-16k
  • oci_genai/meta.llama-2-70b-chat

次の例は、会話IDを取得するための会話メモリーの作成方法を示しています。

POST /_plugins/_ml/memory/conversation
{
  "name": "rag-conversation"
}

レスポンス:

{
  "conversation_id": "<conversation_ID>"
}

BM25リトリーバを使用した会話型検索

GET /conversation-demo-index/_search?search_pipeline=<pipeline_name>
{
    "query": {
        "match": {
            "text": {
                "query": "when did us pass espionage act?"
            }
        }
    },
    "ext": {
        "generative_qa_parameters": {
            "llm_model": "oci_genai/<your_llm_model_name>",
            "llm_question": "when did us pass espionage act? answer only in two sentences using provided context.",
            "conversation_id": "<conversation_ID>",
            "context_size": 2,
            "interaction_size": 1,
            "timeout": 15
        }
    }
}

ハイブリッド検索リトリーバによる会話検索

GET /conversation-demo-index-knn/_search?search_pipeline=<pipeline_name>
{
  "query": {
    "bool" : {
      "should" : [
        {
          "script_score": {
            "query": {
              "neural": {
                "passage_embedding": {
                  "query_text": "when did us pass espionage act?",
                  "model_id": "<embedding_model_ID>",
                  "k": 3
                }
              }
            },
            "script": {
              "source": "_score * 1.5"
            }
          }
        }
      ]
    }
  },
    "ext": {
        "generative_qa_parameters": {
            "llm_model": "oci_genai/<your_llm_model_name>",
            "llm_question": "when did us pass espionage act? answer only in two sentences using provided context.",
            "conversation_id": "<conversation_ID>",
            "context_size": 2,
            "interaction_size": 1,
            "timeout": 15
        }
    }
}
<your_llm_model_name>には、使用するモデルの名前を指定します。たとえば:
  • oci_genai/cohere.command-r-plus
  • oci_genai/cohere.command
  • oci_genai/cohere.command-r-16k
  • oci_genai/meta.llama-2-70b-chat

会話IDを指定すると、OpenSearchに、会話の履歴を追跡するためのメモリーを作成するように求められます。次の詳細が、会話型検索のためにLLMに渡されます。

  • 取得されたコンテキスト・ドキュメント。
  • ユーザーの入力問合せおよびプロンプトのファインチューニング。
  • 指定した会話IDに基づくユーザーの以前の会話履歴。

APIコールでinteraction_sizeパラメータを使用して、考慮する以前の会話コンテキストの数を制御できます。context_sizeを使用して、LLMに解析する取得済最上位ドキュメントの数をコンテキストとして制御し、ナレッジを強化することもできます。