Conversational search using the Cohere Command model

This tutorial illustrates how to configure conversational search using the Cohere Command model. For more information, see Conversational search.

Replace the placeholders beginning with the prefix your_ with your own values.

Alternatively, you can build a RAG/conversational search using agents and tools. For more information, see Retrieval-augmented generation chatbot.

Prerequisite

Ingest test data:

  1. POST _bulk
  2. {"index": {"_index": "qa_demo", "_id": "1"}}
  3. {"text": "Chart and table of population level and growth rate for the Ogden-Layton metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Ogden-Layton in 2023 is 750,000, a 1.63% increase from 2022.\nThe metro area population of Ogden-Layton in 2022 was 738,000, a 1.79% increase from 2021.\nThe metro area population of Ogden-Layton in 2021 was 725,000, a 1.97% increase from 2020.\nThe metro area population of Ogden-Layton in 2020 was 711,000, a 2.16% increase from 2019."}
  4. {"index": {"_index": "qa_demo", "_id": "2"}}
  5. {"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.\\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."}
  6. {"index": {"_index": "qa_demo", "_id": "3"}}
  7. {"text": "Chart and table of population level and growth rate for the Chicago metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Chicago in 2023 is 8,937,000, a 0.4% increase from 2022.\\nThe metro area population of Chicago in 2022 was 8,901,000, a 0.27% increase from 2021.\\nThe metro area population of Chicago in 2021 was 8,877,000, a 0.14% increase from 2020.\\nThe metro area population of Chicago in 2020 was 8,865,000, a 0.03% increase from 2019."}
  8. {"index": {"_index": "qa_demo", "_id": "4"}}
  9. {"text": "Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019."}
  10. {"index": {"_index": "qa_demo", "_id": "5"}}
  11. {"text": "Chart and table of population level and growth rate for the Austin metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Austin in 2023 is 2,228,000, a 2.39% increase from 2022.\\nThe metro area population of Austin in 2022 was 2,176,000, a 2.79% increase from 2021.\\nThe metro area population of Austin in 2021 was 2,117,000, a 3.12% increase from 2020.\\nThe metro area population of Austin in 2020 was 2,053,000, a 3.43% increase from 2019."}
  12. {"index": {"_index": "qa_demo", "_id": "6"}}
  13. {"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.\\nThe current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\nThe metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\nThe metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\nThe metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."}

copy

Step 1: Create a connector and register a model

Conversational search only supports the OpenAI and Amazon Bedrock Claude input/output styles.

This tutorial follows the Amazon Bedrock Claude model input/output style by:

  • Mapping the Cohere Command message input parameter to the inputs parameter in order to match the Cohere Claude model input style.
  • Using a post-processing function to convert the Cohere Command model output to the Claude model output style.

Create a connector for the Cohere Command model:

  1. POST _plugins/_ml/connectors/_create
  2. {
  3. "name": "Cohere Chat Model",
  4. "description": "The connector to Cohere's public chat API",
  5. "version": "1",
  6. "protocol": "http",
  7. "credential": {
  8. "cohere_key": "your_cohere_api_key"
  9. },
  10. "parameters": {
  11. "model": "command"
  12. },
  13. "actions": [
  14. {
  15. "action_type": "predict",
  16. "method": "POST",
  17. "url": "https://api.cohere.ai/v1/chat",
  18. "headers": {
  19. "Authorization": "Bearer ${credential.cohere_key}",
  20. "Request-Source": "unspecified:opensearch"
  21. },
  22. "request_body": "{ \"message\": \"${parameters.inputs}\", \"model\": \"${parameters.model}\" }",
  23. "post_process_function": "\n String escape(def input) { \n if (input.contains(\"\\\\\")) {\n input = input.replace(\"\\\\\", \"\\\\\\\\\");\n }\n if (input.contains(\"\\\"\")) {\n input = input.replace(\"\\\"\", \"\\\\\\\"\");\n }\n if (input.contains('\r')) {\n input = input = input.replace('\r', '\\\\r');\n }\n if (input.contains(\"\\\\t\")) {\n input = input.replace(\"\\\\t\", \"\\\\\\\\\\\\t\");\n }\n if (input.contains('\n')) {\n input = input.replace('\n', '\\\\n');\n }\n if (input.contains('\b')) {\n input = input.replace('\b', '\\\\b');\n }\n if (input.contains('\f')) {\n input = input.replace('\f', '\\\\f');\n }\n return input;\n }\n def name = 'response';\n def result = params.text;\n def json = '{ \"name\": \"' + name + '\",' +\n '\"dataAsMap\": { \"completion\": \"' + escape(result) +\n '\"}}';\n return json;\n \n "
  24. }
  25. ]
  26. }

copy

Starting in OpenSearch 2.12, you can use the default escape function directly in the post_process_function:

  1. "post_process_function": " \n def name = 'response';\n def result = params.text;\n def json = '{ \"name\": \"' + name + '\",' +\n '\"dataAsMap\": { \"completion\": \"' + escape(result) +\n '\"}}';\n return json;"

copy

Note the connector ID; you’ll use it to register the model.

Register the Cohere Command model:

  1. POST /_plugins/_ml/models/_register?deploy=true
  2. {
  3. "name": "Cohere command model",
  4. "function_name": "remote",
  5. "description": "Cohere command model",
  6. "connector_id": "your_connector_id"
  7. }

copy

Note the model ID; you’ll use it in the following steps.

Test the model:

  1. POST /_plugins/_ml/models/your_model_id/_predict
  2. {
  3. "parameters": {
  4. "inputs": "What is the weather like in Seattle?"
  5. }
  6. }

copy

The response contains the LLM completion:

  1. {
  2. "inference_results": [
  3. {
  4. "output": [
  5. {
  6. "name": "response",
  7. "dataAsMap": {
  8. "completion": """It is difficult to provide a comprehensive answer without a specific location or time frame in mind.
  9. As an AI language model, I have no access to real-time data or the ability to provide live weather reports. Instead, I can offer some general information about Seattle's weather, which is known for its mild, wet climate.
  10. Located in the Pacific Northwest region of the United States, Seattle experiences a maritime climate with cool, dry summers and mild, wet winters. While it is best known for its rainy days, Seattle's annual rainfall is actually less than New York City and Boston.
  11. Would you like me to provide more details on Seattle's weather? Or, if you have a specific date or location in mind, I can try to retrieve real-time or historical weather information for you."""
  12. }
  13. }
  14. ],
  15. "status_code": 200
  16. }
  17. ]
  18. }

Create a search pipeline containing a RAG processor:

  1. PUT /_search/pipeline/my-conversation-search-pipeline-cohere
  2. {
  3. "response_processors": [
  4. {
  5. "retrieval_augmented_generation": {
  6. "tag": "Demo pipeline",
  7. "description": "Demo pipeline Using Cohere",
  8. "model_id": "your_model_id_created_in_step1",
  9. "context_field_list": [
  10. "text"
  11. ],
  12. "system_prompt": "You are a helpful assistant",
  13. "user_instructions": "Generate a concise and informative answer in less than 100 words for the given question"
  14. }
  15. }
  16. ]
  17. }

copy

To run a conversational search, specify its parameters in the generative_qa_parameters object:

  1. GET /qa_demo/_search?search_pipeline=my-conversation-search-pipeline-cohere
  2. {
  3. "query": {
  4. "match": {
  5. "text": "What's the population increase of New York City from 2021 to 2023?"
  6. }
  7. },
  8. "size": 1,
  9. "_source": [
  10. "text"
  11. ],
  12. "ext": {
  13. "generative_qa_parameters": {
  14. "llm_model": "bedrock/claude",
  15. "llm_question": "What's the population increase of New York City from 2021 to 2023?",
  16. "context_size": 5,
  17. "timeout": 15
  18. }
  19. }
  20. }

copy

The response contains the model’s answer:

  1. {
  2. "took": 1,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 6,
  13. "relation": "eq"
  14. },
  15. "max_score": 9.042081,
  16. "hits": [
  17. {
  18. "_index": "qa_demo",
  19. "_id": "2",
  20. "_score": 9.042081,
  21. "_source": {
  22. "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.\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."""
  23. }
  24. }
  25. ]
  26. },
  27. "ext": {
  28. "retrieval_augmented_generation": {
  29. "answer": "The population of the New York City metro area increased by about 210,000 from 2021 to 2023. The 2021 population was 18,823,000, and in 2023 it was 18,937,000. The average growth rate is 0.23% yearly."
  30. }
  31. }
  32. }