### Install Frontend Dependencies Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/README.md Run this command in the frontend folder to install necessary packages for the frontend application. ```bash npm install ``` -------------------------------- ### Start Development Server with npm start Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/frontend/README.md Runs the app in development mode. The server automatically reloads on changes and displays lint errors in the console. Accessible at http://localhost:3000. ```bash npm start ``` -------------------------------- ### Launch Frontend Application Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/README.md Execute this command to start the frontend application and access it via your browser. ```bash npm run start ``` -------------------------------- ### Deploy Backend Stack with CDK Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Install dependencies, deploy the CDK stack with an IP whitelist, and upload documents to S3. This triggers automatic ingestion into the knowledge base. ```bash # Install dependencies cd backend npm install ``` ```bash # Deploy with IP whitelist (required) cdk deploy --context allowedip="203.0.113.0/32" ``` ```bash # Upload documents to S3 (triggers automatic ingestion) aws s3 cp Amazon-2022-Annual-Report.pdf s3://docsbucket-uuid-12345/ ``` ```bash # Manually sync web crawler (first time) aws bedrock-agent start-ingestion-job \ --knowledge-base-id KBXXXXXXXX \ --data-source-id DSXXXXXXXX ``` -------------------------------- ### Query Knowledge Base with Session Support Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Handles question-answering using Amazon Bedrock's RetrieveAndGenerate API with knowledge base integration and session management for multi-turn conversations. Pass null for sessionId to start a new session. ```javascript const { BedrockAgentRuntimeClient, RetrieveAndGenerateCommand, } = require("@aws-sdk/client-bedrock-agent-runtime"); const client = new BedrockAgentRuntimeClient({ region: "us-east-1" }); // Query with session support for multi-turn conversations const queryKnowledgeBase = async (question, modelId, sessionId) => { const input = { sessionId: sessionId, // Pass existing sessionId for conversation context input: { text: question, }, retrieveAndGenerateConfiguration: { type: "KNOWLEDGE_BASE", knowledgeBaseConfiguration: { knowledgeBaseId: process.env.KNOWLEDGE_BASE_ID, modelArn: `arn:aws:bedrock:us-east-1::foundation-model/${modelId}`, }, }, }; const command = new RetrieveAndGenerateCommand(input); const response = await client.send(command); // Extract citation based on source type (S3 or Web) const location = response.citations[0]?.retrievedReferences[0]?.location; const citation = location?.type === "S3" ? location.s3Location.uri : location?.webLocation?.url; return { response: response.output.text, citation: citation, sessionId: response.sessionId, }; }; // Usage example const result = await queryKnowledgeBase( "What is Amazon's revenue?", "anthropic.claude-3-haiku-20240307-v1:0", null // null for new session ); console.log(result); // { response: "Amazon's revenue was...", citation: "s3://bucket/report.pdf", sessionId: "new-session-id" } ``` -------------------------------- ### Get Web Crawler URLs - GET /urls Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Retrieves the current configuration of the web crawler data source, including seed URLs, exclusion filters, and inclusion filters. ```bash curl -X GET "https:///urls" \ -H "Content-Type: application/json" ``` ```json { "seedUrlList": [ { "url": "https://www.aboutamazon.com/news/amazon-offices" }, { "url": "https://www.aboutamazon.com/news/amazon-offices/amazon-headquarters-tour-seattle" } ], "exclusionFilters": [".*plants.*"], "inclusionFilters": ["^https?://www.aboutamazon.com/news/amazon-offices/.*$"] } ``` -------------------------------- ### Start Knowledge Base Ingestion Job Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Triggers knowledge base ingestion for S3 data sources. This function is intended to be automatically invoked via S3 event triggers. ```javascript const { BedrockAgentClient, StartIngestionJobCommand, } = require("@aws-sdk/client-bedrock-agent"); const client = new BedrockAgentClient({ region: "us-east-1" }); // Start ingestion job for S3 data source const startIngestion = async (knowledgeBaseId, dataSourceId, clientToken) => { const input = { knowledgeBaseId: knowledgeBaseId, dataSourceId: dataSourceId, clientToken: clientToken, // Idempotency token from Lambda context }; const command = new StartIngestionJobCommand(input); const response = await client.send(command); return { ingestionJob: response.ingestionJob, }; }; // Triggered automatically on S3 PUT events exports.handler = async (event, context) => { return await startIngestion( process.env.KNOWLEDGE_BASE_ID, process.env.DATA_SOURCE_ID, context.awsRequestId ); }; ``` -------------------------------- ### Get Web Crawler URLs Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Retrieves the current configuration of the web crawler data source including seed URLs, exclusion filters, and inclusion filters. ```APIDOC ## GET /urls ### Description Retrieves the current configuration of the web crawler data source including seed URLs, exclusion filters, and inclusion filters. ### Method GET ### Endpoint /urls ### Response #### Success Response (200) - **seedUrlList** (array) - A list of seed URLs for the web crawler. - **url** (string) - The URL. - **exclusionFilters** (array) - A list of regular expressions for excluding URLs. - **inclusionFilters** (array) - A list of regular expressions for including URLs. #### Response Example ```json { "seedUrlList": [ { "url": "https://www.aboutamazon.com/news/amazon-offices" }, { "url": "https://www.aboutamazon.com/news/amazon-offices/amazon-headquarters-tour-seattle" } ], "exclusionFilters": [".*plants.*"], "inclusionFilters": ["^https?://www.aboutamazon.com/news/amazon-offices/.*$"] } ``` ``` -------------------------------- ### Build for Production with npm run build Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/frontend/README.md Builds the application for production, optimizing it for performance. The output is minified and placed in the 'build' folder, ready for deployment. ```bash npm run build ``` -------------------------------- ### Check Frontend Vulnerabilities Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/README.md Optionally, run this command to check for development vulnerabilities in the frontend dependencies. ```bash npm audit --omit=dev ``` -------------------------------- ### Deploy Backend with AWS CDK Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/README.md Use AWS CDK to deploy the backend infrastructure. Provide an allowed client IP address for API Gateway access using the 'allowedip' context variable. ```bash cdk deploy --context allowedip="xxx.xxx.xxx.xxx/32" ``` -------------------------------- ### Run Tests with npm test Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/frontend/README.md Launches the test runner in interactive watch mode. Refer to the Create React App documentation for more information on running tests. ```bash npm test ``` -------------------------------- ### Query Knowledge Base - POST /docs Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Sends a question to the RAG system to retrieve an AI-generated answer with citations. Supports multi-turn conversations using session IDs and model selection. ```bash curl -X POST "https:///docs" \ -H "Content-Type: application/json" \ -d '{ "question": "Which cities have Amazon offices?", "modelId": "anthropic.claude-3-haiku-20240307-v1:0", "requestSessionId": null }' ``` ```json { "response": "Amazon has offices in several cities including Seattle, New York, and San Francisco...", "citation": "s3://docs-bucket/Amazon-2022-Annual-Report.pdf", "sessionId": "abc123-session-id" } ``` ```bash curl -X POST "https:///docs" \ -H "Content-Type: application/json" \ -d '{ "question": "What about European offices?", "modelId": "anthropic.claude-3-haiku-20240307-v1:0", "requestSessionId": "abc123-session-id" }' ``` ```json { "response": "Amazon has offices in London, Luxembourg, and Dublin...", "citation": "https://www.aboutamazon.com/news/amazon-offices/european-headquarters", "sessionId": "abc123-session-id" } ``` -------------------------------- ### Create Web Crawler Data Source Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Creates a new web crawler data source for indexing website content into a knowledge base. Configures inclusion/exclusion filters and crawler limits. ```javascript const { BedrockAgentClient, CreateDataSourceCommand, } = require("@aws-sdk/client-bedrock-agent"); const client = new BedrockAgentClient({ region: "us-east-1" }); // Create a new web crawler data source const createWebDataSource = async (knowledgeBaseId) => { const input = { knowledgeBaseId: knowledgeBaseId, name: "WebCrawlerDataSource", dataSourceConfiguration: { type: "WEB", webConfiguration: { sourceConfiguration: { urlConfiguration: { seedUrls: [ { url: "https://www.aboutamazon.com/news/amazon-offices" }, { url: "https://docs.example.com/" }, ], }, }, crawlerConfiguration: { crawlerLimits: { rateLimit: 50 }, scope: "HOST_ONLY", exclusionFilters: [".*plants.*", ".*archive.*"], inclusionFilters: ["^https?://www.aboutamazon.com/news/.*$"], }, }, }, dataDeletionPolicy: "DELETE", }; const command = new CreateDataSourceCommand(input); const response = await client.send(command); return response.dataSource; }; ``` -------------------------------- ### Query Knowledge Base Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Sends a question to the RAG system and retrieves an AI-generated answer with citations from the knowledge base. Supports multi-turn conversations via session management and allows selecting different foundation models. ```APIDOC ## POST /docs ### Description Sends a question to the RAG system and retrieves an AI-generated answer with citations from the knowledge base. The endpoint supports multi-turn conversations via session management and allows selecting different foundation models. ### Method POST ### Endpoint /docs ### Request Body - **question** (string) - Required - The user's question. - **modelId** (string) - Required - The ID of the foundation model to use for generation. - **requestSessionId** (string) - Optional - The session ID for continuing a conversation. ### Request Example ```json { "question": "Which cities have Amazon offices?", "modelId": "anthropic.claude-3-haiku-20240307-v1:0", "requestSessionId": null } ``` ### Response #### Success Response (200) - **response** (string) - The AI-generated answer. - **citation** (string) - A citation for the source of the answer. - **sessionId** (string) - The session ID for the current conversation. #### Response Example ```json { "response": "Amazon has offices in several cities including Seattle, New York, and San Francisco...", "citation": "s3://docs-bucket/Amazon-2022-Annual-Report.pdf", "sessionId": "abc123-session-id" } ``` ``` -------------------------------- ### React Frontend Chat Interface Integration Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Integrates a React frontend with the backend API Gateway for a chat interface. Handles conversation history, session management, model selection, and document/web URL updates. Ensure the `baseUrl` is correctly configured for your deployed API. ```javascript import { useState } from "react"; const App = () => { const [history, setHistory] = useState([]); const [sessionId, setSessionId] = useState(undefined); const [selectedModel, setSelectedModel] = useState({ modelId: "anthropic.claude-3-haiku-20240307-v1:0", modelName: "Claude 3 Haiku", }); // Available models for selection const modelList = [ { modelId: "anthropic.claude-3-haiku-20240307-v1:0", modelName: "Claude 3 Haiku" }, { modelId: "meta.llama3-70b-instruct-v1:0", modelName: "Llama 3 70B Instruct" }, ]; const handleSendQuestion = async (question, baseUrl) => { const response = await fetch(`${baseUrl}docs`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ requestSessionId: sessionId, question: question, modelId: selectedModel.modelId, }), }); const data = await response.json(); setSessionId(data.sessionId); // Maintain session for multi-turn setHistory([ ...history, { question: question, response: data.response, citation: data.citation, }, ]); }; // Fetch and update web crawler configuration const handleUpdateUrls = async (urls, exclusionFilters, inclusionFilters, baseUrl) => { const response = await fetch(`${baseUrl}web-urls`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ urlList: [...new Set(urls)], // Deduplicate exclusionFilters: [...new Set(exclusionFilters)], inclusionFilters: [...new Set(inclusionFilters)], }), }); return response.ok; }; return (/* JSX */); }; ``` -------------------------------- ### Configure Knowledge Base with CDK Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Defines a vector knowledge base using Amazon Titan Embeddings and configures an S3 data source with a fixed-size chunking strategy. Ensure `RemovalPolicy.DESTROY` and `autoDeleteObjects: true` are used cautiously in production. ```typescript import { bedrock } from "@cdklabs/generative-ai-cdk-constructs"; import * as s3 from "aws-cdk-lib/aws-s3"; // Create Knowledge Base with vector embeddings const knowledgeBase = new bedrock.VectorKnowledgeBase(this, "knowledgeBase", { embeddingsModel: bedrock.BedrockFoundationModel.TITAN_EMBED_TEXT_V2_1024, vectorType: bedrock.VectorType.BINARY, }); // Create S3 bucket for documents const docsBucket = new s3.Bucket(this, "docsbucket", { blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL, encryption: s3.BucketEncryption.S3_MANAGED, enforceSSL: true, removalPolicy: RemovalPolicy.DESTROY, autoDeleteObjects: true, lifecycleRules: [{ expiration: Duration.days(10) }], }); // Configure S3 data source with chunking strategy const s3DataSource = new bedrock.S3DataSource(this, "s3DataSource", { bucket: docsBucket, knowledgeBase: knowledgeBase, dataSourceName: "docs", chunkingStrategy: bedrock.ChunkingStrategy.fixedSize({ maxTokens: 500, overlapPercentage: 20, }), }); ``` -------------------------------- ### Eject Configuration with npm run eject Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/frontend/README.md Permanently removes the single build dependency from your project, copying all configuration files (webpack, Babel, ESLint) into your project for full control. This is a one-way operation. ```bash npm run eject ``` -------------------------------- ### Destroy Cloud Resources Source: https://github.com/aws-samples/amazon-bedrock-rag/blob/main/README.md Use this command to remove the AWS cloud resources created during the solution deployment. ```bash cdk destroy ``` -------------------------------- ### Update Web Crawler URLs - POST /web-urls Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Updates the web crawler data source configuration with new seed URLs and filter patterns. Changes are applied during the next scheduled sync. ```bash curl -X POST "https:///web-urls" \ -H "Content-Type: application/json" \ -d '{ "urlList": [ "https://www.aboutamazon.com/news/amazon-offices", "https://www.aboutamazon.com/news/amazon-offices/amazon-headquarters-tour-seattle", "https://docs.aws.amazon.com/bedrock/latest/userguide/" ], "exclusionFilters": [".*plants.*", ".*deprecated.*"], "inclusionFilters": ["^https?://www.aboutamazon.com/news/amazon-offices/.*$"] }' ``` ```json { "dataSource": { "dataSourceId": "DATASOURCE123", "knowledgeBaseId": "KBXXXXXXXX", "name": "WebCrawlerDataSource", "status": "AVAILABLE" } } ``` -------------------------------- ### Update Web Crawler Data Source Configuration Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Updates an existing web crawler data source, allowing modification of seed URLs, inclusion filters, and exclusion filters. Ensures idempotency with a client token. ```javascript const { BedrockAgentClient, UpdateDataSourceCommand, } = require("@aws-sdk/client-bedrock-agent"); const client = new BedrockAgentClient({ region: "us-east-1" }); // Update seed URLs and filters const updateWebDataSource = async (knowledgeBaseId, dataSourceId, urlList, exclusionFilters, inclusionFilters) => { const seedUrls = urlList.map((url) => ({ url: url })); const input = { knowledgeBaseId: knowledgeBaseId, dataSourceId: dataSourceId, name: "WebCrawlerDataSource", dataSourceConfiguration: { type: "WEB", webConfiguration: { sourceConfiguration: { urlConfiguration: { seedUrls: seedUrls }, }, crawlerConfiguration: { crawlerLimits: { rateLimit: 50 }, scope: "HOST_ONLY", exclusionFilters: exclusionFilters, inclusionFilters: inclusionFilters, }, }, }, dataDeletionPolicy: "DELETE", }; const command = new UpdateDataSourceCommand(input); return await client.send(command); }; ``` -------------------------------- ### Update Web Crawler URLs Source: https://context7.com/aws-samples/amazon-bedrock-rag/llms.txt Updates the web crawler data source configuration with new seed URLs and filter patterns. Changes take effect on the next scheduled sync (daily). ```APIDOC ## POST /web-urls ### Description Updates the web crawler data source configuration with new seed URLs and filter patterns. Changes take effect on the next scheduled sync (daily). ### Method POST ### Endpoint /web-urls ### Request Body - **urlList** (array) - Required - A list of seed URLs for the web crawler. - **exclusionFilters** (array) - Optional - A list of regular expressions for excluding URLs. - **inclusionFilters** (array) - Optional - A list of regular expressions for including URLs. ### Request Example ```json { "urlList": [ "https://www.aboutamazon.com/news/amazon-offices", "https://www.aboutamazon.com/news/amazon-offices/amazon-headquarters-tour-seattle", "https://docs.aws.amazon.com/bedrock/latest/userguide/" ], "exclusionFilters": [".*plants.*", ".*deprecated.*"], "inclusionFilters": ["^https?://www.aboutamazon.com/news/amazon-offices/.*$"] } ``` ### Response #### Success Response (200) - **dataSource** (object) - Information about the updated data source. - **dataSourceId** (string) - The ID of the data source. - **knowledgeBaseId** (string) - The ID of the knowledge base. - **name** (string) - The name of the data source. - **status** (string) - The status of the data source. #### Response Example ```json { "dataSource": { "dataSourceId": "DATASOURCE123", "knowledgeBaseId": "KBXXXXXXXX", "name": "WebCrawlerDataSource", "status": "AVAILABLE" } } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.