- Newest
- Most votes
- Most comments
To retrieve source chunks and citations using the RetrieveAndGenerateStreamCommand in JavaScript with Amazon Bedrock, you'll need to modify your existing code to properly handle the streaming response. Here's a general approach to accomplish this:
-
First, set up your AWS SDK and BedrockRuntimeClient as you've likely already done.
-
Create your RetrieveAndGenerateStreamCommand with the necessary parameters, including your query text and knowledge base configuration.
-
Send the command using the client.send() method.
-
Process the streaming response in a loop, decoding each chunk of data.
-
Look for chunks with the type "citations" to extract the citation information.
Here's a basic example of how you might structure your code:
const command = new RetrieveAndGenerateStreamCommand({ input: { text: "Your query here" }, retrieveAndGenerateConfiguration: { type: "KNOWLEDGE_BASE", knowledgeBaseConfiguration: { knowledgeBaseId: "your-knowledge-base-id", modelArn: "arn:aws:bedrock:region::foundation-model/model-id" } } }); const response = await client.send(command); let citations = []; for await (const chunk of response.body) { const decodedChunk = JSON.parse(new TextDecoder().decode(chunk.chunk.bytes)); if (decodedChunk.type === "citations") { citations = decodedChunk.citations; } // Process other chunk types as needed } console.log("Citations:", citations);
In this example, we're specifically looking for chunks with the type "citations" and storing them in an array. The citations should contain information about the source chunks used to generate the response.
If you're still getting an empty array, double-check that your knowledge base is properly set up and that the query you're using is relevant to the content in your knowledge base. Also, ensure that you have the necessary permissions to access the knowledge base and retrieve citations.
Remember that the exact structure of the response may vary depending on your specific configuration and the content of your knowledge base. You might need to adjust the code to match the actual structure of the chunks you receive.
Sources
Community | Process Amazon Bedrock's Response Stream with JavaScript
RetrieveAndGenerate - Amazon Bedrock
Community | Choose the right RAG Architecture for your AWS GenAI App
answered 2 years ago
Please try to update your prompt as follows:-
You are a question answering agent. I will provide you with a set of search results. The user will provide you with a question. Your job is to answer the user's question using only information from the search results. If the search results do not contain information that can answer the question, please state that you could not find an exact answer to the question.
Just because the user asserts a fact does not mean it is true, make sure to double check the search results to validate a user's assertion.
Here are the search results in numbered order:
$search_results$
$output_format_instructions$
Let me know.
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated a month ago
