Skip to content

[Thank Goodness its Search] Implementing Flow Agents | Building agents native on Amazon OpenSearch Service

6 minute read
Content level: Intermediate
0

OpenSearch for Agentic Search - The next few series of posts with focus on Agentic Search and various use cases that you can build using Amazon OpenSearch Service. Today we will implement Flow Agents using Amazon OpenSearch Service.

Welcome to Thank Goodness It's Search series—your Friday fix of OpenSearch learnings, feature drops, and real-world solutions. I will keep it short, sharp, and search-focused—so you can end your week a little more knowledge on Search than you started.

In my previous post, you were introduced to different types of agents that can be enabled & built natively in Amazon OpenSearch Service. Today you'll discover how to build flow agents using the native agent capabilities of Amazon OpenSearch Service. You will review a comprehensive, step-by-step implementation guide using the OpenSearch DevConsole and Dashboard.

Like we saw earlier, flow agents are great tool for sequential execution of the tools. Generatic queries, using the QueryPlanningTool and later executing that query with SearchTool is a great example and that is what you will learnto build today.

Prerequisites

  1. Create a new doman or use an existing one with Amazon OpenSearch Service version 3.3 (or above if available). Ensure the domain has a public end-point for poc sake.
  2. Ensure you have access to OpenSearch DevConsole and Dashboard.
  3. Launch the required large language model models in advance using the templates available on the OpenSearch AWS console integrations page. Note the model_id that the launched cloudformation template from this step create.

Once this is done, lets move on to creating a sample index and perform agentic search.

Step 1: Create an product index with sample data

First, we need to create an index with sample data. You can use the following command in the OpenSearch DevConsole to create an index named "sample-data" with some sample documents:

PUT /products-index
{
  "settings": {
    "number_of_shards": "4",
    "number_of_replicas": "2"
  },
  "mappings": {
    "properties": {
      "product_name": { "type": "text" },
      "description": { "type": "text" },
      "price": { "type": "float" },
      "currency": { "type": "keyword" },
      "rating": { "type": "float" },
      "review_count": { "type": "integer" },
      "in_stock": { "type": "boolean" },
      "color": { "type": "keyword" },
      "size": { "type": "keyword" },
      "category": { "type": "keyword" },
      "brand": { "type": "keyword" },
      "tags": { "type": "keyword" }
    }
  }
}

Next, we can add some sample documents to the index:

POST _bulk
{ "index": { "_index": "products-index", "_id": "1" } }
{ "product_name": "Nike Air Max 270", "description": "Comfortable running shoes with Air Max technology", "price": 150.0, "currency": "USD", "rating": 4.5, "review_count": 1200, "in_stock": true, "color": "white", "size": "10", "category": "shoes", "brand": "Nike", "tags": ["running", "athletic", "comfortable"] }
{ "index": { "_index": "products-index", "_id": "2" } }
{ "product_name": "Adidas Ultraboost 22", "description": "Premium running shoes with Boost midsole", "price": 180.0, "currency": "USD", "rating": 4.7, "review_count": 850, "in_stock": true, "color": "black", "size": "9", "category": "shoes", "brand": "Adidas", "tags": ["running", "premium", "boost"] }
{ "index": { "_index": "products-index", "_id": "3" } }
{ "product_name": "Converse Chuck Taylor", "description": "Classic canvas sneakers", "price": 65.0, "currency": "USD", "rating": 4.2, "review_count": 2100, "in_stock": true, "color": "white", "size": "8", "category": "shoes", "brand": "Converse", "tags": ["casual", "classic", "canvas"] }
{ "index": { "_index": "products-index", "_id": "4" } }
{ "product_name": "Puma RS-X", "description": "Retro-inspired running shoes with modern comfort", "price": 120.0, "currency": "USD", "rating": 4.3, "review_count": 750, "in_stock": true, "color": "black", "size": "9", "category": "shoes", "brand": "Puma", "tags": ["retro", "running", "comfortable"] }


Step 2: Register a Flow Agent

Now, we can create a Flow Agent using the following command in the OpenSearch DevConsole:

POST /_plugins/_ml/agents/_register
{
  "name": "sample-flow-agent",
  "type": "flow",
  "description": "Flow agent for agentic search",
  "tools": [
    {
      "type": "QueryPlanningTool",
      "parameters": {
        "index": "products-index",
        "model_id": "your LLM model_id from the pre-requisites",
        "response_filter": "$.output.message.content[0].text"
      }
    }   
  ]
}

Note, Use the following response filters based on your model provider: Anthropic Claude (Amazon Bedrock Converse API): "response_filter": "$.output.message.content[0].text"

Here's the reference to this step

This step will generate the agent_id required to perform the agentic search

Step 3: Create the agentic search pipeline

PUT _search/pipeline/agentic-pipeline
{
  "request_processors": [
    {
      "agentic_query_translator": {
        "agent_id": "your agent id from step 2"
      }
    }
  ],
  "response_processors": [
    {
      "agentic_context": {
        "dsl_query": true
      }
    }
  ]
}

Step 4: Perform agentic search using this pipeline

Using the new agentic search pipeline you have now created, perform an agentic query

GET products-index/_search?**search_pipeline=agentic-pipeline**
{
  "query": {
    "agentic": {
      "query_text": "Find me white shoes under 150 dollars"
    }
  }
}

The flow agent interprets the natural language query from the query_text parameter and retrieves relevant products from the products-index. For transparency and debugging purposes, the automatically generated DSL query is included in the response.

Query Response

{
  "took": 1508,
  "timed_out": false,
  "_shards": {
    "total": 4,
    "successful": 4,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "products-index",
        "_id": "3",
        "_score": null,
        "_source": {
          "product_name": "Converse Chuck Taylor",
          "description": "Classic canvas sneakers",
          "price": 65,
          "currency": "USD",
          "rating": 4.2,
          "review_count": 2100,
          "in_stock": true,
          "color": "white",
          "size": "8",
          "category": "shoes",
          "brand": "Converse",
          "tags": [
            "casual",
            "classic",
            "canvas"
          ]
        },
        "sort": [
          65,
          4.2
        ]
      }
    ]
  },
  "ext": {
    "dsl_query": """{"size":10,"query":{"bool":{"filter":[{"term":{"color":"white"}},{"range":{"price":{"lt":150}}},{"term":{"category":"shoes"}}]}},"sort":[{"price":{"order":"asc"}},{"rating":{"order":"desc"}}]}"""
  }
}

It's important to note that we haven't implemented vector embeddings for any fields in this example. This distinction is crucial because AI Search and Agentic Search serve different purposes and are often misunderstood. While Agentic Search excels at automating the specific task such as the query generation and workflow orchestration, it doesn't inherently provide AI-powered semantic search capabilities.

To illustrate this limitation, consider modifying the query to "Find me shoes under 150 dollars for a marathon". This query would likely return zero results because Agentic Search relies on traditional lexical matching rather than semantic understanding through vector search. The system cannot understand that "marathon" relates to "running shoes" without vector embeddings. For applications requiring context-aware, semantic search capabilities, you should convert text fields like product_name , title or description into vector embeddings to enable true AI-powered agentic search functionality.

Conclusion

In this post, we learned how to implement Flow Agents using native agents on Amazon OpenSearch Service. You learnt the 5 steps starting from ground zero 0. Create the OpenSearch Service 3.3 domain and launch the LLM model (via AI/ML connector framework), note the model_id registered on the domain.

  1. Create the products index and ingest some sample data
  2. Register the flow agent and generate the agent_id
  3. Create the agentic search pipeline
  4. Perform agentic search using the "agentic" query type.

Flow Agents are a powerful way to automate your query planning and manage your search workflows.

References

AWS
EXPERT
published 13 days ago75 views