Búsqueda conversacional con OCI Generative AI

Siga los pasos de este tutorial para configurar y utilizar un pipeline integral de generación aumentada de recuperación (RAG) en OCI Search con OpenSearch, mediante un conector de OCI Generative AI.

Puede aprovechar el conector para tener acceso a todas las funciones de IA generativa, como la generación con recuperación aumentada (RAG), el resumen de texto, la generación de texto, la búsqueda conversacional y la búsqueda semántica.

Los siguientes pasos necesarios para crear un conector de IA generativa:

En adición a estos pasos básicos, este tema continúa con los pasos necesarios para configurar y utilizar un pipeline de generación aumentada de recuperación (RAG) de extremo a extremo en OCI Search con OpenSearch, mediante un conector de OCI Generative AI. Cada paso incluye una plantilla genérica del código necesario. Puede utilizar la consola para generar automáticamente este código con los valores configurados para su entorno. Consulte Creación de un pipeline de RAG para la búsqueda con OpenSearch.

El conector utiliza el modelo de incrustación de Cohere alojado por la IA generativa. La IA generativa también admite el modelo Llma2 con el que también puedes experimentar.

Requisitos

  • Para utilizar OCI Generative AI, el arrendamiento debe estar suscrito a la región del Medio Oeste de EE. UU. (Chicago) o a la región del Centro de Alemania (Fráncfort). No es necesario que cree el cluster en ninguna de esas regiones. Asegúrese de que el arrendamiento está suscrito a una de las regiones.
  • Para utilizar un conector de OCI Generative AI con OCI Search con OpenSearch, necesita un cluster configurado para utilizar OpenSearch versión 2.11. Por defecto, los nuevos clusters están configurados para utilizar la versión 2.11. Para crear un cluster, consulte Creación de un cluster OpenSearch.

    Para los clusters existentes configurados para la versión 2.3, puede realizar una actualización en línea a la versión 2.11. Para obtener más información, consulte OpenSearch Cluster Software Upgrades.

    Para actualizar los clusters existentes configurados para la versión 1.2.3 a 2.11, debe utilizar el proceso de actualización que se describe en OpenSearch Cluster Software Upgrades.

  • Cree una política para otorgar acceso a los recursos de IA generativa. El siguiente ejemplo de política incluye los permisos necesarios:
    ALLOW ANY-USER to manage generative-ai-family in tenancy WHERE ALL {request.principal.type='opensearchcluster', request.resource.compartment.id='<cluster_compartment_id>'}

    Si no está familiarizado con las políticas, consulte Introducción a las políticas y Políticas comunes.

  • Utilice la operación de configuración de las API de cluster para configurar los valores de cluster recomendados que le permiten crear un conector. El siguiente ejemplo incluye la configuración recomendada:
    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
          }
        }
      }
      }

Registrar el grupo de modelos

Registre un grupo de modelos mediante la operación register en las API de grupo de modelos, como se muestra en el siguiente ejemplo:

POST /_plugins/_ml/model_groups/_register
{
   "name": "public_model_group-emb",
   "description": "This is a public model group"
}

Anote el valor model_group_id devuelto en la respuesta:

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

Creación del conector

Puede crear un conector para cualquiera de los modelos de LLM remotos soportados por el servicio GenAI. Hay varios modelos alojados en ON-DEMAND, pero estos modelos a veces quedan en desuso. Opcionalmente, puede configurar y utilizar un punto final de modelo DEDICATED GENAI si lo prefiere.

Nota

Si utiliza el modelo ON-DEMAND, manténgase al día con las notificaciones de desuso del modelo del servicio GenAI y actualice el conector cuando sea necesario para evitar posibles interrupciones del servicio. Consulte Modelos fundamentales previamente entrenados en IA generativa para seleccionar un modelo de lenguaje grande de la lista de modelos soportados.

Si utiliza el modelo DEDICATED, cambie el parámetro servingType en el siguiente ejemplo de carga útil de ON-DEMAND a DEDICATED.

Las siguientes secciones muestran la plantilla de carga útil para las clases de modelo Cohere y Llama.

Plantilla de conector GenAI para modelos Cohere.Command

Proporcione la información de los siguientes marcadores de posición en su carga útil:

  • <connector_name>: introduzca un nombre único para identificar el conector. Por ejemplo, "cohere command-r-plus Connector v01".
  • <connector_description>: introduzca una breve descripción para el conector de hasta 30 caracteres. Por ejemplo, "Mi conector al modelo GenAI cohere.command-r-plus".
  • <compartment_OCID>: introduzca el OCID del compartimento en el que reside el cluster OpenSearch.
  • <genai_cohere_model>: introduzca el nombre del modelo de Cohere que desea utilizar. Por ejemplo, "cohere.command-r-plus."
POST _plugins/_ml/connectors/_create
{
     "name": "<connector_name>",
     "description": "<connector_description>",
     "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\":\"<compartment_OCID>\",\"servingMode\":{\"modelId\":\"<genai_cohere_model>\",\"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 + '\"}]}}}'"
  
         }
     ]
 }

Plantilla de conector GenAI para modelos Meta LLAMA

Proporcione la información de los siguientes marcadores de posición en su carga útil:

  • <connector_name>: introduzca un nombre único para identificar el conector. Por ejemplo, "conector meta-llama v1".
  • <connector_description>: introduzca una breve descripción para el conector de hasta 30 caracteres. Por ejemplo, "Mi conector a GenAI meta.llama."
  • <compartment_OCID>: introduzca el OCID del compartimento en el que reside el cluster OpenSearch.
  • <genai_llama_model>: introduzca el nombre del modelo de LLAMA que desea utilizar. Por ejemplo, "conector meta-llama v1".
POST _plugins/_ml/connectors/_create
{
     "name": "<connector_name>",
     "description": "<connector_description>",
     "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\":\<compartment_OCID>\",\"servingMode\":{\"modelId\":\"<genai_llama_model>\",\"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 + '\"}]}}}'"
 
 
 
         }
     ]
 }

Registro del modelo

Registre el modelo remoto mediante el conector de IA generativa con el ID de conector y el ID de grupo de modelos de los pasos anteriores, como se muestra en el siguiente ejemplo:

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

Despliegue del modelo

Despliegue el modelo con el ID de modelo devuelto en la respuesta de los pasos anteriores, como se muestra en el siguiente ejemplo:

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

Crear un pipeline de RAG

Cree un pipeline de RAG mediante model_id del paso anterior, como se muestra en el siguiente ejemplo:

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"
      }
    }
  ]
}

Crear el índice de búsqueda

Después de crear el pipeline de RAG, puede realizar RAG más la búsqueda conversacional en cualquier índice. También puede aprovechar un pipeline de ingestión de datos con un modelo previamente entrenado para utilizar la búsqueda híbrida como parte de la recuperación.

Crear el índice de búsqueda sin el plugin k-NN

En esta sección se describen los pasos para crear un índice sin utilizar un pipeline de ingestión.

Utilice esta opción para crear datos sin embeber,

  1. Cree un índice de búsqueda, como se muestra en el siguiente ejemplo:
    PUT /conversation-demo-index
    {
      "settings": {
        "index": {
          "number_of_shards": 1,
          "number_of_replicas": 0
        }
      },
      "mappings": {
        "properties": {
          "title": {
            "type": "text"
          },
          "text": {
            "type": "text"
          }
        }
      }
    }
  2. Ingiera datos en el índice, como se muestra en el siguiente ejemplo:
    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"
    }

Dado que este procedimiento no utiliza un pipeline de ingestión con este índice, no se generan incrustaciones para los documentos de texto durante la ingestión. Esto significa que solo se utiliza la búsqueda BM25 para recuperar los documentos relevantes.

Después de crear un índice de búsqueda sin el plugin k-NN, al ejecutar el siguiente comando para comprobar un documento indexado, solo se devuelve el texto, de la siguiente manera:

Solicitud:

GET /conversation-demo-index/_doc/1

Respuesta:

{
  "_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."
  }
}

Crear el índice de búsqueda con el plugin k-NN

Para aprovechar la búsqueda híbrida en lugar de BM25 para enriquecer la parte del recuperador del pipeline de RAG, debe configurar un pipeline de ingesta y utilizar un modelo previamente entrenado importado para que se creen incrustaciones de documentos en el momento de la ingesta.

Utilice esta opción para crear el índice con un pipeline de ingestión a fin de generar automáticamente la incrustación de los datos durante la ingestión.

  1. Para este recorrido virtual, utilice una de las siguientes opciones:
    • Opción 1: registre y despliegue un modelo previamente entrenado alojado en OCI Search con OpenSearch mediante los pasos descritos en Uso de un modelo previamente entrenado OpenSearch. Esta opción es la más sencilla de usar, no necesita configurar ninguna política de IAM adicional y la carga útil no es tan compleja como la carga útil para la siguiente opción.

    • Opción 2: importe, registre y despliegue un modelo entrenado previamente OpenSearch mediante los pasos descritos en Modelos personalizados. Esto incluye cargar el archivo de modelo en un cubo de Object Storage y, a continuación, especificar la URL de Object Storage del archivo de modelo al registrar el modelo.

    • Opción 3: también puede registrar y desplegar un modelo de incrustación GenAI remoto, como cohere.embed-english-v3.0, en el cluster mediante nuestro conector GenAI. Primero debe crear un conector y, a continuación, registrar y desplegar el modelo con el ID de conector, como se describe en los siguientes pasos.

      Nota

      Si utiliza el modelo ON-DEMAND, manténgase al día con las notificaciones de desuso del modelo del servicio GenAI y actualice el conector cuando sea necesario para evitar posibles interrupciones del servicio. Consulte Modelos fundamentales previamente entrenados en IA generativa para conocer los modelos de incrustación soportados a fin de seleccionar un modelo de incrustación de la lista de modelos soportados.

      Si utiliza el modelo DEDICATED, cambie el parámetro servingType en el siguiente ejemplo de carga útil de ON-DEMAND a DEDICATED.

    Anote el ID de modelo devuelto al registrar y desplegar el modelo.

  2. Cree un pipeline de ingesta utilizando model id del paso anterior, como se muestra en el siguiente ejemplo:
    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. Cree un índice de búsqueda con un pipeline de ingestión, como se muestra en el siguiente ejemplo:
    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. Ingerir los datos mediante el pipeline de ingestión del paso anterior, como se muestra en el siguiente ejemplo

    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"
    }

Después de crear el índice de búsqueda con el plugin k-NN, al ejecutar el siguiente comando para comprobar un documento indexado, la respuesta incluye incrustaciones, de la siguiente manera:

Solicitud:

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

Respuesta:

{
  "_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,
      ..............

Crear ID de conversación

Ejecute este comando para crear el ID de conversación:

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

Respuesta:

{
  "conversation_id": "<conversation_ID>"
}

Realizar RAG con BM25

Después de indexar los documentos, puede realizar RAG con el nombre del pipeline de RAG especificado en Crear un pipeline de RAG, como se muestra en el siguiente ejemplo.

Si utiliza un conector de Data Science, debe cambiar el valor "llm_model" a "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
        }
    }
}

La primera parte es la recuperación con la consulta al índice para encontrar los documentos más relevantes basados en la consulta del usuario. La segunda parte aumenta la consulta de usuario con más instrucciones y, a continuación, utiliza la respuesta de la parte de recuperación como contexto. A continuación, todos se transfieren al modelo de lenguaje grande para generar la respuesta utilizando solo el conocimiento en los datos del caso de uso.

Especifique el nombre de modelo que ha utilizado en la carga útil desde Crear el conector para <your_llm_model_name>. A continuación, se muestran ejemplos de nombres de modelos:

  • oci_genai/cohere.command-r-plus-08-2024
  • oci_genai/cohere.command-08-2024
  • oci_genai/meta.llama-3.1-70b-instruct
  • oci_genai/meta.llama-3.2-90b-vision-instruct

En la siguiente respuesta, la primera parte es un juego de documentos devueltos por el recuperador (el motor de búsqueda en OpenSearch). Esto es seguido por la respuesta del modelo de lenguaje grande (LLM), que es la respuesta real del modelo LLM que utiliza la generación de texto y el aprendizaje en contexto para aumentar la base de conocimientos del LLM. El motor de búsqueda que se utiliza aquí es BM25 porque el índice conversation-demo-index no utiliza un modelo desplegado para generar incrustaciones de documentos durante la ingesta.

{
  "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. "
    }
  }
}