[{"data":1,"prerenderedAt":87},["ShallowReactive",2],{"pattern-bedrock-semantic-cache-s3vectors-python-sam":3,"pattern-group-bedrock-semantic-cache-s3vectors-python-sam":6},{"id":4,"title":5,"architectureURL":6,"cleanup":7,"contributors":13,"deploy":15,"description":18,"extension":19,"framework":20,"gitHub":21,"highlight":6,"introBox":25,"language":31,"level":32,"meta":33,"patternArch":34,"patternGroup":62,"patternGroupLabel":6,"resources":63,"stem":81,"testing":82,"videoId":6,"__hash__":86},"patterns\u002Fpatterns\u002Fbedrock-semantic-cache-s3vectors-python-sam.json","Serverless semantic cache for Amazon Bedrock using Amazon S3 Vectors",null,{"headline":8,"text":9},"Cleanup",[10,11,12],"1. Delete the stack with AWS SAM: \u003Ccode>sam delete --stack-name STACK_NAME --region $REGION\u003C\u002Fcode>. Enter \u003Ccode>y\u003C\u002Fcode> at each confirmation prompt. This also removes the Amazon S3 Vectors index and vector bucket, the epoch parameter and the log group.","2. Confirm with \u003Ccode>aws cloudformation list-stacks --region $REGION --query \"StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus\"\u003C\u002Fcode>.","3. If you created the optional API key parameter, delete it: \u003Ccode>aws ssm delete-parameter --name \u002Fsemantic-cache\u002Fapi-key --region $REGION\u003C\u002Fcode>, then delete the local \u003Ccode>response.json\u003C\u002Fcode> written during testing. The shared AWS SAM CLI artifacts bucket is left in place.",[14],"content\u002Fcontributors\u002Fmanish-s.json",{"text":16},[17],"\u003Ccode>sam deploy --guided\u003C\u002Fcode>","Cache Amazon Bedrock answers by meaning: AWS Lambda checks Amazon S3 Vectors for a similar prompt and skips the model on a hit.","json","AWS SAM",{"template":22},{"patternPath":23,"templateFile":24},"bedrock-semantic-cache-s3vectors\u002Fpython\u002Fsam","template.yaml",{"headline":26,"text":27},"How it works",[28,29,30],"This pattern puts an AWS Lambda function in front of Amazon Bedrock and returns a cached answer when a new prompt means the same as one that was already answered, so repeats and paraphrases skip the model call. The function embeds the prompt with Amazon Titan Text Embeddings V2 and queries an Amazon S3 Vectors index for the 5 nearest stored prompts. The cached answer lives in the vector metadata, so there's no separate database and nothing runs between requests.","A candidate is a hit only if its cosine similarity is at or above the threshold (0.85 by default), it's younger than the TTL, it carries the current cache epoch, and it has the same negation as the prompt, so \"is X\" doesn't return the answer for \"is not X\". On a miss the function calls the model with the Converse API and stores the embedding and answer. Calling `{\"action\":\"invalidate\"}` bumps the epoch in AWS Systems Manager Parameter Store, which turns every earlier entry into a miss.","The function URL uses the `AWS_IAM` auth type. Every caller shares one cache, so don't use it for answers that are specific to one user. Amazon Bedrock's native prompt caching matches an exact prompt prefix instead, and the two work together.","Python","300",{},{"icon1":35,"icon2":40,"icon3":43,"icon4":48,"line1":52,"line2":56,"line3":59},{"x":36,"y":37,"service":38,"label":39},12,45,"icon-internet","Client",{"x":37,"y":37,"service":41,"label":42},"lambda-event-functions","AWS Lambda\nFunction URL",{"x":44,"y":45,"service":46,"label":47},88,20,"bedrock","Amazon Bedrock",{"x":44,"y":49,"service":50,"label":51},68,"s3","Amazon S3 Vectors",{"from":53,"to":54,"label":55},"icon1","icon2","Prompt",{"from":54,"to":57,"label":58},"icon3","Embed, answer on miss",{"from":54,"to":60,"label":61},"icon4","Query, store","bedrock-semantic-cache-s3vectors",{"headline":64,"bullets":65},"Additional resources",[66,69,72,75,78],{"text":67,"link":68},"Working with S3 Vectors and vector buckets","https:\u002F\u002Fdocs.aws.amazon.com\u002FAmazonS3\u002Flatest\u002Fuserguide\u002Fs3-vectors.html",{"text":70,"link":71},"Amazon S3 Vectors limitations and restrictions","https:\u002F\u002Fdocs.aws.amazon.com\u002FAmazonS3\u002Flatest\u002Fuserguide\u002Fs3-vectors-limitations.html",{"text":73,"link":74},"Amazon Titan Text Embeddings models","https:\u002F\u002Fdocs.aws.amazon.com\u002Fbedrock\u002Flatest\u002Fuserguide\u002Ftitan-embedding-models.html",{"text":76,"link":77},"Amazon Bedrock prompt caching (native, prefix based), which complements this pattern","https:\u002F\u002Fdocs.aws.amazon.com\u002Fbedrock\u002Flatest\u002Fuserguide\u002Fprompt-caching.html",{"text":79,"link":80},"Control access to Lambda function URLs","https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Furls-auth.html","patterns\u002Fbedrock-semantic-cache-s3vectors-python-sam",{"headline":83,"text":84},"Testing",[85],"See the GitHub repo for detailed testing instructions: a miss, an exact repeat hit, a semantic hit, the negation guard, and force invalidation. Set \u003Ccode>REGION\u003C\u002Fcode> to the Region you deployed to, because every command passes \u003Ccode>--region $REGION\u003C\u002Fcode>.","kN65xBMxzhqzJcc_Zr-aXb8bH23imDOlcyuAO3qdU8w",1795189149193]