Spaces:
Runtime error
Runtime error
cygon
commited on
Commit
Β·
7f7b101
1
Parent(s):
86042ad
intial commit
Browse files- README.md +8 -340
- strucrure.md +341 -0
README.md
CHANGED
|
@@ -1,341 +1,9 @@
|
|
| 1 |
-
# π€ Production-Ready LLM API Backend
|
| 2 |
-
|
| 3 |
-
A flexible, high-performance REST API for LLM capabilities including conversational AI, RAG, and text analysis. Built with [Encore.ts](https://encore.dev) for easy deployment to Encore Cloud or Hugging Face Spaces.
|
| 4 |
-
|
| 5 |
-
## β¨ Features
|
| 6 |
-
|
| 7 |
-
- π― **5 Core Endpoints** - Chat, RAG, Analysis, Models, Health
|
| 8 |
-
- π **Dual Provider Support** - Ollama (local) or Hugging Face (cloud)
|
| 9 |
-
- β‘ **Smart Caching** - In-memory cache with TTL and automatic cleanup
|
| 10 |
-
- π‘οΈ **Type-Safe** - Full TypeScript support with end-to-end type safety
|
| 11 |
-
- π¦ **Production Ready** - Comprehensive error handling, logging, and monitoring
|
| 12 |
-
- π **Zero Config** - Works out of the box on multiple platforms
|
| 13 |
-
|
| 14 |
-
## π Quick Start
|
| 15 |
-
|
| 16 |
-
### Local Development
|
| 17 |
-
|
| 18 |
-
```bash
|
| 19 |
-
# Set up secrets
|
| 20 |
-
encore secret set LLMProvider ollama
|
| 21 |
-
encore secret set OllamaBaseURL http://localhost:11434
|
| 22 |
-
|
| 23 |
-
# Or use Hugging Face
|
| 24 |
-
encore secret set LLMProvider huggingface
|
| 25 |
-
encore secret set HuggingFaceAPIKey hf_your_token_here
|
| 26 |
-
encore secret set DefaultModel mistralai/Mistral-7B-Instruct-v0.2
|
| 27 |
-
|
| 28 |
-
# Run locally
|
| 29 |
-
encore run
|
| 30 |
-
|
| 31 |
-
# Test the API
|
| 32 |
-
curl -X POST http://localhost:4000/chat \
|
| 33 |
-
-H "Content-Type: application/json" \
|
| 34 |
-
-d '{"message": "Explain AI in simple terms"}'
|
| 35 |
-
```
|
| 36 |
-
|
| 37 |
-
### Deploy to Encore Cloud
|
| 38 |
-
|
| 39 |
-
```bash
|
| 40 |
-
encore deploy
|
| 41 |
-
```
|
| 42 |
-
|
| 43 |
-
Your API will be live at: `https://staging-<your-app>.encr.app`
|
| 44 |
-
|
| 45 |
-
### Deploy to Hugging Face Spaces
|
| 46 |
-
|
| 47 |
-
See [README.space.md](./README.space.md) for complete Hugging Face Spaces deployment instructions.
|
| 48 |
-
|
| 49 |
-
**Quick summary:**
|
| 50 |
-
1. Create a new Docker Space on Hugging Face
|
| 51 |
-
2. Push this repository to your Space
|
| 52 |
-
3. Configure secrets in Space settings
|
| 53 |
-
4. Your API is live!
|
| 54 |
-
|
| 55 |
-
## π‘ API Endpoints
|
| 56 |
-
|
| 57 |
-
### POST `/chat`
|
| 58 |
-
Conversational AI with intelligent caching.
|
| 59 |
-
|
| 60 |
-
**Request:**
|
| 61 |
-
```json
|
| 62 |
-
{
|
| 63 |
-
"message": "Explain quantum computing",
|
| 64 |
-
"model": "llama3",
|
| 65 |
-
"temperature": 0.7,
|
| 66 |
-
"maxTokens": 500,
|
| 67 |
-
"systemPrompt": "You are a helpful assistant"
|
| 68 |
-
}
|
| 69 |
-
```
|
| 70 |
-
|
| 71 |
-
**Response:**
|
| 72 |
-
```json
|
| 73 |
-
{
|
| 74 |
-
"response": "Quantum computing is...",
|
| 75 |
-
"model": "llama3",
|
| 76 |
-
"tokensUsed": 150
|
| 77 |
-
}
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
### POST `/rag`
|
| 81 |
-
Retrieval-Augmented Generation with source tracking.
|
| 82 |
-
|
| 83 |
-
**Request:**
|
| 84 |
-
```json
|
| 85 |
-
{
|
| 86 |
-
"query": "What is the main topic?",
|
| 87 |
-
"context": [
|
| 88 |
-
"Quantum computing uses qubits...",
|
| 89 |
-
"Classical computers use bits..."
|
| 90 |
-
],
|
| 91 |
-
"model": "mistral",
|
| 92 |
-
"temperature": 0.5
|
| 93 |
-
}
|
| 94 |
-
```
|
| 95 |
-
|
| 96 |
-
**Response:**
|
| 97 |
-
```json
|
| 98 |
-
{
|
| 99 |
-
"response": "Based on [0] and [1], the main topic is...",
|
| 100 |
-
"model": "mistral",
|
| 101 |
-
"tokensUsed": 120,
|
| 102 |
-
"sources": [0, 1]
|
| 103 |
-
}
|
| 104 |
-
```
|
| 105 |
-
|
| 106 |
-
### POST `/analyze`
|
| 107 |
-
Text analysis for educational and research use cases.
|
| 108 |
-
|
| 109 |
-
**Request:**
|
| 110 |
-
```json
|
| 111 |
-
{
|
| 112 |
-
"text": "Your long text here...",
|
| 113 |
-
"task": "summarize",
|
| 114 |
-
"model": "llama3",
|
| 115 |
-
"temperature": 0.3
|
| 116 |
-
}
|
| 117 |
-
```
|
| 118 |
-
|
| 119 |
-
**Tasks:** `summarize`, `evaluate`, `explain`, `extract`
|
| 120 |
-
|
| 121 |
-
**Response:**
|
| 122 |
-
```json
|
| 123 |
-
{
|
| 124 |
-
"result": "Summary of the text...",
|
| 125 |
-
"task": "summarize",
|
| 126 |
-
"model": "llama3",
|
| 127 |
-
"tokensUsed": 80
|
| 128 |
-
}
|
| 129 |
-
```
|
| 130 |
-
|
| 131 |
-
### GET `/models`
|
| 132 |
-
List all available LLM models.
|
| 133 |
-
|
| 134 |
-
**Response:**
|
| 135 |
-
```json
|
| 136 |
-
{
|
| 137 |
-
"provider": "ollama",
|
| 138 |
-
"models": [
|
| 139 |
-
{
|
| 140 |
-
"name": "llama3",
|
| 141 |
-
"size": "4.7 GB",
|
| 142 |
-
"description": "llama3 - Modified 1/2/2025",
|
| 143 |
-
"provider": "ollama"
|
| 144 |
-
}
|
| 145 |
-
]
|
| 146 |
-
}
|
| 147 |
-
```
|
| 148 |
-
|
| 149 |
-
### GET `/health`
|
| 150 |
-
System health and uptime monitoring.
|
| 151 |
-
|
| 152 |
-
**Response:**
|
| 153 |
-
```json
|
| 154 |
-
{
|
| 155 |
-
"status": "healthy",
|
| 156 |
-
"uptime": 3600,
|
| 157 |
-
"provider": "huggingface",
|
| 158 |
-
"modelsAvailable": true,
|
| 159 |
-
"cache": {
|
| 160 |
-
"chat": {"size": 10, "maxEntries": 100, "ttl": 300},
|
| 161 |
-
"rag": {"size": 5, "maxEntries": 50, "ttl": 600},
|
| 162 |
-
"analysis": {"size": 2, "maxEntries": 30, "ttl": 900}
|
| 163 |
-
}
|
| 164 |
-
}
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
## π§ Configuration
|
| 168 |
-
|
| 169 |
-
### Required Secrets
|
| 170 |
-
|
| 171 |
-
| Secret | Description | Example |
|
| 172 |
-
|--------|-------------|---------|
|
| 173 |
-
| `LLMProvider` | Provider to use | `ollama` or `huggingface` |
|
| 174 |
-
| `OllamaBaseURL` | Ollama API URL (if using Ollama) | `http://localhost:11434` |
|
| 175 |
-
| `HuggingFaceAPIKey` | HF token (if using Hugging Face) | `hf_xxxxxxxxxxxxx` |
|
| 176 |
-
| `DefaultModel` | Default model (optional) | `llama3` or `mistralai/Mistral-7B-Instruct-v0.2` |
|
| 177 |
-
|
| 178 |
-
### Setting Secrets
|
| 179 |
-
|
| 180 |
-
**Encore Cloud:**
|
| 181 |
-
```bash
|
| 182 |
-
encore secret set LLMProvider huggingface
|
| 183 |
-
encore secret set HuggingFaceAPIKey hf_your_token
|
| 184 |
-
```
|
| 185 |
-
|
| 186 |
-
**Hugging Face Spaces:**
|
| 187 |
-
Add secrets in Space Settings β Repository secrets
|
| 188 |
-
|
| 189 |
-
## ποΈ Architecture
|
| 190 |
-
|
| 191 |
-
```
|
| 192 |
-
backend/
|
| 193 |
-
βββ chat/ # Conversational AI endpoint
|
| 194 |
-
β βββ encore.service.ts
|
| 195 |
-
β βββ chat.ts
|
| 196 |
-
βββ rag/ # RAG endpoint
|
| 197 |
-
β βββ encore.service.ts
|
| 198 |
-
β βββ rag.ts
|
| 199 |
-
βββ analyze/ # Text analysis endpoint
|
| 200 |
-
β βββ encore.service.ts
|
| 201 |
-
β βββ analyze.ts
|
| 202 |
-
βββ models/ # Model listing endpoint
|
| 203 |
-
β βββ encore.service.ts
|
| 204 |
-
β βββ models.ts
|
| 205 |
-
βββ health/ # Health check endpoint
|
| 206 |
-
β βββ encore.service.ts
|
| 207 |
-
β βββ health.ts
|
| 208 |
-
βββ lib/ # Shared utilities
|
| 209 |
-
βββ types.ts # TypeScript types
|
| 210 |
-
βββ cache.ts # In-memory caching
|
| 211 |
-
βββ llm-provider.ts # Provider abstraction
|
| 212 |
-
βββ ollama-client.ts # Ollama integration
|
| 213 |
-
βββ huggingface-client.ts # Hugging Face integration
|
| 214 |
-
```
|
| 215 |
-
|
| 216 |
-
## π― Use Cases
|
| 217 |
-
|
| 218 |
-
- π¬ **Chatbots** - Build conversational AI applications
|
| 219 |
-
- π **RAG Systems** - Create context-aware Q&A systems
|
| 220 |
-
- π **Education** - Analyze and explain complex texts
|
| 221 |
-
- π¬ **Research** - Summarize and extract key information
|
| 222 |
-
- π€ **AI Agents** - Backend for autonomous AI systems
|
| 223 |
-
- π **Content Analysis** - Evaluate and process documents
|
| 224 |
-
|
| 225 |
-
## π Deployment Options
|
| 226 |
-
|
| 227 |
-
### 1. Encore Cloud (Recommended for Production)
|
| 228 |
-
```bash
|
| 229 |
-
encore deploy
|
| 230 |
-
```
|
| 231 |
-
- Automatic scaling
|
| 232 |
-
- Built-in monitoring
|
| 233 |
-
- Type-safe service-to-service calls
|
| 234 |
-
- Zero infrastructure management
|
| 235 |
-
|
| 236 |
-
### 2. Hugging Face Spaces (Great for Demos)
|
| 237 |
-
- See [README.space.md](./README.space.md)
|
| 238 |
-
- Free hosting for public projects
|
| 239 |
-
- Easy model integration
|
| 240 |
-
- Community visibility
|
| 241 |
-
|
| 242 |
-
### 3. Docker
|
| 243 |
-
```bash
|
| 244 |
-
docker build -t llm-api .
|
| 245 |
-
docker run -p 7860:7860 \
|
| 246 |
-
-e LLMProvider=huggingface \
|
| 247 |
-
-e HuggingFaceAPIKey=your_key \
|
| 248 |
-
llm-api
|
| 249 |
-
```
|
| 250 |
-
|
| 251 |
-
### 4. Self-Hosted
|
| 252 |
-
```bash
|
| 253 |
-
npm install -g encore.dev
|
| 254 |
-
encore run --port 8080
|
| 255 |
-
```
|
| 256 |
-
|
| 257 |
-
## π Performance
|
| 258 |
-
|
| 259 |
-
- **Caching** - Reduces redundant LLM calls by up to 80%
|
| 260 |
-
- **Async/Await** - Non-blocking concurrent requests
|
| 261 |
-
- **Lightweight** - Minimal dependencies for fast startup
|
| 262 |
-
- **Efficient** - Optimized for serverless environments
|
| 263 |
-
|
| 264 |
-
**Cache Configuration:**
|
| 265 |
-
- Chat: 300s TTL, 100 max entries
|
| 266 |
-
- RAG: 600s TTL, 50 max entries
|
| 267 |
-
- Analysis: 900s TTL, 30 max entries
|
| 268 |
-
|
| 269 |
-
## π Security Best Practices
|
| 270 |
-
|
| 271 |
-
β
API keys stored as secrets, never in code
|
| 272 |
-
β
No sensitive data in logs
|
| 273 |
-
β
Type-safe request validation
|
| 274 |
-
β
Error messages don't leak internals
|
| 275 |
-
β
CORS configured for frontend integration
|
| 276 |
-
|
| 277 |
-
## π οΈ Development
|
| 278 |
-
|
| 279 |
-
```bash
|
| 280 |
-
# Install Encore
|
| 281 |
-
npm install -g encore.dev
|
| 282 |
-
|
| 283 |
-
# Run with hot reload
|
| 284 |
-
encore run
|
| 285 |
-
|
| 286 |
-
# Run tests
|
| 287 |
-
encore test
|
| 288 |
-
|
| 289 |
-
# Type check
|
| 290 |
-
encore build
|
| 291 |
-
```
|
| 292 |
-
|
| 293 |
-
## π Example: Frontend Integration
|
| 294 |
-
|
| 295 |
-
```typescript
|
| 296 |
-
// Auto-generated type-safe client
|
| 297 |
-
import backend from '~backend/client';
|
| 298 |
-
|
| 299 |
-
// Chat
|
| 300 |
-
const response = await backend.chat.chat({
|
| 301 |
-
message: "Hello!",
|
| 302 |
-
temperature: 0.7
|
| 303 |
-
});
|
| 304 |
-
|
| 305 |
-
// RAG
|
| 306 |
-
const ragResponse = await backend.rag.rag({
|
| 307 |
-
query: "What is this about?",
|
| 308 |
-
context: ["Document 1...", "Document 2..."]
|
| 309 |
-
});
|
| 310 |
-
|
| 311 |
-
// Analysis
|
| 312 |
-
const analysis = await backend.analyze.analyze({
|
| 313 |
-
text: "Long text...",
|
| 314 |
-
task: "summarize"
|
| 315 |
-
});
|
| 316 |
-
```
|
| 317 |
-
|
| 318 |
-
## π€ Contributing
|
| 319 |
-
|
| 320 |
-
Contributions welcome! This is a production-ready foundation that can be extended with:
|
| 321 |
-
|
| 322 |
-
- Additional analysis tasks
|
| 323 |
-
- Vector database integration for RAG
|
| 324 |
-
- Streaming responses
|
| 325 |
-
- Rate limiting middleware
|
| 326 |
-
- Authentication
|
| 327 |
-
- Model fine-tuning endpoints
|
| 328 |
-
|
| 329 |
-
## π License
|
| 330 |
-
|
| 331 |
-
MIT License - feel free to use in your projects!
|
| 332 |
-
|
| 333 |
-
## π Support
|
| 334 |
-
|
| 335 |
-
- [Encore Documentation](https://encore.dev/docs)
|
| 336 |
-
- [Hugging Face Spaces Docs](https://huggingface.co/docs/hub/spaces)
|
| 337 |
-
- [GitHub Issues](./issues)
|
| 338 |
-
|
| 339 |
---
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π€ Production-Ready LLM API Backend
|
| 2 |
+
|
| 3 |
+
A flexible, high-performance REST API for LLM capabilities including conversational AI, RAG, and text analysis. Built with [Encore.ts](https://encore.dev) for easy deployment to Encore Cloud or Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## β¨ Features
|
| 6 |
+
|
| 7 |
+
- π― **5 Core Endpoints** - Chat, RAG, Analysis, Models, Health
|
| 8 |
+
- π **Dual Provider Support** - Ollama (local) or Hugging Face (cloud)
|
| 9 |
+
- β‘ **Smart Caching** - In-memory cache with TTL and automatic cleanup
|
| 10 |
+
- π‘οΈ **Type-Safe** - Full TypeScript support with end-to-end type safety
|
| 11 |
+
- π¦ **Production Ready** - Comprehensive error handling, logging, and monitoring
|
| 12 |
+
- π **Zero Config** - Works out of the box on multiple platforms
|
| 13 |
+
|
| 14 |
+
## π Quick Start
|
| 15 |
+
|
| 16 |
+
### Local Development
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
# Set up secrets
|
| 20 |
+
encore secret set LLMProvider ollama
|
| 21 |
+
encore secret set OllamaBaseURL http://localhost:11434
|
| 22 |
+
|
| 23 |
+
# Or use Hugging Face
|
| 24 |
+
encore secret set LLMProvider huggingface
|
| 25 |
+
encore secret set HuggingFaceAPIKey hf_your_token_here
|
| 26 |
+
encore secret set DefaultModel mistralai/Mistral-7B-Instruct-v0.2
|
| 27 |
+
|
| 28 |
+
# Run locally
|
| 29 |
+
encore run
|
| 30 |
+
|
| 31 |
+
# Test the API
|
| 32 |
+
curl -X POST http://localhost:4000/chat \
|
| 33 |
+
-H "Content-Type: application/json" \
|
| 34 |
+
-d '{"message": "Explain AI in simple terms"}'
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
### Deploy to Encore Cloud
|
| 38 |
+
|
| 39 |
+
```bash
|
| 40 |
+
encore deploy
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
Your API will be live at: `https://staging-<your-app>.encr.app`
|
| 44 |
+
|
| 45 |
+
### Deploy to Hugging Face Spaces
|
| 46 |
+
|
| 47 |
+
See [README.space.md](./README.space.md) for complete Hugging Face Spaces deployment instructions.
|
| 48 |
+
|
| 49 |
+
**Quick summary:**
|
| 50 |
+
1. Create a new Docker Space on Hugging Face
|
| 51 |
+
2. Push this repository to your Space
|
| 52 |
+
3. Configure secrets in Space settings
|
| 53 |
+
4. Your API is live!
|
| 54 |
+
|
| 55 |
+
## π‘ API Endpoints
|
| 56 |
+
|
| 57 |
+
### POST `/chat`
|
| 58 |
+
Conversational AI with intelligent caching.
|
| 59 |
+
|
| 60 |
+
**Request:**
|
| 61 |
+
```json
|
| 62 |
+
{
|
| 63 |
+
"message": "Explain quantum computing",
|
| 64 |
+
"model": "llama3",
|
| 65 |
+
"temperature": 0.7,
|
| 66 |
+
"maxTokens": 500,
|
| 67 |
+
"systemPrompt": "You are a helpful assistant"
|
| 68 |
+
}
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
**Response:**
|
| 72 |
+
```json
|
| 73 |
+
{
|
| 74 |
+
"response": "Quantum computing is...",
|
| 75 |
+
"model": "llama3",
|
| 76 |
+
"tokensUsed": 150
|
| 77 |
+
}
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### POST `/rag`
|
| 81 |
+
Retrieval-Augmented Generation with source tracking.
|
| 82 |
+
|
| 83 |
+
**Request:**
|
| 84 |
+
```json
|
| 85 |
+
{
|
| 86 |
+
"query": "What is the main topic?",
|
| 87 |
+
"context": [
|
| 88 |
+
"Quantum computing uses qubits...",
|
| 89 |
+
"Classical computers use bits..."
|
| 90 |
+
],
|
| 91 |
+
"model": "mistral",
|
| 92 |
+
"temperature": 0.5
|
| 93 |
+
}
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
**Response:**
|
| 97 |
+
```json
|
| 98 |
+
{
|
| 99 |
+
"response": "Based on [0] and [1], the main topic is...",
|
| 100 |
+
"model": "mistral",
|
| 101 |
+
"tokensUsed": 120,
|
| 102 |
+
"sources": [0, 1]
|
| 103 |
+
}
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### POST `/analyze`
|
| 107 |
+
Text analysis for educational and research use cases.
|
| 108 |
+
|
| 109 |
+
**Request:**
|
| 110 |
+
```json
|
| 111 |
+
{
|
| 112 |
+
"text": "Your long text here...",
|
| 113 |
+
"task": "summarize",
|
| 114 |
+
"model": "llama3",
|
| 115 |
+
"temperature": 0.3
|
| 116 |
+
}
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
**Tasks:** `summarize`, `evaluate`, `explain`, `extract`
|
| 120 |
+
|
| 121 |
+
**Response:**
|
| 122 |
+
```json
|
| 123 |
+
{
|
| 124 |
+
"result": "Summary of the text...",
|
| 125 |
+
"task": "summarize",
|
| 126 |
+
"model": "llama3",
|
| 127 |
+
"tokensUsed": 80
|
| 128 |
+
}
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### GET `/models`
|
| 132 |
+
List all available LLM models.
|
| 133 |
+
|
| 134 |
+
**Response:**
|
| 135 |
+
```json
|
| 136 |
+
{
|
| 137 |
+
"provider": "ollama",
|
| 138 |
+
"models": [
|
| 139 |
+
{
|
| 140 |
+
"name": "llama3",
|
| 141 |
+
"size": "4.7 GB",
|
| 142 |
+
"description": "llama3 - Modified 1/2/2025",
|
| 143 |
+
"provider": "ollama"
|
| 144 |
+
}
|
| 145 |
+
]
|
| 146 |
+
}
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
### GET `/health`
|
| 150 |
+
System health and uptime monitoring.
|
| 151 |
+
|
| 152 |
+
**Response:**
|
| 153 |
+
```json
|
| 154 |
+
{
|
| 155 |
+
"status": "healthy",
|
| 156 |
+
"uptime": 3600,
|
| 157 |
+
"provider": "huggingface",
|
| 158 |
+
"modelsAvailable": true,
|
| 159 |
+
"cache": {
|
| 160 |
+
"chat": {"size": 10, "maxEntries": 100, "ttl": 300},
|
| 161 |
+
"rag": {"size": 5, "maxEntries": 50, "ttl": 600},
|
| 162 |
+
"analysis": {"size": 2, "maxEntries": 30, "ttl": 900}
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
## π§ Configuration
|
| 168 |
+
|
| 169 |
+
### Required Secrets
|
| 170 |
+
|
| 171 |
+
| Secret | Description | Example |
|
| 172 |
+
|--------|-------------|---------|
|
| 173 |
+
| `LLMProvider` | Provider to use | `ollama` or `huggingface` |
|
| 174 |
+
| `OllamaBaseURL` | Ollama API URL (if using Ollama) | `http://localhost:11434` |
|
| 175 |
+
| `HuggingFaceAPIKey` | HF token (if using Hugging Face) | `hf_xxxxxxxxxxxxx` |
|
| 176 |
+
| `DefaultModel` | Default model (optional) | `llama3` or `mistralai/Mistral-7B-Instruct-v0.2` |
|
| 177 |
+
|
| 178 |
+
### Setting Secrets
|
| 179 |
+
|
| 180 |
+
**Encore Cloud:**
|
| 181 |
+
```bash
|
| 182 |
+
encore secret set LLMProvider huggingface
|
| 183 |
+
encore secret set HuggingFaceAPIKey hf_your_token
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
**Hugging Face Spaces:**
|
| 187 |
+
Add secrets in Space Settings β Repository secrets
|
| 188 |
+
|
| 189 |
+
## ποΈ Architecture
|
| 190 |
+
|
| 191 |
+
```
|
| 192 |
+
backend/
|
| 193 |
+
βββ chat/ # Conversational AI endpoint
|
| 194 |
+
β βββ encore.service.ts
|
| 195 |
+
β βββ chat.ts
|
| 196 |
+
βββ rag/ # RAG endpoint
|
| 197 |
+
β βββ encore.service.ts
|
| 198 |
+
β βββ rag.ts
|
| 199 |
+
βββ analyze/ # Text analysis endpoint
|
| 200 |
+
β βββ encore.service.ts
|
| 201 |
+
β βββ analyze.ts
|
| 202 |
+
βββ models/ # Model listing endpoint
|
| 203 |
+
β βββ encore.service.ts
|
| 204 |
+
β βββ models.ts
|
| 205 |
+
βββ health/ # Health check endpoint
|
| 206 |
+
β βββ encore.service.ts
|
| 207 |
+
β βββ health.ts
|
| 208 |
+
βββ lib/ # Shared utilities
|
| 209 |
+
βββ types.ts # TypeScript types
|
| 210 |
+
βββ cache.ts # In-memory caching
|
| 211 |
+
βββ llm-provider.ts # Provider abstraction
|
| 212 |
+
βββ ollama-client.ts # Ollama integration
|
| 213 |
+
βββ huggingface-client.ts # Hugging Face integration
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
## π― Use Cases
|
| 217 |
+
|
| 218 |
+
- π¬ **Chatbots** - Build conversational AI applications
|
| 219 |
+
- π **RAG Systems** - Create context-aware Q&A systems
|
| 220 |
+
- π **Education** - Analyze and explain complex texts
|
| 221 |
+
- π¬ **Research** - Summarize and extract key information
|
| 222 |
+
- π€ **AI Agents** - Backend for autonomous AI systems
|
| 223 |
+
- π **Content Analysis** - Evaluate and process documents
|
| 224 |
+
|
| 225 |
+
## π Deployment Options
|
| 226 |
+
|
| 227 |
+
### 1. Encore Cloud (Recommended for Production)
|
| 228 |
+
```bash
|
| 229 |
+
encore deploy
|
| 230 |
+
```
|
| 231 |
+
- Automatic scaling
|
| 232 |
+
- Built-in monitoring
|
| 233 |
+
- Type-safe service-to-service calls
|
| 234 |
+
- Zero infrastructure management
|
| 235 |
+
|
| 236 |
+
### 2. Hugging Face Spaces (Great for Demos)
|
| 237 |
+
- See [README.space.md](./README.space.md)
|
| 238 |
+
- Free hosting for public projects
|
| 239 |
+
- Easy model integration
|
| 240 |
+
- Community visibility
|
| 241 |
+
|
| 242 |
+
### 3. Docker
|
| 243 |
+
```bash
|
| 244 |
+
docker build -t llm-api .
|
| 245 |
+
docker run -p 7860:7860 \
|
| 246 |
+
-e LLMProvider=huggingface \
|
| 247 |
+
-e HuggingFaceAPIKey=your_key \
|
| 248 |
+
llm-api
|
| 249 |
+
```
|
| 250 |
+
|
| 251 |
+
### 4. Self-Hosted
|
| 252 |
+
```bash
|
| 253 |
+
npm install -g encore.dev
|
| 254 |
+
encore run --port 8080
|
| 255 |
+
```
|
| 256 |
+
|
| 257 |
+
## π Performance
|
| 258 |
+
|
| 259 |
+
- **Caching** - Reduces redundant LLM calls by up to 80%
|
| 260 |
+
- **Async/Await** - Non-blocking concurrent requests
|
| 261 |
+
- **Lightweight** - Minimal dependencies for fast startup
|
| 262 |
+
- **Efficient** - Optimized for serverless environments
|
| 263 |
+
|
| 264 |
+
**Cache Configuration:**
|
| 265 |
+
- Chat: 300s TTL, 100 max entries
|
| 266 |
+
- RAG: 600s TTL, 50 max entries
|
| 267 |
+
- Analysis: 900s TTL, 30 max entries
|
| 268 |
+
|
| 269 |
+
## π Security Best Practices
|
| 270 |
+
|
| 271 |
+
β
API keys stored as secrets, never in code
|
| 272 |
+
β
No sensitive data in logs
|
| 273 |
+
β
Type-safe request validation
|
| 274 |
+
β
Error messages don't leak internals
|
| 275 |
+
β
CORS configured for frontend integration
|
| 276 |
+
|
| 277 |
+
## π οΈ Development
|
| 278 |
+
|
| 279 |
+
```bash
|
| 280 |
+
# Install Encore
|
| 281 |
+
npm install -g encore.dev
|
| 282 |
+
|
| 283 |
+
# Run with hot reload
|
| 284 |
+
encore run
|
| 285 |
+
|
| 286 |
+
# Run tests
|
| 287 |
+
encore test
|
| 288 |
+
|
| 289 |
+
# Type check
|
| 290 |
+
encore build
|
| 291 |
+
```
|
| 292 |
+
|
| 293 |
+
## π Example: Frontend Integration
|
| 294 |
+
|
| 295 |
+
```typescript
|
| 296 |
+
// Auto-generated type-safe client
|
| 297 |
+
import backend from '~backend/client';
|
| 298 |
+
|
| 299 |
+
// Chat
|
| 300 |
+
const response = await backend.chat.chat({
|
| 301 |
+
message: "Hello!",
|
| 302 |
+
temperature: 0.7
|
| 303 |
+
});
|
| 304 |
+
|
| 305 |
+
// RAG
|
| 306 |
+
const ragResponse = await backend.rag.rag({
|
| 307 |
+
query: "What is this about?",
|
| 308 |
+
context: ["Document 1...", "Document 2..."]
|
| 309 |
+
});
|
| 310 |
+
|
| 311 |
+
// Analysis
|
| 312 |
+
const analysis = await backend.analyze.analyze({
|
| 313 |
+
text: "Long text...",
|
| 314 |
+
task: "summarize"
|
| 315 |
+
});
|
| 316 |
+
```
|
| 317 |
+
|
| 318 |
+
## π€ Contributing
|
| 319 |
+
|
| 320 |
+
Contributions welcome! This is a production-ready foundation that can be extended with:
|
| 321 |
+
|
| 322 |
+
- Additional analysis tasks
|
| 323 |
+
- Vector database integration for RAG
|
| 324 |
+
- Streaming responses
|
| 325 |
+
- Rate limiting middleware
|
| 326 |
+
- Authentication
|
| 327 |
+
- Model fine-tuning endpoints
|
| 328 |
+
|
| 329 |
+
## π License
|
| 330 |
+
|
| 331 |
+
MIT License - feel free to use in your projects!
|
| 332 |
+
|
| 333 |
+
## π Support
|
| 334 |
+
|
| 335 |
+
- [Encore Documentation](https://encore.dev/docs)
|
| 336 |
+
- [Hugging Face Spaces Docs](https://huggingface.co/docs/hub/spaces)
|
| 337 |
+
- [GitHub Issues](./issues)
|
| 338 |
+
|
| 339 |
+
---
|
| 340 |
+
|
| 341 |
+
**Built with** β€οΈ using [Encore.ts](https://encore.dev)
|