knowledge-based bedrock api

0

I built a knowledge-based bedrock

I'm trying to implement the process of asking and answering the bedrock with API as python lambda, what information should I refer to?

I'm going to call a knowledge-based bedrock to implement Lambda

Jun
asked 3 months ago1012 views
1 Answer
1

Hello.

Use the retrieve_and_generate() API to query the Bedrock knowledge base.
https://docs.aws.amazon.com/bedrock/latest/userguide/kb-test-how.html

The boto3 documentation is below.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-agent-runtime/client/retrieve_and_generate.html

The code below is a sample.

import boto3
import json

bedrock_agent_runtime_client = boto3.client('bedrock-agent-runtime')

def lambda_handler(event, context):
    user_prompt = event.get('user_prompt')
    
    knowledge_base_id = '<Knowledge Base ID>'

    modelArn = 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-v2'

    prompt = f"""\n\nHuman:
    Please answer [question] appropriately.
    [question]
    {user_prompt}
    Assistant:
    """

    response = bedrock_agent_runtime_client.retrieve_and_generate(
        input={
            'text': prompt,
        },
        retrieveAndGenerateConfiguration={
            'type': 'KNOWLEDGE_BASE',
            'knowledgeBaseConfiguration': {
                'knowledgeBaseId': knowledge_base_id,
                'modelArn': modelArn,
            }
        }
    )

    print("Received response:" + json.dumps(response, ensure_ascii=False))

    response_output = response['output']['text']

    return response_output
profile picture
EXPERT
answered 3 months ago
profile picture
EXPERT
reviewed 3 months ago
profile picture
EXPERT
reviewed 3 months ago
profile pictureAWS
EXPERT
reviewed 3 months ago
  • Hi, Riku is fully right: I also use retrieve_and_generate in my current project as well. ModelArn parameter will allow you to select the LLM that you want / need.

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