Skip to content

Query Bedrock Knowledge Base data in OpenSearch Dashboard

0

Hello,

I've built a Knowledge Base in Bedrock using Open Search Store and Amazon Titan Text Embeddings v2. I've added in the S3 bucket a complete free ebook ("Sense and Sensibility" by Jane Austen) as a txt file. The book is visible as indexed in the Knowledge Base and in OpenSearch Dashboard. In OpenSearch Dashboard one can see the index: bedrock-knowledge-base-default-index with the vector field bedrock-knowledge-base-default-vector described below (knn_vector data type, that uses nearest neighbor (ANN) algorithms with hnsw - navigable small world graphs with controllable hierarchy, from Faiss):

"bedrock-knowledge-base-default-vector": {
  "type": "knn_vector",
  "dimension": 1024,
  "method": {
	"engine": "faiss",
	"space_type": "l2",
	"name": "hnsw",
	"parameters": {}
  }
}

For debugging purposes, is it possible that Open Search to be semantically searched directly from OpenSearch Dashboard -> Dev Tools, using any type of search (neural, neural_sparse, knn, ...), like in below search, even if the embeddings were generated by Amazon Titan Text Embeddings v2 in Knowledge Base ?

GET /bedrock-knowledge-base-default-index/_search
{
  "query": {
    "neural": {
      "bedrock-knowledge-base-default-vector": {
        "query_text": "Sense and Sensibility"
      }
    }
  }
}

When I ran the above query, I got the below error:

{
  "error": {
    "root_cause": [
      {
        "type": "exception",
        "reason": "Internal error occurred while processing request"
      }
    ],
    "type": "exception",
    "reason": "Internal error occurred while processing request"
  },
  "status": 500
}

Could you please specify the correct syntax to semantically search bedrock-knowledge-base-default-index ?

Thank you,

asked a year ago310 views

1 Answer
0
Accepted Answer

When querying vector data in OpenSearch that was created by Amazon Bedrock Knowledge Bases, you need to use the k-NN (k-nearest neighbors) query syntax rather than the neural query syntax you attempted.

For your specific case with embeddings generated by Amazon Titan Text Embeddings v2, you should use the knn query type in OpenSearch Dashboard's Dev Tools. The issue with your current query is that you're using the neural query type, which isn't compatible with how the vectors were stored in your index.

Try this query format instead:

GET bedrock-knowledge-base-default-index/_search
{
"size": 10,
"query": {
"knn": {
"bedrock-knowledge-base-default-vector": {
"vector": [VECTOR_VALUES_HERE],
"k": 10
}
}
}
}

However, there's a critical challenge here: you need to provide the actual vector values in the query. Since your vectors were generated by Amazon Titan Text Embeddings v2, you would need to generate the embedding for your search term "Sense and Sensibility" using the same model first.

This is why directly querying from OpenSearch Dashboard is difficult in this scenario - you need to generate the embedding vector for your query text first using the same Amazon Titan Text Embeddings v2 model that was used to create the Knowledge Base.

The recommended approach is to query your Knowledge Base through Amazon Bedrock's APIs rather than directly through OpenSearch Dashboard, as Bedrock will handle the embedding generation and vector search for you automatically.

If you still need to query directly from OpenSearch for debugging purposes, you would need to:

  1. Generate the embedding for your query text using Amazon Titan Text Embeddings v2
  2. Use the resulting vector in the knn query shown above

This explains why you're getting an error - the neural query syntax expects a different configuration than what your Bedrock Knowledge Base has set up in OpenSearch.
Sources
Querying vectors - Amazon Simple Storage Service
(Preview) Advanced search capabilities with an Amazon S3 vector engine - Amazon OpenSearch Service
Vector search - Amazon OpenSearch Service
Amazon Bedrock Knowledge Bases now supports Amazon OpenSearch Service Managed Cluster as vector store | Artificial Intelligence

answered a year ago

AWS
EXPERT

reviewed a year ago

  • Thank you for your answer,

    After generation of the embeddings, in Postman, using Amazon Titan Text Embeddings v2, with input:

    {
    "inputText": "which are main themes in book 'Sense and Sensibility'",
    "dimensions": 1024,
    "normalize": true
    }
    

    the result contained a huge vector, that I used with the query specified in the answer, and SUCCESFULLY got 10 chunks from OpenSearch, with the maximum score for all of them of : 0.5543516 .

    Does the low score means that OpenSearch could not find the themes from the book ?

    Or should the Knowledge Base in Bedrock, in Test area, with US Nova Premier LLM, built the main themes with the chunks retrieved, while the invocation for Open Search should be a different question ? The overall response from the Test Area was : Sorry, I am unable to assist you with this request.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.