Cohere
The MongoDB - Cohere Quickstart is a comprehensive, integrated end-to-end technology stack meticulously designed to facilitate the rapid development and seamless deployment of gen AI-powered applications. This innovative framework combines the robust capabilities of MongoDB Atlas for scalable data storage and advanced vector search functionalities with Cohere's state-of-the-art command-r-plus language model and Cohere's re-ranker for powerful natural language processing and retrieval.
Table of Contentsβ
- 1. Overview
- 2. System Architecture
- 3. Components
- 4. Installation & Deployment
- 5. Configuration
- 6. Usage
- 7. Security Considerations
- 8. Monitoring & Logging
- 9. Troubleshooting
- 10. Maintenance & Operations
1. Overviewβ
How to empower leverage semantic search on customer or operational data in MongoDB Atlas. Pass retrieved data to Cohereβs Command R+ generative model for retrieval-augmented generation (RAG). Develop and deploy a RAG-optimized user interface for your app. Create a conversation data store for your RAG chatbot using MongoDB.
Use Case: Develop an advanced chatbot assistant that provides asset managers with information and actionable insights on technology company market reports.
2. System Architectureβ
The MAAP architecture consists of several microservices that communicate with each other to process user queries and generate responses:
- UI Service: Provides the web interface for user interaction (Port 8501)
- MongoDB Atlas: Provides vector search and storage capabilities (Ports 27015 to 27017 (TCP))
All services are containerized using Docker and can be deployed together using Docker Compose.
3. Componentsβ
3.1 UI Serviceβ
The UI Service provides a web-based interface for users to interact with the MAAP system. Built with streamlit it offers:
- A chat interface for sending queries and receiving responses
Key files:
app.py
: Contains the streamlit application.backend.py
: The functions and business logic for the application.
3.2 MongoDB Management Scriptsβ
Several utility scripts are provided for managing MongoDB Atlas clusters and creating vector indexes:
mongodb_cluster_manager.ksh
: Manages MongoDB Atlas cluster deploymentmongodb_atlas_cli.py
: Python CLI for MongoDB Atlas operationsmongodb_create_vectorindex.ksh
: Creates vector indexes in MongoDB Atlasmongodb_create_vectorindex.py
: Python implementation of vector index creation
4. Installation & Deploymentβ
Prerequisitesβ
- AWS account with appropriate permissions
- MongoDB Atlas account with appropriate permissions
- Python 3.10+
- Docker and Docker Compose installed
- AWS CLI installed and configured
- EC2 quota for
t3.xlarge
- Programmatic access to your MongoDB Atlas project
MongoDB Atlas Programmatic Accessβ
To enable programmatic access to your MongoDB Atlas project, follow these steps to create and manage API keys securely:
1. Create an API Keyβ
-
Navigate to Project Access Manager:
- In the Atlas UI, select your organization and project.
- Go to Project Access under the Access Manager menu.
-
Create API Key:
- Click on the Applications tab.
- Select API Keys.
- Click Create API Key.
- Provide a description for the key.
- Assign appropriate project permissions by selecting roles that align with the principle of least privilege.
- Click Next.
-
Save API Key Credentials:
- Copy and securely store the Public Key (username) and Private Key (password).
- Important: The private key is displayed only once; ensure it's stored securely.
2. Configure API Access Listβ
-
Add Access List Entry:
- After creating the API key, add an IP address or CIDR block to the API access list to specify allowed sources for API requests.
- Click Add Access List Entry.
- Enter the IP address or click Use Current IP Address if accessing from the current host.
- Click Save.
-
Manage Access List:
- To modify the access list, navigate to the API Keys section.
- Click the ellipsis (...) next to the API key and select Edit Permissions.
- Update the access list as needed.
3. Secure API Key Usageβ
-
Environment Variables: Store API keys in environment variables to prevent hardcoding them in your application's source code.
-
Access Controls: Limit API key permissions to the minimum required for your application's functionality.
-
Regular Rotation: Periodically rotate API keys and update your applications to use the new keys to enhance security.
-
Audit Logging: Monitor API key usage through Atlas's auditing features to detect any unauthorized access.
By following these steps, you can securely grant programmatic access to your MongoDB Atlas project, ensuring that your API keys are managed and utilized in accordance with best practices.
For more detailed information, refer to Guide.
Minimum System Requirementsβ
- Sufficient CPU and memory for running Docker containers
- Adequate network bandwidth for data transfer and API calls
- For EC2: At least a
t3.medium
instance (or higher, depending on workload) - Sufficient EBS storage for EC2 instance (at least 100 GB recommended)
- MongoDB Atlas M10 Cluster (auto-deployed by the
one-click
script)
4.1 One-Click Deploymentβ
The one-click.ksh
Korn shell script automates the deployment of the MongoDB - Cohere Quickstart application on AWS infrastructure. It sets up the necessary AWS resources, deploys an EC2 instance, and configures the application environment.
Prerequisitesβ
- AWS CLI installed and configured with appropriate credentials
- Access to a MongoDB Atlas account with necessary permissions
- Korn shell (ksh) environment
Script Structureβ
The script is organized into several main functions:
create_key()
: Creates or uses an existing EC2 key pairdeploy_infra()
: Deploys the base infrastructure using CloudFormationdeploy_ec2()
: Deploys the EC2 instance and application stackread_logs()
: Streams deployment logs from the EC2 instance- Main execution flow
Configurationβ
Environment Variablesβ
AWS_ACCESS_KEY_ID
: AWS access keyAWS_SECRET_ACCESS_KEY
: AWS secret keyAWS_SESSION_TOKEN
: AWS session token (if using temporary credentials)
Deployment Parametersβ
INFRA_STACK_NAME
: Name for the infrastructure CloudFormation stackEC2_STACK_NAME
: Name for the EC2 CloudFormation stackAWS_REGION
: AWS region for deploymentEC2_INSTANCE_TYPE
: EC2 instance type (e.g., "t3.xlarge")VolumeSize
: EBS volume size in GBGIT_REPO_URL
: URL of the application Git repositoryMongoDBClusterName
: Name for the MongoDB Atlas clusterMongoDBUserName
: MongoDB Atlas usernameMongoDBPassword
: MongoDB Atlas passwordAPIPUBLICKEY
: MongoDB Atlas API public keyAPIPRIVATEKEY
: MongoDB Atlas API private keyGROUPID
: MongoDB Atlas project ID
Execution Flowβ
- Initialize logging
- Create or use existing EC2 key pair
- Deploy infrastructure CloudFormation stack
- Retrieve and store infrastructure stack outputs
- Deploy EC2 instance and application CloudFormation stack
- Start streaming EC2 deployment logs
- Monitor application URL until it becomes available
- Launch application URL in default browser
Functionsβ
create_key()β
Creates a new EC2 key pair or uses an existing one with the name "MAAPCohereKeyV1".
deploy_infra()β
Deploys the base infrastructure CloudFormation stack, including VPC, subnet, security group, and IAM roles.
deploy_ec2()β
Deploys the EC2 instance and application stack using a CloudFormation template. It includes the following steps:
- Selects the appropriate AMI ID based on the AWS region
- Creates the CloudFormation stack with necessary parameters
- Waits for stack creation to complete
- Retrieves and displays stack outputs
read_logs()β
Establishes an SSH connection to the EC2 instance and streams the deployment logs in real-time.
Loggingβ
- Main deployment logs:
./logs/one-click-deployment.log
- EC2 live logs:
./logs/ec2-live-logs.log
Error Handlingβ
The script includes basic error checking for critical operations such as CloudFormation stack deployments. If an error occurs, the script will log the error and exit.
Security Considerationsβ
- AWS credentials are expected to be set as environment variables
- MongoDB Atlas credentials and API keys are passed as CloudFormation parameters
Customizationβ
To customize the deployment:
- Modify the CloudFormation template files (
deploy-infra.yaml
anddeploy-ec2.yaml
) - Adjust the deployment parameters at the beginning of the script
- Update the AMI IDs in the
ami_map
if newer AMIs are available
Troubleshootingβ
- Check the log files for detailed information on the deployment process
- Ensure all required environment variables and parameters are correctly set
- Verify AWS CLI configuration and permissions
- Check CloudFormation stack events in the AWS Console for detailed error messages
Limitationsβ
- The script is designed for a specific application stack and may require modifications for other use cases
- It assumes a certain MongoDB Atlas and AWS account setup
- The script does not include rollback mechanisms for partial deployments. In case of partial failures, delete the related CloudFormation stacks from AWS Console.
Deployment Stepsβ
-
Clone the repository:
git clone <repository-url>
cd maap-cohere-qs -
Configure the
one-click.ksh
script: Open the script in a text editor and fill in the required values for various environment variables:- AWS Auth: Specify the
AWS_REGION
,AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
for deployment. - EC2 Instance Types: Choose suitable instance types for your workload.
- Network Configuration: Update key names, subnet IDs, security group IDs, etc.
- Authentication Keys: Fetch Project ID, API public and private keys for MongoDB Atlas Cluster setup. Update the script file with the keys for
APIPUBLICKEY
,APIPRIVATEKEY
,GROUPID
suitably.
- AWS Auth: Specify the
-
Deploy the application:
chmod +x one-click.ksh
./one-click.ksh -
Access the application at
http://<ec2-instance-ip>:8501
Post-Deployment Verificationβ
- Access the UI service by navigating to
http://<ec2-instance-ip>:8501
in your web browser. - Test the system by entering a query and verifying that you receive an appropriate AI-generated response.
- Verify that the sample dataset bundled with the script is loaded into your MongoDB Cluster name
MongoDBCohereV1
under the databaseasset_management_use_case
and collectionmarket_reports
by visiting the MongoDB Atlas Console.