This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

This is an experimental feature and is not recommended for use in a production environment. For updates on the progress of the feature or if you want to leave feedback, see the associated GitHub issue.

Conversational search

Conversational search is an experimental machine learning (ML) feature that enables a new search interface. Whereas traditional document search allows you to ask a question and receive a list of documents that might contain the answer to that question, conversational search uses large language models (LLMs) to read the top N documents and synthesizes those documents into a plaintext “answer” to your question.

Currently, conversational search uses two systems to synthesize documents:

Conversation memory

Conversation memory consists of a simple CRUD-life API comprising two resources: Conversations and Interactions. Conversations are made up of interactions. An interaction represents a pair of messages: a human input and an artificial intelligence (AI) response. You cannot create any interactions until you’ve created a conversation.

To make it easier to build and debug applications that use conversation memory, conversation-meta and conversation-interactions are stored in two system indexes.

conversation-meta index

In the conversation-meta index, you can customize the name field to make it easier for end users to know how to continue a conversation with the AI, as shown in the following schema:

  1. .plugins-ml-conversation-meta
  2. {
  3. "_meta": {
  4. "schema_version": 1
  5. },
  6. "properties": {
  7. "name": {"type": "keyword"},
  8. "create_time": {"type": "date", "format": "strict_date_time||epoch_millis"},
  9. "user": {"type": "keyword"}
  10. }
  11. }

conversation-interactions index

In the conversation-interactions index, all of the following fields are set by the user or AI application. Each field is entered as a string.

FieldDescription
inputThe question that forms the basis for an interaction.
prompt_templateThe prompt template that was used as the framework for this interaction.
responseThe AI response to the prompt.
originThe name of the AI or other system that generated the response.
additional_infoAny other information that was sent to the “origin” in the prompt.

The conversation-interactions index creates a clean interaction abstraction and make it easy for the index to reconstruct the exact prompts sent to the LLM, enabling robust debugging and explainability, as shown in the following schema:

  1. .plugins-ml-conversation-interactions
  2. {
  3. "_meta": {
  4. "schema_version": 1
  5. },
  6. "properties": {
  7. "conversation_id": {"type": "keyword"},
  8. "create_time": {"type": "date", "format": "strict_date_time||epoch_millis"},
  9. "input": {"type": "text"},
  10. "prompt_template": {"type": "text"},
  11. "response": {"type": "text"},
  12. "origin": {"type": "keyword"},
  13. "additional_info": {"type": "text"}
  14. }
  15. }

Working with conversations and interactions

When the Security plugin is enabled, all conversations in ML Commons exist in a “private” security mode. Only the user who created a conversation can interact with that conversation. No users on the cluster can see another user’s conversation.

To begin using conversation memory, enable the following cluster setting:

  1. PUT /_cluster/settings
  2. {
  3. "persistent": {
  4. "plugins.ml_commons.memory_feature_enabled": true
  5. }
  6. }

After conversation memory is enabled, you can use the Memory API to create a conversation.

To make the conversation easily identifiable, use the optional name field in the Memory API, as shown in the following example. This will be your only opportunity to name your conversation.

  1. POST /_plugins/_ml/memory/conversation
  2. {
  3. "name": Example conversation
  4. }

The Memory API responds with the conversation ID, as shown in the following example response:

  1. { "conversation_id": "4of2c9nhoIuhcr" }

You’ll use the conversation_id to create interactions inside the conversation. To create interactions, enter the conversation_id into the Memory API path. Then customize the fields in the request body, as shown in the following example:

  1. POST /_plugins/_ml/memory/conversation/4of2c9nhoIuhcr
  2. {
  3. "input": "How do I make an interaction?",
  4. "prompt_template": "Hello OpenAI, can you answer this question? \
  5. Here's some extra info that may help. \
  6. [INFO] \n [QUESTION]",
  7. "response": "Hello, this is OpenAI. Here is the answer to your question.",
  8. "origin": "MyFirstOpenAIWrapper",
  9. "additional_info": "Additional text related to the answer \
  10. A JSON or other semi-structured response"
  11. }

The Memory API then responds with an interaction ID, as shown in the following example response:

  1. { "interaction_id": "948vh_PoiY2hrnpo" }

Getting conversations

You can get a list of conversations using the following Memory API operation:

  1. GET /_plugins/_ml/memory/conversation?max_results=3&next_token=0

Use the following path parameters to customize your results.

ParameterData typeDescription
max_resultsIntegerThe maximum number of results returned by the response. Default is 10.
next_tokenIntegerRepresents the conversation order position that will be retrieved. For example, if conversations A, B, and C exist, next_token=1 would return conversations B and C. Default is 0.

The Memory API responds with the most recent conversation, as indicated in the create_time field of the following example response:

  1. {
  2. "conversations": [
  3. {
  4. "conversation_id": "0y4hto_in1",
  5. "name": "",
  6. "create_time": "2023-4-23 10:25.324662"
  7. }, ... (2 more since we specified max_results=3)
  8. ],
  9. "next_token": 3
  10. }

If there are fewer conversations than the number set in max_results, the response only returns the number of conversations that exist. Lastly, next_token provides an ordered position of the sorted list of conversations. When a conversation is added between subsequent GET conversation calls, one of the listed conversations will be duplicated in the results, for example:

  1. GetConversations -> [BCD]EFGH
  2. CreateConversation -> ABCDEFGH
  3. GetConversations(next_token=3) -> ABC[DEF]GH

Getting interactions

To see a list of interactions in a conversation, enter the conversation_id at the end of the API request, as shown in the following example. You can use max_results and next_token to sort the response:

  1. GET /_plugins/_ml/memory/conversation/4of2c9nhoIuhcr

The Memory API returns the following interaction information:

  1. {
  2. "interactions": [
  3. {
  4. "interaction_id": "342968y2u4-0",
  5. "conversation_id": "0y4hto_in1",
  6. "create_time": "2023-4-23 10:25.324662",
  7. "input": "How do I make an interaction?",
  8. "prompt_template": "Hello OpenAI, can you answer this question? \
  9. Here's some extra info that may help. \
  10. [INFO] \n [QUESTION]",
  11. "response": "Hello, this is OpenAI. Here is the answer to your question.",
  12. "origin": "MyFirstOpenAIWrapper",
  13. "additional_info": "Additional text related to the answer \
  14. A JSON or other semi-structured response"
  15. }, ... (9 more since max_results defaults to 10)
  16. ],
  17. "next_token": 10
  18. }

Deleting conversations

To delete a conversation, use the DELETE operation, as showing in the following example:

  1. DELETE /_plugins/_ml/memory/conversation/4of2c9nhoIuhcr

The Memory API responds with the following:

  1. { "success": true }

RAG pipeline

RAG is a technique that retrieves documents from an index, passes them through a seq2seq model, such as an LLM, and then generates more factual outputs.

Enabling RAG

Use the following cluster setting to enable the RAG pipeline feature:

  1. PUT /_cluster/settings
  2. {
  3. "persistent": {"plugins.ml_commons.rag_pipeline_feature_enabled": "true"}
  4. }

Connecting the model

RAG requires an LLM in order to function. We recommend using a connector.

Use the following steps to set up an HTTP connector using the OpenAI GPT 3.5 model:

  1. Use the Connector API to create the HTTP connector:
  1. POST /_plugins/_ml/connectors/_create
  2. {
  3. "name": "OpenAI Chat Connector",
  4. "description": "The connector to public OpenAI model service for GPT 3.5",
  5. "version": 2,
  6. "protocol": "http",
  7. "parameters": {
  8. "endpoint": "[api.openai.com](http://api.openai.com/)",
  9. "model": "gpt-3.5-turbo",
  10. "temperature": 0
  11. },
  12. "credential": {
  13. "openAI_key": "<your OpenAI key>"
  14. },
  15. "actions": [
  16. {
  17. "action_type": "predict",
  18. "method": "POST",
  19. "url": "[https://$](https://%24/){parameters.endpoint}/v1/chat/completions",
  20. "headers": {
  21. "Authorization": "Bearer ${credential.openAI_key}"
  22. },
  23. "request_body": "{ \"model\": \"${parameters.model}\", \"messages\": ${parameters.messages}, \"temperature\": $ {parameters.temperature} }"
  24. }
  25. ]
  26. }
  1. Create a new model group for the connected model. You’ll use the model_group_id returned by the Register API to register the model:

    1. POST /_plugins/_ml/model_group/_register
    2. {
    3. "name": "public_model_group",
    4. "description": "This is a public model group"
    5. }
  2. Register and deploy the model using the connector_id from the Connector API response in Step 1 and the model_group_id returned in Step 2:

  1. POST /_plugins/_ml/models/_register
  2. {
  3. "name": "openAI-gpt-3.5-turbo",
  4. "function_name": "remote",
  5. "model_group_id": "fp-hSYoBu0R6vVqGMnM1",
  6. "description": "test model",
  7. "connector_id": "f5-iSYoBu0R6vVqGI3PA"
  8. }
  1. With the model registered, use the task_id returned in the registration response to get the model_id. You’ll use the model_id to deploy the model to OpenSearch:
  1. GET /_plugins/_ml/tasks/<task_id>
  1. Using the model_id from step 4, deploy the model:
  1. POST /_plugins/_ml/models/<model_id>/_deploy

Setting up the pipeline

Next, you’ll create a search pipeline for the connector model. Use the following Search API request to create a pipeline:

  1. PUT /_search/pipeline/<pipeline_name>
  2. {
  3. "response_processors": [
  4. {
  5. "retrieval_augmented_generation": {
  6. "tag": "openai_pipeline_demo",
  7. "description": "Demo pipeline Using OpenAI Connector",
  8. "model_id": "<model_id>",
  9. "context_field_list": ["text"]
  10. }
  11. }
  12. ]
  13. }

context_field_list is the list of fields in document sources that the pipeline uses as context for the RAG. For example, when context_field_list parses through the following document, the pipeline sends the text field from the response to OpenAI model:

  1. {
  2. "_index": "qa_demo",
  3. "_id": "SimKcIoBOVKVCYpk1IL-",
  4. "_source": {
  5. "title": "Abraham Lincoln 2",
  6. "text": "Abraham Lincoln was born on February 12, 1809, the second child of Thomas Lincoln and Nancy Hanks Lincoln, in a log cabin on Sinking Spring Farm near Hodgenville, Kentucky.[2] He was a descendant of Samuel Lincoln, an Englishman who migrated from Hingham, Norfolk, to its namesake, Hingham, Massachusetts, in 1638. The family then migrated west, passing through New Jersey, Pennsylvania, and Virginia.[3] Lincoln was also a descendant of the Harrison family of Virginia; his paternal grandfather and namesake, Captain Abraham Lincoln and wife Bathsheba (née Herring) moved the family from Virginia to Jefferson County, Kentucky.[b] The captain was killed in an Indian raid in 1786.[5] His children, including eight-year-old Thomas, Abraham's father, witnessed the attack.[6][c] Thomas then worked at odd jobs in Kentucky and Tennessee before the family settled in Hardin County, Kentucky, in the early 1800s.[6]\n"
  7. }
  8. }

You can customize context_field_list in your RAG pipeline to send any fields that exist in your documents to the LLM.

Using the pipeline

Using the pipeline is similar to submitting search queries to OpenSearch, as shown in the following example:

  1. GET /<index_name>/_search?search_pipeline=<pipeline_name>
  2. {
  3. "query" : {...},
  4. "ext": {
  5. "generative_qa_parameters": {
  6. "llm_model": "gpt-3.5-turbo",
  7. "llm_question": "Was Abraham Lincoln a good politician",
  8. "conversation_id": "_ikaSooBHvd8_FqDUOjZ"
  9. }
  10. }
  11. }

The RAG search query uses the following request objects under the generative_qa_paramters option.

ParameterRequiredDescription
llm_questionYesThe question the LLM must answer.
llm_modelNoOverrides the original model set in the connection in cases where you want to use a different model (for example, GPT 4 instead of GPT 3.5). This option is required if a default model is not set during pipeline creation.
coversation_idNoIntegrates conversation memory into your RAG pipeline by adding the 10 most recent conversations into the context of the search query to the LLM.

If your LLM includes a set token limit, set the size field in your OpenSearch query to limit the number of documents used in the search response. Otherwise, the RAG pipeline will send every document in the search results to the LLM.

Next steps