4.7.4 ML_RAG

The ML_RAG routine performs retrieval-augmented generation (RAG) by:

  1. Taking a natural-language query.

  2. Retrieving context from relevant documents using semantic search.

  3. Generating a response that integrates information from the retrieved documents.

This routine provides detailed, accurate, and contextually relevant answers by augmenting a generative model with information retrieved from a comprehensive knowledge base.

ML_RAG Syntax

mysql> call sys.ML_RAG('QueryInNaturalLanguage', 'Output', [options]);

options: {
  JSON_OBJECT('key','value'[,'key','value'] ...)
    'key','value': {
    ['vector_store', JSON_ARRAY('VectorStoreTableName1'[,'VectorStoreTableName2'] ...)]
    ['schema', JSON_ARRAY('Schema1'[,'Schema2'] ...)]
    ['n_citations', NumberOfCitations]
    ['distance_metric', {'COSINE'|'DOT'|'EUCLIDEAN'}]
    ['document_name', JSON_ARRAY('DocumentName1'[,'DocumentName2'] ...)]
    ['skip_generate', {true|false}]
    ['model_options', JSON_OBJECT('Key1','Value1'[,'Key2','Value2'] ...)]
    ['exclude_vector_store', JSON_ARRAY('ExcludeVectorStoreTableName1'[,'ExcludeVectorStoreTableName2'] ...)]
    ['exclude_document_name', JSON_ARRAY('ExcludeDocumentName1'[,'ExcludeDocumentName2'] ...)]
    }
}

Following are ML_RAG parameters:

  • QueryInNaturalLangugae: specifies the natural-language query.

  • Output: stores the generated output. The output contains the following segments:

    • text: the generated text-based response.

    • citations: contains the following details:

      • segment: the textual content that is retrieved from the vector store through semantic search, and used as context generating the response.

      • distance: the distance between the query embedding the segment embedding.

      • document_name: the name of the document from which the segment is retrieved.

    • vector_store: the list of vector store tables used for context retrieval.

  • options: specifies optional parameters as key-value pairs in JSON format. It can include the following parameters:

    • vector_store: specifies a list of loaded vector store tables to use for context retrieval. The routine ignores invalid table names. By default, the routine performs a global search across all the available vector store tables in the DB system.

    • schema: specifies a list of schemas to check for loaded vector store tables. By default, the routine performs a global search across all the available vector store tables in all the schemas that are available in the DB system.

    • n_citations: specifies the number of segments to consider for context retrieval. Default value is 3. Possible values are integer values between 0 and 100.

    • distance_metric: specifies the distance metrics to use for context retrieval. Default value is COSINE. Possible values are COSINE, DOT, and EUCLIDEAN.

    • document_name: limits the documents to use for context retrieval. Only the specified documents are used. By default, the routine performs a global search across all the available documents stored in all the available vector stores in the DB system.

    • skip_generate: specifies whether to skip generation of the text-based response, and only perform context retrieval from the available or specified vector stores, schemas, or documents. Default value is false.

    • model_options: additional options that you can set for generating the text-based response. These are the same options that are available in the ML_GENERATE routine, which alter the text-based response per the specified settings. However, the context option is not supported as an ML_RAG model option. Default value is '{"model_id": "mistral-7b-instruct-v1"}'.

    • exclude_vector_store: specifies a list of loaded vector store tables to exclude from context retrieval. The routine ignores invalid table names. Default value is NULL. This option is available in HeatWave 9.0.1-u1 and later versions.

    • exclude_document_name: specifies a list of documents to exclude from context retrieval. Default value is NULL. This option is available in HeatWave 9.0.1-u1 and later versions.

Syntax Examples

  • Retrieving context and generating output:

    call sys.ML_RAG("What is AutoML",@output,@options);

    Where, @options is set to specify the vector store table to use using vector_store key, as shown below:

    set @options = JSON_OBJECT("vector_store", JSON_ARRAY("demo_db.demo_embeddings"));

    Print the output:

    select JSON_PRETTY(@output);

    The output of the routine looks similar to the following:

    {
    "citations": [
    {
    "segment": "Oracle AutoML also produces high quality models very efficiently,
    which is achieved through a scalable design and intelligent choices that
    reduce trials at each stage in the pipeline.\n Scalable design: The Oracle
    AutoML pipeline is able to exploit both HeatWave internode and intranode
    parallelism, which improves scalability and reduces runtime.",
    "distance": 0.4262576103210449,
    "document_name": "https://objectstorage.Region.oraclecloud.com/n/Namespace/b/BucketName/o/Path/Filename"
    },
    {
    "segment": "The HeatWave AutoML ML_TRAIN routine leverages Oracle AutoML
    technology to automate the process of training a machine learning model.
    Oracle AutoML replaces the laborious and time consuming tasks of the data
    analyst whose workflow is as follows:\n1. Selecting a model from a large
    number of viable candidate models.\n2.\n99",
    "distance": 0.4311879277229309,
    "document_name": " https://objectstorage. Region.oraclecloud.com/n/Namespace/b/BucketName/o/Path/Filename"
    },
    {
    "segment": "3.1 HeatWave AutoML Features HeatWave AutoML makes it easy to
    use machine learning, whether you are a novice user or an experienced ML
    practitioner. You provide the data, and HeatWave AutoML analyzes the
    characteristics of the data and creates an optimized machine learning model
    that you can use to generate predictions and explanations.",
    "distance": 0.4441382884979248,
    "document_name": "https://objectstorage. Region.oraclecloud.com/n/Namespace/b/BucketName/o/Path/Filename"
    }
    ],
    "vector_store": [
    "demo_db.demo_embeddings"
    ]
    }