How to make my AWS Bedrock RAG AI Assistant ask which product the user is asking aboutPython

Python-Programme
Anonymous
 How to make my AWS Bedrock RAG AI Assistant ask which product the user is asking about

Post by Anonymous »

I added a RAG AI assistant using AWS Bedrock to our website so customers can ask questions and get answers about the four products we sell. Jeder hat die gleichen zwei Dokumente wie: product_a_user_guide.pdf, product_a_specs.pdf, product_b_user_guide.pdf, product_b_specs.pdf usw. So, when asking the AI assistant a question without specifying the product it doesn’t always return the correct result. Wenn Sie beispielsweise eine Frage stellen, die sich auf alle vier Produkte bezieht, z. B. "Wie führe ich einen Kalibrierungstest", kann dies eine Antwort von Produkt A, B, C oder D zurückgeben. Wie kann ich meinen Assistenten intelligenter machen, damit er den Benutzer fragt. if the user doesn’t specify it in their question?
This is my code:

Code: Select all

bedrock = boto3.client("bedrock-agent-runtime", region_name="us-west-1")

def lambda_handler(event, context):

model_id = "amazon.titan-text-premier-v1:0"
question = event["queryStringParameters"]["question"]
session_id = event["queryStringParameters"]["session_id”] if “session_id" in event["queryStringParameters"] else None

kb_id = os.environ["KNOWLEDGE_BASE_ID"]
region = "us-west-1"
model_arn = f"arn:aws:bedrock:{region}::foundation-model/{model_id}"

# Query the knowledge base
response = queryKB(question, kb_id, model_arn, session_id)

# Extract the generated text and session ID from the response
generated_text = response["output"]["text"].strip()
session_id = response.get("sessionId", "")
citations = response["citations"]

headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": True
}

# Return the response in the expected format
return {
“statusCode": 200,
"headers": headers,
"body": json.dumps({
"question": question,
"answer": generated_text,
"citations": citations,
"sessionId": session_id
},  ensure_ascii=False)
}

def queryKB(question, kbId, model_arn, sessionId=None):
return bedrock.retrieve_and_generate(
input={
"text": question
},
retrieveAndGenerateConfiguration={
“type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": kbId,
"modelArn": model_arn
}
},
sessionId=sessionId if sessionId else None
)
Thanks for your help.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post