RAG tool

Introduced 2.13

The RAGTool performs retrieval-augmented generation (RAG). For more information about RAG, see Conversational search.

RAG calls a large language model (LLM) and supplements its knowledge by providing relevant OpenSearch documents along with the user question. To retrieve relevant documents from an OpenSearch index, you’ll need a text embedding model that facilitates vector search.

The RAG tool supports the following search methods:

Before you start

To register and deploy a text embedding model and an LLM and ingest data into an index, perform Steps 1–5 of the Agents and tools tutorial.

The following example uses neural search. To configure neural sparse search and deploy a sparse encoding model, see Neural sparse search.

Step 1: Register a flow agent that will run the RAGTool

A flow agent runs a sequence of tools in order and returns the last tool’s output. To create a flow agent, send the following request, providing the text embedding model ID in the embedding_model_id parameter and the LLM model ID in the inference_model_id parameter:

  1. POST /_plugins/_ml/agents/_register
  2. {
  3. "name": "Test_Agent_For_RagTool",
  4. "type": "flow",
  5. "description": "this is a test flow agent",
  6. "tools": [
  7. {
  8. "type": "RAGTool",
  9. "description": "A description of the tool",
  10. "parameters": {
  11. "embedding_model_id": "Hv_PY40Bk4MTqircAVmm",
  12. "inference_model_id": "SNzSY40B_1JGmyB0WbfI",
  13. "index": "my_test_data",
  14. "embedding_field": "embedding",
  15. "query_type": "neural",
  16. "source_field": [
  17. "text"
  18. ],
  19. "input": "${parameters.question}",
  20. "prompt": "\n\nHuman:You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say don't know. \n\n Context:\n${parameters.output_field}\n\nHuman:${parameters.question}\n\nAssistant:"
  21. }
  22. }
  23. ]
  24. }

copy

For parameter descriptions, see Register parameters.

OpenSearch responds with an agent ID:

  1. {
  2. "agent_id": "9X7xWI0Bpc3sThaJdY9i"
  3. }

To create a conversational agent containing a RAGTool, see Conversational agents.

Step 2: Run the agent

Before you run the agent, make sure that you add the sample OpenSearch Dashboards Sample web logs dataset. To learn more, see Adding sample data.

Then, run the agent by sending the following request:

  1. POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
  2. {
  3. "parameters": {
  4. "question": "what's the population increase of Seattle from 2021 to 2023"
  5. }
  6. }

copy

OpenSearch performs vector search and returns the relevant documents:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "result": """{"_index":"my_test_data","_source":{"text":"Chart and table of population level and growth rate for the Seattle metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\n
  8. The current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\n
  9. The metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\n
  10. The metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\n
  11. The metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."},"_id":"6","_score":0.8173238}
  12. {"_index":"my_test_data","_source":{"text":"Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\n
  13. The current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\n
  14. The metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\n
  15. The metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\n
  16. The metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."},"_id":"2","_score":0.6641471}
  17. """
  18. }
  19. ]
  20. }
  21. ]
  22. }

Register parameters

The following table lists all tool parameters that are available when registering an agent.

ParameterTypeRequired/OptionalDescription
embedding_model_idStringRequiredThe model ID of the model to use for generating vector embeddings.
inference_model_idStringRequiredThe model ID of the LLM to use for inference.
indexStringRequiredThe index from which to retrieve relevant documents to pass to the LLM.
embedding_fieldStringRequiredWhen the model encodes raw text documents, the encoding result is saved in a field. Specify this field as the embedding_field. Neural search matches documents to the query by calculating the similarity score between the query text and the text in the document’s embedding_field.
source_fieldStringRequiredThe document field or fields to return. You can provide a list of multiple fields as an array of strings, for example, [“field1”, “field2”].
inputStringRequired for flow agentRuntime input sourced from flow agent parameters. If using an LLM, this field is populated with the LLM response.
output_fieldStringOptionalThe name of the output field. Default is response.
query_typeStringOptionalSpecifies the type of query to run to perform neural search. Valid values are neural (for dense retrieval) and neural_sparse (for sparse retrieval). Default is neural.
doc_sizeIntegerOptionalThe number of documents to fetch. Default is 2.
promptStringOptionalThe prompt to provide to the LLM.
kIntegerOptionalThe number of nearest neighbors to search for when performing neural search. Default is 10.
enable_Content_GenerationBooleanOptionalIf true, returns results generated by an LLM. If false, returns results directly without LLM-assisted content generation. Default is true.
nested_pathStringOptionalThe path to the nested object for the nested query. Only used for nested fields. Default is null.

Execute parameters

The following table lists all tool parameters that are available when running the agent.

ParameterTypeRequired/OptionalDescription
questionStringRequiredThe natural language question to send to the LLM.