Spaces:
Sleeping
Sleeping
File size: 10,418 Bytes
526927a |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# Lily LLM API μ°Έμ‘° λ¬Έμ
## π κ°μ
Lily LLM APIλ λ€μν μΈμ΄ λͺ¨λΈμ μ§μνλ RESTful API μλ²μ
λλ€. ν
μ€νΈ μμ±, λ©ν°λͺ¨λ¬ μ²λ¦¬, RAG(Retrieval-Augmented Generation) κΈ°λ₯μ μ 곡ν©λλ€.
## π κΈ°λ³Έ μ 보
- **Base URL**: `http://localhost:8001`
- **API λ¬Έμ**: `http://localhost:8001/docs`
- **ReDoc λ¬Έμ**: `http://localhost:8001/redoc`
## π μΈμ¦
### JWT ν ν° μΈμ¦
```bash
# λ‘κ·ΈμΈ
curl -X POST "http://localhost:8001/auth/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=your_username&password=your_password"
# μλ΅
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...",
"token_type": "bearer"
}
```
### 보νΈλ μλν¬μΈνΈ μ¬μ©
```bash
curl -X GET "http://localhost:8001/auth/me" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN"
```
## π€ AI λͺ¨λΈ κ΄λ ¨
### 1. λͺ¨λΈ λͺ©λ‘ μ‘°ν
```http
GET /models
```
**μλ΅ μμ:**
```json
{
"available_models": [
{
"model_id": "polyglot-ko-1.3b-chat",
"display_name": "Polyglot Korean 1.3B Chat",
"model_type": "text",
"description": "νκ΅μ΄ νΉν ν
μ€νΈ μμ± λͺ¨λΈ"
},
{
"model_id": "kanana-1.5-v-3b-instruct",
"display_name": "Kanana 1.5 v3B Instruct",
"model_type": "multimodal",
"description": "λ©ν°λͺ¨λ¬ μ΄λ―Έμ§+ν
μ€νΈ μ²λ¦¬ λͺ¨λΈ"
}
],
"current_model": "polyglot-ko-1.3b-chat"
}
```
### 2. ν
μ€νΈ μμ±
```http
POST /generate
```
**μμ² νλΌλ―Έν°:**
```json
{
"prompt": "μλ
νμΈμ, AIμ λν΄ μ€λͺ
ν΄μ£ΌμΈμ.",
"model_id": "polyglot-ko-1.3b-chat",
"max_length": 200,
"temperature": 0.7,
"top_p": 0.9,
"do_sample": true
}
```
**μλ΅ μμ:**
```json
{
"generated_text": "μλ
νμΈμ! AI(μΈκ³΅μ§λ₯)λ μΈκ°μ νμ΅λ₯λ ₯κ³Ό μΆλ‘ λ₯λ ₯μ μΈκ³΅μ μΌλ‘ ꡬνν μ»΄ν¨ν° μμ€ν
μ
λλ€...",
"model_name": "polyglot-ko-1.3b-chat",
"processing_time": 2.34,
"tokens_generated": 45
}
```
### 3. λ©ν°λͺ¨λ¬ μμ± (μ΄λ―Έμ§ + ν
μ€νΈ)
```http
POST /generate-multimodal
```
**μμ² (multipart/form-data):**
```
prompt: "μ΄ μ΄λ―Έμ§μ λν΄ μ€λͺ
ν΄μ£ΌμΈμ"
model_id: "kanana-1.5-v-3b-instruct"
max_length: 200
temperature: 0.7
image_files: [νμΌ1, νμΌ2, ...]
```
**μλ΅ μμ:**
```json
{
"generated_text": "μ΄ μ΄λ―Έμ§λ μλ¦λ€μ΄ μμ° νκ²½μ 보μ¬μ€λλ€...",
"model_name": "kanana-1.5-v-3b-instruct",
"processing_time": 15.67,
"images_processed": 2
}
```
## π λ¬Έμ μ²λ¦¬ (RAG)
### 1. λ¬Έμ μ
λ‘λ
```http
POST /document/upload
```
**μμ² (multipart/form-data):**
```
file: [PDF/DOC/DOCX/PPTX νμΌ]
user_id: "user123"
```
**μλ΅ μμ:**
```json
{
"document_id": "doc_123456",
"filename": "sample.pdf",
"file_type": "pdf",
"file_size": 1024000,
"pages": 15,
"chunks": 45,
"upload_time": "2025-08-04T10:30:00Z"
}
```
### 2. RAG 쿼리
```http
POST /rag/generate
```
**μμ² νλΌλ―Έν°:**
```json
{
"query": "μΈκ³΅μ§λ₯μ λ―Έλμ λν΄ μλ €μ£ΌμΈμ",
"user_id": "user123",
"max_length": 300,
"temperature": 0.7
}
```
**μλ΅ μμ:**
```json
{
"response": "μΈκ³΅μ§λ₯μ λ―Έλλ λ§€μ° λ°μ΅λλ€. νμ¬ λ¬Έμμ λ°λ₯΄λ©΄...",
"sources": [
{
"document_id": "doc_123456",
"page": 5,
"chunk": "AI κΈ°μ μ λ°μ λ°©ν₯..."
}
],
"confidence": 0.85,
"processing_time": 3.45
}
```
### 3. νμ΄λΈλ¦¬λ RAG (μ΄λ―Έμ§ + λ¬Έμ)
```http
POST /rag/generate-hybrid
```
**μμ² (multipart/form-data):**
```
query: "μ΄ μ΄λ―Έμ§μ κ΄λ ¨λ λ¬Έμ λ΄μ©μ μ°Ύμμ£ΌμΈμ"
user_id: "user123"
image_files: [μ΄λ―Έμ§ νμΌλ€]
max_length: 300
temperature: 0.7
```
## π¬ μ±ν
λ° μΈμ
κ΄λ¦¬
### 1. μ¬μ©μ μμ±
```http
POST /user/create
```
**μμ² νλΌλ―Έν°:**
```json
{
"user_id": "user123",
"username": "ν
μ€νΈμ¬μ©μ",
"email": "test@example.com"
}
```
### 2. μ±ν
μΈμ
μμ±
```http
POST /session/create
```
**μμ² νλΌλ―Έν°:**
```json
{
"user_id": "user123",
"session_name": "AI μλ΄ μΈμ
"
}
```
### 3. λ©μμ§ μΆκ°
```http
POST /chat/message
```
**μμ² νλΌλ―Έν°:**
```json
{
"session_id": "session_123",
"user_id": "user123",
"message_type": "text",
"content": "μλ
νμΈμ!"
}
```
### 4. μ±ν
κΈ°λ‘ μ‘°ν
```http
GET /chat/history/{session_id}
```
## π λ°±κ·ΈλΌμ΄λ μμ
### 1. λ¬Έμ μ²λ¦¬ μμ
```http
POST /tasks/document/process
```
**μμ² νλΌλ―Έν°:**
```json
{
"file_path": "/uploads/document.pdf",
"user_id": "user123"
}
```
**μλ΅ μμ:**
```json
{
"task_id": "task_123456",
"status": "PENDING",
"message": "λ¬Έμ μ²λ¦¬ μμ
μ΄ μμλμμ΅λλ€."
}
```
### 2. μμ
μν νμΈ
```http
GET /tasks/{task_id}
```
**μλ΅ μμ:**
```json
{
"task_id": "task_123456",
"status": "SUCCESS",
"result": {
"document_id": "doc_123456",
"chunks": 45
},
"progress": 100
}
```
## π λͺ¨λν°λ§
### 1. μ±λ₯ λͺ¨λν°λ§ μμ
```http
POST /monitoring/start
```
### 2. μ±λ₯ μν μ‘°ν
```http
GET /monitoring/status
```
**μλ΅ μμ:**
```json
{
"current_metrics": {
"cpu_percent": 25.5,
"memory_percent": 68.2,
"memory_used_mb": 8192.0,
"disk_usage_percent": 45.0
},
"performance_stats": {
"avg_response_time": 1.23,
"avg_inference_time": 2.45,
"total_requests": 1250,
"success_rate": 98.5
},
"system_health": {
"status": "healthy",
"recommendations": []
}
}
```
### 3. μμ€ν
κ±΄κ° μν
```http
GET /monitoring/health
```
## π WebSocket
### μ°κ²°
```javascript
const ws = new WebSocket('ws://localhost:8001/ws/user123');
ws.onopen = function() {
console.log('WebSocket μ°κ²°λ¨');
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('λ©μμ§ μμ :', data);
};
```
### λ©μμ§ μ μ‘
```javascript
ws.send(JSON.stringify({
type: 'chat',
message: 'μλ
νμΈμ!',
session_id: 'session_123'
}));
```
## π¨ μ€λ₯ μ½λ
| μ½λ | μλ―Έ | ν΄κ²° λ°©λ² |
|------|------|-----------|
| 400 | μλͺ»λ μμ² | μμ² νλΌλ―Έν° νμΈ |
| 401 | μΈμ¦ μ€ν¨ | ν ν° νμΈ |
| 403 | κΆν μμ | κΆν νμΈ |
| 404 | 리μμ€ μμ | URL νμΈ |
| 422 | κ²μ¦ μ€ν¨ | μμ² λ°μ΄ν° νμ νμΈ |
| 500 | μλ² μ€λ₯ | μλ² λ‘κ·Έ νμΈ |
| 503 | μλΉμ€ λΆκ° | μλΉμ€ μν νμΈ |
## π μμ μ½λ
### Python ν΄λΌμ΄μΈνΈ
```python
import requests
import json
class LilyLLMClient:
def __init__(self, base_url="http://localhost:8001"):
self.base_url = base_url
self.token = None
def login(self, username, password):
response = requests.post(f"{self.base_url}/auth/login",
data={"username": username, "password": password})
if response.status_code == 200:
self.token = response.json()["access_token"]
return True
return False
def generate_text(self, prompt, model_id="polyglot-ko-1.3b-chat"):
headers = {"Authorization": f"Bearer {self.token}"} if self.token else {}
data = {
"prompt": prompt,
"model_id": model_id,
"max_length": 200,
"temperature": 0.7
}
response = requests.post(f"{self.base_url}/generate",
data=data, headers=headers)
return response.json()
def upload_document(self, file_path, user_id):
headers = {"Authorization": f"Bearer {self.token}"} if self.token else {}
with open(file_path, 'rb') as f:
files = {'file': f}
data = {'user_id': user_id}
response = requests.post(f"{self.base_url}/document/upload",
files=files, data=data, headers=headers)
return response.json()
# μ¬μ© μμ
client = LilyLLMClient()
if client.login("username", "password"):
result = client.generate_text("μλ
νμΈμ!")
print(result["generated_text"])
```
### JavaScript ν΄λΌμ΄μΈνΈ
```javascript
class LilyLLMClient {
constructor(baseUrl = 'http://localhost:8001') {
this.baseUrl = baseUrl;
this.token = null;
}
async login(username, password) {
const response = await fetch(`${this.baseUrl}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `username=${username}&password=${password}`
});
if (response.ok) {
const data = await response.json();
this.token = data.access_token;
return true;
}
return false;
}
async generateText(prompt, modelId = 'polyglot-ko-1.3b-chat') {
const headers = this.token ?
{'Authorization': `Bearer ${this.token}`} : {};
const formData = new FormData();
formData.append('prompt', prompt);
formData.append('model_id', modelId);
formData.append('max_length', '200');
formData.append('temperature', '0.7');
const response = await fetch(`${this.baseUrl}/generate`, {
method: 'POST',
headers,
body: formData
});
return await response.json();
}
}
// μ¬μ© μμ
const client = new LilyLLMClient();
client.login('username', 'password').then(async (success) => {
if (success) {
const result = await client.generateText('μλ
νμΈμ!');
console.log(result.generated_text);
}
});
```
## π§ μ€μ
### νκ²½ λ³μ
```bash
# μλ² μ€μ
HOST=0.0.0.0
PORT=8001
LOG_LEVEL=INFO
# λ°μ΄ν°λ² μ΄μ€
DATABASE_URL=sqlite:///app/data/lily_llm.db
# Redis
REDIS_URL=redis://localhost:6379
# Celery
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0
# 보μ
SECRET_KEY=your-secret-key
JWT_SECRET_KEY=your-jwt-secret-key
```
## π μΆκ° 리μμ€
- [FastAPI λ¬Έμ](https://fastapi.tiangolo.com/)
- [Celery λ¬Έμ](https://docs.celeryproject.org/)
- [Redis λ¬Έμ](https://redis.io/documentation)
- [LangChain λ¬Έμ](https://python.langchain.com/) |