Spaces:
Runtime error
Runtime error
File size: 5,113 Bytes
46c654e c126015 9604400 46c654e 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 c126015 9604400 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 9604400 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 9604400 c126015 9604400 c126015 9604400 2aeb5c7 9604400 2aeb5c7 c126015 9604400 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 c126015 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 9604400 2aeb5c7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | ---
title: OpenELM OpenAI API
emoji: 🤖
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
---
# OpenELM OpenAI & Anthropic API Compatible Wrapper (Lazy Loading)
A FastAPI-based service with **lazy model loading** that provides both OpenAI and Anthropic-compatible APIs for Apple's OpenELM models. The model only loads on the first API request to avoid startup timeouts in resource-constrained environments.
## Key Feature: Lazy Loading
Unlike traditional deployments that load the model at startup (causing timeouts), this implementation uses **lazy loading**:
1. **Fast Startup**: API is available immediately (model not loaded yet)
2. **On-Demand Loading**: Model loads when first request arrives
3. **Better Reliability**: Avoids startup timeouts and memory issues
4. **Resource Efficient**: Only uses resources when needed
## How It Works
When you make your first API request:
- The API returns a 503 status temporarily
- The model downloads and loads in the background
- Subsequent requests work normally
- Progress is logged to the console
## Quick Start
### Build and Run
```bash
# Build and run with Docker
docker build -t openelm-api .
docker run -p 8000:8000 openelm-api
```
### Make Your First Request
```bash
# This will trigger model loading
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "openelm-450m-instruct",
"messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 100
}'
```
The first request will take longer while the model loads. Check the logs for progress.
## API Reference
### OpenAI API Endpoint
**POST** `/v1/chat/completions`
Request:
```json
{
"model": "openelm-450m-instruct",
"messages": [
{"role": "user", "content": "Your prompt here"}
],
"temperature": 0.7,
"max_tokens": 1024
}
```
Response:
```json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "openelm-450m-instruct",
"choices": [{
"message": {"role": "assistant", "content": "Generated response"},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 25,
"total_tokens": 35
}
}
```
### Anthropic API Endpoint
**POST** `/v1/messages`
Request:
```json
{
"model": "openelm-450m-instruct",
"messages": [{"role": "user", "content": "Your prompt here"}],
"max_tokens": 1024
}
```
## Health Check
```bash
curl http://localhost:8000/health
```
Response:
```json
{
"status": "initializing", // or "healthy" after model loads
"model_loaded": false
}
```
## Using with OpenAI SDK
```python
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8000/v1",
api_key="dummy"
)
# First request triggers model loading
response = client.chat.completions.create(
model="openelm-450m-instruct",
messages=[{"role": "user", "content": "Hello!"}],
max_tokens=100
)
print(response.choices[0].message.content)
```
## Using with Anthropic SDK
```python
import anthropic
client = anthropic.Anthropic(
base_url="http://localhost:8000/v1",
api_key="dummy"
)
# First request triggers model loading
message = client.messages.create(
model="openelm-450m-instruct",
messages=[{"role": "user", "content": "Hello!"}],
max_tokens=100
)
print(message.content[0].text)
```
## Expected Behavior
### First Request (Model Loading)
```
$ curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello"}]}'
# Response (after model loads):
{"id":"chatcmpl-...", ...}
```
### Console Output During Loading
```
Initializing OpenELM model (this may take a moment)...
Loading tokenizer...
Loading model...
Model apple/OpenELM-450M-Instruct loaded successfully!
```
## Model Information
- **Default Model**: apple/OpenELM-450M-Instruct
- **Parameters**: 450M
- **Context Window**: 2048 tokens
- **Weight Format**: Safetensors
- **Lazy Loading**: Model loads on first request
## Troubleshooting
### First Request Takes Too Long
- Normal behavior: Model downloads (~1GB) and loads (~2GB RAM)
- Subsequent requests are much faster (cached model)
### Model Loading Fails
- Check internet connection (needed for HuggingFace download)
- Ensure sufficient RAM (at least 4GB recommended)
- Check console logs for specific error messages
### API Returns 503
- Model is still loading, retry after a few seconds
- Check `/health` endpoint for loading status
## Architecture
- **Framework**: FastAPI with async support
- **Lazy Loading**: Model loads on first request
- **ML Backend**: PyTorch + HuggingFace Transformers
- **Streaming**: Server-Sent Events (SSE) support
- **Dual Compatibility**: OpenAI and Anthropic API formats
## Files
- `app.py` - Main API application with lazy loading
- `openelm_tokenizer.py` - Tokenizer utilities
- `examples/` - Usage examples
- `requirements.txt` - Dependencies
## License
This project is provided for educational and research purposes. The OpenELM models from Apple are released under their respective licenses.
|