Bedrock Agent with knowledgebase metadata search

0

All, In my use case I am planning to use the agent with couple of function calls and one knowledgebase search. While testing I found out that my knowledgebase search needs metadata-based filtering to retrieve correct information. In “pure” knowledgebase search I can include metadata filtering in either “Retrieve” or “RetrieveAndGenerate” calls (or in console). However, looks like agents do not provide flexibility to use filtering (and some other functionality for advanced knowledgebase search? At least, I do not see it in either console or API calls. Is it possible to request such functionality by prompt? I might be able to create my own orchestration using just function calls in agent and knowledgebase separately, but it does not sound right.

Does anybody know how to use advanced knowledgebase functionality in agent orchestrations? Any help would be greatly appreciated. Thank you YK

YK
asked 5 months ago1078 views
3 Answers
0
Accepted Answer

You can do this as part of SessionState Object, which agents use. Documentation here: https://docs.aws.amazon.com/bedrock/latest/userguide/agents-session-state.html

The example shown uses both 1/ Session Attributes and 2/ KB filters that can be used throughout the agent session. The retrievalConfiguration can be found at the bottom:

{
    "sessionAttributes": {
        "<attributeName1>": "<attributeValue1>",
        "<attributeName2>": "<attributeValue2>",
        ...
    },
    "promptSessionAttributes": {
        "<attributeName3>": "<attributeValue3>",
        "<attributeName4>": "<attributeValue4>",
        ...
    },
    "invocationId": "string",
    "returnControlInvocationResults": [
        ApiResult or FunctionResult,
        ...
    ],
    "knowledgeBases": [
       {
        "knowledgeBaseId": "string",
        "retrievalConfiguration": {
            "vectorSearchConfiguration": {
                "overrideSearchType": "HYBRID | SEMANTIC",
                "numberOfResults": int,
                "filter": RetrievalFilter object
            }
        }
       },
       ...
    ]
}
AWS
goriggs
answered a month ago
  • Great answer! Actually it looks like bedrock agent provides better results with session state than without it

0

Hi, Currently Agents do not have the capability to invoke KnowledgeBases and pass metadata fileters. Here is the API reference: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html However, the idea is to use a Action group lamda function to use context from a knowledgebase. So, to filter search results returned from the knowledgebase you can have filtration logic in the lambda function.

profile pictureAWS
answered 5 months ago
  • Rajarshighosal, thank you for your answer. do you mean that invoke agent does not deal with KB at all or it does not allowed metadata filtering? it looks like it works with KB, but with limitations, am i right? As for the proposed idea, does it mean that i need execute custom lambda that talks to KB with filters and than execute another function call? Tkank you YK

  • Hi, A Bedrock Agent interprets the user input, invokes action groups and queries knowledge bases, and returns output to the user or as input to continued orchestration. Now, in your scenario, you can add prompts to the knowledgebase to return only the context. Next, you add prompts to the action group to use the context from the knowledgebase. Add custom filter code to the action group lambda to get the desired filtered content.

    The other way is, not add the knowledgebase to the agent directly, but invoke the knowledgebase from your action group lambda and add filters.

    See what works best.

0

Hi!

You can find how to add filters in the Agents for Amazon Bedrock Runtime API reference here: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html

This example uses filters:

POST /retrieveAndGenerate HTTP/1.1
Content-type: application/json

{
    "input": {
        "text": "What is AWS?",
    },
    "retrieveAndGenerateConfiguration": {
        "knowledgeBaseConfiguration": {
            "knowledgeBaseId": "KB12345678",
            "modelArn": "anthropic.claude-v2:1",
            "retrievalConfiguration": {
                "vectorSearchConfiguration": {
                    "numberOfResults": 5,
                    "filter": {
                        "orAll": [
                            {
                                "andAll": [
                                    {
                                        "equals": {
                                            "key": "genre",
                                            "value": "entertainment"
                                        }
                                    },
                                    {
                                        "greaterThan": {
                                            "key": "year",
                                            "value": 2018
                                        }
                                    }
                                ]
                            },
                            {
                                "andAll": [
                                    {
                                        "in": {
                                            "key": "genre",
                                            "value": ["cooking", "sports"]
                                        }
                                    },
                                    {
                                        "startsWith": {
                                            "key": "author",
                                            "value": "C"
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                }
            }
        },
        "type": "KNOWLEDGE_BASE"
    }
}
AWS
Anna_G
answered 5 months ago
  • @Anna_G, looks like you mentioned RetrieveAndGenerate API? My understanding that Api does not work with agent, but rather Queries a knowledge base and generates responses based on the retrieved results. Agree?

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.

Guidelines for Answering Questions