Ricerca conversazionale con OCI Generative AI
Seguire i passi di questa procedura guidata per impostare e utilizzare una pipeline di Retrieval-Augmented Generation (RAG) end-to-end in OCI Search con OpenSearch, utilizzando un connettore OCI Generative AI.
Puoi utilizzare il connettore per accedere a tutte le funzioni dell'intelligenza artificiale generativa, ad esempio la Retrieval-Augmented Generation (RAG), il riepilogo del testo, la generazione di testo, la ricerca conversazionale e la ricerca semantica.
Per creare un connettore AI generativa, effettuare le operazioni riportate di seguito.
In aggiunta a questi passi di base, questo argomento continua con i passi necessari per impostare e utilizzare una pipeline di Retrieval-Augmented Generation (RAG) end-to-end in OCI Search con OpenSearch, utilizzando un connettore OCI Generative AI. Ogni passo include un modello generico del codice richiesto. È possibile utilizzare la console per generare automaticamente questo codice con valori configurati per l'ambiente in uso. Vedere Creazione di una pipeline RAG per la ricerca con OpenSearch.
Il connettore utilizza il modello di incorporamento Cohere ospitato dall'AI generativa. L'intelligenza artificiale generativa supporta anche il modello Llma2 con cui è possibile sperimentare.
Requisiti indispensabili
- Per utilizzare OCI Generative AI, la tenancy deve essere sottoscritta all'area Midwest degli Stati Uniti (Chicago) o all'area centrale della Germania (Francoforte). Non è necessario creare il cluster in nessuna di queste aree, assicurarsi che la tenancy sia sottoscritta a una delle aree.
-
Per utilizzare un connettore AI generativa OCI con OCI Search con OpenSearch, è necessario un cluster configurato per utilizzare OpenSearch versione 2.11. Per impostazione predefinita, i nuovi cluster sono configurati per l'uso della versione 2.11. Per creare un cluster, vedere Creazione di un cluster OpenSearch.
Per i cluster esistenti configurati per la versione 2.3, è possibile eseguire un aggiornamento in linea alla versione 2.11. Per ulteriori informazioni, vedere OpenSearch Aggiornamenti software del cluster.
Per aggiornare i cluster esistenti configurati per la versione 1.2.3 alla 2.11, è necessario utilizzare il processo di aggiornamento descritto in OpenSearch Aggiornamenti software del cluster.
- Creare un criterio per concedere l'accesso alle risorse di intelligenza artificiale generativa. L'esempio di criteri riportato di seguito include le autorizzazioni necessarie.
ALLOW ANY-USER to manage generative-ai-family in tenancy WHERE ALL {request.principal.type='opensearchcluster', request.resource.compartment.id='<cluster_compartment_id>'}
Se non si conoscono i criteri, vedere Guida introduttiva ai criteri e Criteri comuni.
- Utilizzare l'operazione settings delle API cluster per configurare le impostazioni cluster consigliate che consentono di creare un connettore. L'esempio seguente include le impostazioni consigliate:
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 } } } }
Registra il gruppo di modelli
Registrare un gruppo di modelli utilizzando l'operazione di registrazione nelle API del gruppo di modelli, come mostrato nell'esempio riportato di seguito.
POST /_plugins/_ml/model_groups/_register
{
"name": "public_model_group-emb",
"description": "This is a public model group"
}
Prendere nota del model_group_id
restituito nella risposta:
{
"model_group_id": "<model_group_ID>",
"status": "CREATED"
}
Creare il connettore
È possibile creare un connettore per qualsiasi modello LLM remoto supportato dal servizio GenAI. Ci sono diversi modelli ospitati ON-DEMAND, ma questi modelli a volte diventano deprecati. Se lo si desidera, è possibile configurare e utilizzare un endpoint del modello GENAI DEDICATO.
Se si utilizza il modello ON-DEMAND, tenere aggiornate le notifiche di deprecazione del modello dal servizio GenAI e aggiornare il connettore quando necessario per evitare potenziali interruzioni del servizio. Fare riferimento alla sezione Modelli di base pre-addestrati nell'intelligenza artificiale generativa per selezionare un modello di linguaggio di grandi dimensioni dall'elenco dei modelli supportati.
Se si utilizza il modello DEDICATED, modificare il parametro servingType
nell'esempio di payload seguente da ON-DEMAND a DEDICATED.
Le sezioni seguenti mostrano il modello di payload per le classi modello Cohere e Llama.
Modello del connettore GenAI per i modelli Cohere.Command
Fornire le informazioni per i segnaposto seguenti nel payload:
<connector_name>
: immettere un nome univoco per identificare il connettore. Ad esempio, "connettore command-r-plus cohere v01".<connector_description>
: immettere una breve descrizione del connettore con una lunghezza massima di 30 caratteri. Ad esempio, "Il mio connettore per il modello GenAI cohere.command-r-plus".<compartment_OCID>
: immettere l'OCID del compartimento in cui risiede il cluster OpenSearch.<genai_cohere_model>
: immettere il nome del modello Cohere che si desidera utilizzare. Ad esempio, "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 + '\"}]}}}'"
}
]
}
GenAI Modello di connettore per i modelli Meta LLAMA
Fornire le informazioni per i segnaposto seguenti nel payload:
<connector_name>
: immettere un nome univoco per identificare il connettore. Ad esempio, "connettore meta-llama v1".<connector_description>
: immettere una breve descrizione del connettore con una lunghezza massima di 30 caratteri. Ad esempio, "Il mio connettore per GenAI meta.llama"."<compartment_OCID>
: immettere l'OCID del compartimento in cui risiede il cluster OpenSearch.<genai_llama_model>
: immettere il nome del modello LLAMA che si desidera utilizzare. Ad esempio, "connettore 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 + '\"}]}}}'"
}
]
}
Modelli del connettore GenAI per i modelli OpenAI
Di seguito sono riportati alcuni esempi di modelli di connettore GenAI per i modelli OpenAI.
4o/4o-mini
POST _plugins/_ml/connectors/_create
{
"name": "rag-connector",
"description": "OpenAI connector for RAG pipeline",
"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\":\"<MODEL NAME>\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"maxCompletionTokens\":600,\"temperature\":0.5,\"frequencyPenalty\":0,\"presencePenalty\":0,\"topP\":1.0,\"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 + '\"}]}}}'"
}
]
}
o3-mini/o1
POST _plugins/_ml/connectors/_create
{
"name": "o3-mini-rag-connector",
"description": "OpenAI o3-mini connector for RAG pipeline",
"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\":\"<MODEL_NAME>\",\"servingType\":\"ON_DEMAND\"},\"chatRequest\":{\"isStream\":false,\"apiFormat\":\"GENERIC\",\"reasoningEffort\":\"LOW\",\"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 + '\"}]}}}'"
}
]
}
Registrazione del modello
Registrare il modello remoto utilizzando il connettore AI generativa con l'ID connettore e l'ID gruppo di modelli dei passi precedenti, come mostrato nell'esempio riportato di seguito.
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>"
}
Distribuisci modello
Distribuire il modello utilizzando l'ID modello restituito nella risposta dai passi precedenti, come mostrato nell'esempio riportato di seguito.
POST /_plugins/_ml/models/<embedding_model_ID>/_deploy
Crea una pipeline RAG
Creare una pipeline RAG utilizzando model_id
dal passo precedente, come mostrato nell'esempio riportato di seguito.
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"
}
}
]
}
Crea l'indice di ricerca
Dopo aver creato la pipeline RAG, è possibile eseguire la ricerca RAG più la ricerca conversazionale su qualsiasi indice. È inoltre possibile utilizzare una pipeline di inclusione dati con un modello pre-addestrato per utilizzare la ricerca ibrida come parte del recupero.
Creare l'indice di ricerca senza il plugin k-NN
Questa sezione descrive i passi per creare un indice senza utilizzare una pipeline di inclusione.
Utilizzare questa opzione per creare dati senza incorporarli,
- Creare un indice di ricerca, come mostrato nell'esempio seguente:
PUT /conversation-demo-index { "settings": { "index": { "number_of_shards": 1, "number_of_replicas": 0 } }, "mappings": { "properties": { "title": { "type": "text" }, "text": { "type": "text" } } } }
- Includere i dati nell'indice, come mostrato nell'esempio seguente:
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" }
Poiché questa procedura non utilizza una pipeline di inclusione con questo indice, le integrazioni non vengono generate per i documenti di testo durante l'inclusione. Ciò significa che solo la ricerca BM25 viene utilizzata per recuperare i documenti pertinenti.
Dopo aver creato un indice di ricerca senza il plugin k-NN, quando si esegue il seguente comando per controllare un documento indicizzato, viene restituito solo il testo, come segue:
Richiesta:
GET /conversation-demo-index/_doc/1
Risposta:
{
"_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."
}
}
Creare l'indice di ricerca con il plugin k-NN
Per utilizzare la ricerca ibrida anziché BM25 per arricchire la parte di recupero della pipeline RAG, è necessario impostare una pipeline di inclusione e utilizzare un modello pre-addestrato importato in modo che le integrazioni dei documenti vengano create al momento dell'inclusione.
Utilizzare questa opzione per creare l'indice con una pipeline di inclusione per generare automaticamente l'incorporamento dei dati durante l'inclusione.
- Ai fini di questa procedura guidata, utilizzare una delle seguenti opzioni:
-
Opzione 1: registrare e distribuire un modello pre-addestrato ospitato in OCI Search con OpenSearch utilizzando i passi descritti in Uso di un modello pre-addestrato OpenSearch. Questa opzione è la più semplice da usare, non è necessario configurare criteri IAM aggiuntivi e il payload non è complesso come il payload per l'opzione successiva.
-
Opzione 2: importare, registrare e distribuire un modello pre-addestrato OpenSearch utilizzando i passi descritti in Modelli personalizzati. Ciò include il caricamento del file modello in un bucket di storage degli oggetti, quindi la specifica dell'URL di storage degli oggetti del file modello quando si registra il modello.
-
Opzione 3: è inoltre possibile registrare e distribuire un modello di incorporamento GenAI remoto, ad esempio cohere.embed-english-v3.0, nel cluster utilizzando il connettore GenAI. È necessario prima creare un connettore, quindi registrare e distribuire il modello utilizzando l'ID connettore come descritto nei passi riportati di seguito.
Nota
Se si utilizza il modello ON-DEMAND, tenere aggiornate le notifiche di deprecazione del modello dal servizio GenAI e aggiornare il connettore quando necessario per evitare potenziali interruzioni del servizio. Fare riferimento alla sezione Modelli di base pre-addestrati nell'AI generativa per i modelli di incorporamento supportati per selezionare un modello di incorporamento dall'elenco dei modelli supportati.
Se si utilizza il modello DEDICATED, modificare il parametro
servingType
nell'esempio di payload seguente da ON-DEMAND a DEDICATED.
Prendere nota dell'ID modello restituito quando si registra e si distribuisce il modello.
-
- Creare una pipeline di inclusione utilizzando il comando
model id
del passo precedente, come mostrato nell'esempio seguente: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" } } } ] }
- Creare un indice di ricerca con una pipeline di inclusione, come mostrato nell'esempio riportato di seguito.
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" } } } }
-
Includere i dati utilizzando la pipeline di inclusione dal passo precedente, come mostrato nell'esempio seguente
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" }
Dopo aver creato l'indice di ricerca con il plugin k-NN, quando si esegue il seguente comando per controllare un documento indicizzato, la risposta include le integrazioni, come indicato di seguito.
Richiesta:
GET /conversation-demo-index-knn/_doc/1
Risposta:
{
"_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,
..............
Crea ID conversazione
Eseguire questo comando per creare l'ID conversazione:
POST /_plugins/_ml/memory/conversation
{
"name": "rag-conversation"
}
Risposta:
{
"conversation_id": "<conversation_ID>"
}
Esegui RAG con BM25
Dopo l'indicizzazione dei documenti, è possibile eseguire RAG utilizzando il nome della pipeline RAG specificata in Crea una pipeline RAG, come mostrato nell'esempio riportato di seguito.
Se si utilizza un connettore Data Science, è necessario modificare il valore "llm_model"
in "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 prima parte è il recupero con la query all'indice per trovare i documenti più rilevanti in base alla query dell'utente. La seconda parte aumenta la query dell'utente con ulteriori istruzioni, quindi utilizza la risposta dalla parte di recupero come contesto. Questi vengono quindi tutti passati al modello linguistico di grandi dimensioni per generare la risposta utilizzando solo la conoscenza nei dati dei casi d'uso.
Specificare il nome del modello utilizzato nel payload da Crea connettore per <your_llm_model_name>
. Di seguito vengono riportati alcuni esempi di nomi di modello.
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
Nella risposta seguente, la prima parte è un insieme di documenti restituiti dal retriever (il motore di ricerca in OpenSearch). Questo è seguito dalla risposta del modello linguistico di grandi dimensioni (LLM, large language model) che è la risposta effettiva del modello LLM utilizzando la generazione di testo e l'apprendimento contestuale per aumentare la knowledge base del LLM. Il motore di ricerca utilizzato qui è BM25 perché l'indice conversation-demo-index
non utilizza un modello distribuito per generare incorporamenti di documenti durante l'inclusione.
{
"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. "
}
}
}
Esegui RAG con ricerca ibrida
È inoltre possibile eseguire RAG con la ricerca ibrida come recupero. L'utilizzo della ricerca ibrida al posto di BM25 può migliorare notevolmente la qualità del retriever. Questo perché il Retriever di ricerca ibrida utilizza un modello distribuito per incorporare la query utente nello stesso iperspazio dei documenti indicizzati e quindi esegue una ricerca semantica pura per recuperare i documenti più pertinenti per aumentare la conoscenza del LLM. Se il retriever non fa un buon lavoro nel recuperare e fornire il contesto più pertinente al LLM, la risposta del modello LLM non è così accurata.
Utilizzando l'indice conversation-demo-index-knn
di Crea indice di ricerca, che utilizza già una pipeline di inclusione con un modello di trasformatore di frasi pre-addestrato distribuito, la query RAG utilizza la ricerca ibrida anziché la ricerca BM5, come mostrato nell'esempio riportato di seguito.
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
}
}
}
Specificare il nome del modello utilizzato nel payload da Crea connettore per <your_llm_model_name>
. Di seguito vengono riportati alcuni esempi di nomi di modello.
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
Se si utilizza un connettore Data Science, è necessario modificare il valore "llm_model"
in "oci_datascience/<your_llm_model_name>"
.
Risposta:
{
"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? """
}
}
}
Esegui ricerca conversazionale
Per eseguire la ricerca conversazionale, creare una memoria di conversazione e passare l'ID conversazione restituito alla chiamata API RAG. Specificare il nome del modello utilizzato nel payload da Crea connettore per il nome del modello in questi esempi, sostituire <llm_model_name>
con il nome del modello. Di seguito vengono riportati alcuni esempi di nomi di modello.
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
L'esempio seguente mostra come creare la memoria della conversazione per ottenere l'ID conversazione:
POST /_plugins/_ml/memory/conversation
{
"name": "rag-conversation"
}
Risposta:
{
"conversation_id": "<conversation_ID>"
}
Ricerca conversazionale con BM25 Retriever
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
}
}
}
Ricerca conversazionale con recupero ricerca ibrida
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>
, specificare il nome del modello in uso, ad esempio: 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
Se si specifica l'ID conversazione, viene richiesto a OpenSearch di creare una memoria per tenere traccia della cronologia della conversazione. I seguenti dettagli vengono passati al LLM per la ricerca conversazionale:
- Documenti di contesto recuperati.
- Query di input e ottimizzazione prompt dell'utente.
- Cronologia delle conversazioni precedenti dell'utente in base all'ID conversazione specificato.
È possibile controllare il numero di contesti di conversazione precedenti da considerare utilizzando il parametro interaction_size
nella chiamata API. È inoltre possibile utilizzare context_size
per controllare quanti dei primi documenti recuperati si desidera analizzare all'LLM come contesto per aumentare la conoscenza.