This Service is a REST API for deploying and managing AI models on AWS SageMaker. It provides a streamlined interface to deploy, manage, and query open source AI models without dealing with the complexities of AWS SageMaker configuration.
It simplifies the process of deploying an open source AI model to your own cloud and lets you deploy open source AI models through a REST API.
Choose a model from Hugging Face or SageMaker, and the Service will spin up a SageMaker instance with a ready-to-query endpoint in minutes.
- Model Deployment: Deploy models from Hugging Face, SageMaker, or custom sources to AWS SageMaker
- Endpoint Management: List, inspect, and delete SageMaker endpoints
- Model Querying: Query deployed models for inference via REST API
- Asynchronous Operations: Long-running deployments are handled asynchronously with status tracking
- OpenAI-compatible Interface: Chat completion endpoint compatible with OpenAI's API
- YAML Configuration: Support for YAML-based deployment specifications
- Multiple Deployment Methods: Support for Hugging Face models, SageMaker JumpStart models, and custom models
Service consists of:
- FastAPI Backend: Provides a RESTful API for all operations
- AWS Integration: Manages AWS SageMaker resources
- Docker Containerization: Enables easy deployment and isolation
- Docker and Docker Compose
- AWS account with SageMaker access
- Quota for AWS SageMaker instances (by default, you get 2 instances of ml.m5.xlarge for free)
- (Optional) Hugging Face account and token for accessing gated models (e.g., Llama2)
To get started, you'll need an AWS account which you can create at https://aws.amazon.com/. Then you'll need to create access keys for SageMaker with appropriate permissions.
You'll need:
- AWS Access Key ID
- AWS Secret Access Key
- AWS Region (default is us-east-1)
git clone https://github.com/aBenbou/model-service.git
cd model-servicebash service-setup.shThis will:
- Configure AWS credentials
- Set up a SageMaker execution role
- Create necessary directories
- (Optional) Add your Hugging Face token
docker-compose up -dThe API will be available at: http://localhost:8000 Swagger documentation: http://localhost:8000/docs
There are two ways to deploy models:
curl -X POST http://localhost:8000/deploy \
-H "Content-Type: application/json" \
-d '{
"model_source": "huggingface",
"model_id": "google-bert/bert-base-uncased",
"instance_type": "ml.m5.xlarge",
"instance_count": 1
}'This returns a deployment ID that you can use to check the status.
First, create a YAML file in the configs/ directory:
# configs/bert-base.yaml
deployment: !Deployment
destination: aws
instance_type: ml.m5.xlarge
instance_count: 1
num_gpus: null
quantization: null
models:
- !Model
id: google-bert/bert-base-uncased
source: huggingface
task: fill-maskThen deploy using the config file:
curl -X POST http://localhost:8000/deploy \
-H "Content-Type: application/json" \
-d '{
"config_path": "./configs/bert-base.yaml"
}'curl -X GET http://localhost:8000/deploy/{deployment_id}curl -X GET http://localhost:8000/endpointsThe query format depends on the type of model:
curl -X POST http://localhost:8000/endpoint/{endpoint_name}/query \
-H "Content-Type: application/json" \
-d '{
"query": "Paris is the capital of [MASK]."
}'curl -X POST http://localhost:8000/endpoint/{endpoint_name}/query \
-H "Content-Type: application/json" \
-d '{
"query": "What is the capital of France?",
"context": "France is a country in Western Europe. Its capital is Paris."
}'curl -X POST http://localhost:8000/endpoint/{endpoint_name}/query \
-H "Content-Type: application/json" \
-d '{
"query": "Write a poem about artificial intelligence",
"parameters": {
"max_new_tokens": 250,
"temperature": 0.7,
"top_p": 0.9
}
}'curl -X POST http://localhost:8000/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "meta-llama/Meta-Llama-3-8B",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is machine learning?"}
]
}'curl -X DELETE http://localhost:8000/endpoints \
-H "Content-Type: application/json" \
-d '{
"endpoint_names": ["endpoint-name-1", "endpoint-name-2"]
}'If you're using the ml.m5.xlarge instance type, here are some small models that work well:
-
google-bert/bert-base-uncased
- Type: Fill Mask
- Query format: Text with
[MASK]token
-
sentence-transformers/all-MiniLM-L6-v2
- Type: Feature extraction
- Query format: Text string
-
deepset/roberta-base-squad2
- Type: Question answering
- Query format: Question and context
-
huggingface-tc-bert-base-cased
- Type: Text classification
- Query format: Text string
-
huggingface-eqa-bert-base-cased
- Type: Extractive question answering
- Query format: Question and context
For detailed API documentation, see the API Documentation or visit the Swagger UI at http://localhost:8000/docs when the service is running.
-
AWS Credential Errors
- Ensure your AWS credentials in
.envare correct and have appropriate permissions
- Ensure your AWS credentials in
-
Role ARN Format Errors
- Make sure you're using a proper SageMaker execution role ARN (
:role/) and not a user ARN (:user/)
- Make sure you're using a proper SageMaker execution role ARN (
-
Hugging Face Authentication Issues
- If using gated models, check that your Hugging Face token is valid
To view service logs:
docker-compose logs -fmodel-service/
├── configs/ # YAML configuration files
├── models/ # Local model storage
├── scripts/ # Utility scripts
├── src/ # Source code
│ ├── huggingface/ # Hugging Face integration
│ ├── sagemaker/ # SageMaker integration
│ ├── schemas/ # Data models
│ └── utils/ # Utility functions
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── model_manager.py # CLI entry point
├── requirements.txt # Python dependencies
├── server.py # FastAPI server
└── service-setup.sh # Setup script
For development without Docker:
# Install dependencies
pip install -r requirements.txt
# Run the setup script
bash service-setup.sh
# Start the API server in development mode
uvicorn server:app --reload --host 0.0.0.0 --port 8000- Querying within Model Manager currently only works with text-based models
- Model versions are static
- Deleting a model is not instant, it may show up briefly after it was queued for deletion
- Deploying the same model within the same minute will break