File size: 18,083 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# Lily LLM API μ‚¬μš©μž κ°€μ΄λ“œ

## πŸ“‹ λͺ©μ°¨

1. [μ‹œμž‘ν•˜κΈ°](#μ‹œμž‘ν•˜κΈ°)
2. [κΈ°λ³Έ κΈ°λŠ₯](#κΈ°λ³Έ-κΈ°λŠ₯)
3. [κ³ κΈ‰ κΈ°λŠ₯](#κ³ κΈ‰-κΈ°λŠ₯)
4. [문제 ν•΄κ²°](#문제-ν•΄κ²°)
5. [λͺ¨λ²” 사둀](#λͺ¨λ²”-사둀)

## πŸš€ μ‹œμž‘ν•˜κΈ°

### μ‹œμŠ€ν…œ μš”κ΅¬μ‚¬ν•­

- **μ΅œμ†Œ 사양**:
  - CPU: 4μ½”μ–΄ 이상
  - RAM: 8GB 이상
  - μ €μž₯곡간: 20GB 이상
  - GPU: 선택사항 (CUDA 지원 μ‹œ μ„±λŠ₯ ν–₯상)

- **ꢌμž₯ 사양**:
  - CPU: 8μ½”μ–΄ 이상
  - RAM: 16GB 이상
  - μ €μž₯곡간: 50GB 이상
  - GPU: NVIDIA RTX 3060 이상 (CUDA 지원)

### μ„€μΉ˜ 및 μ‹€ν–‰

#### 1. Dockerλ₯Ό μ‚¬μš©ν•œ 배포 (ꢌμž₯)

```bash
# μ €μž₯μ†Œ 클둠
git clone <repository-url>
cd lily_generate_package

# 배포 μ‹€ν–‰
chmod +x scripts/deploy.sh
./scripts/deploy.sh deploy

# μƒνƒœ 확인
./scripts/deploy.sh status
```

#### 2. 둜컬 개발 ν™˜κ²½

```bash
# κ°€μƒν™˜κ²½ 생성
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# μ˜μ‘΄μ„± μ„€μΉ˜
pip install -r requirements.txt

# NLTK 데이터 λ‹€μš΄λ‘œλ“œ
python -c "import nltk; nltk.download('punkt'); nltk.download('punkt_tab')"

# μ„œλ²„ μ‹€ν–‰
python run_server_v2.py
```

### 첫 번째 μš”μ²­

```bash
# μ„œλ²„ μƒνƒœ 확인
curl http://localhost:8001/health

# λͺ¨λΈ λͺ©λ‘ 쑰회
curl http://localhost:8001/models

# κ°„λ‹¨ν•œ ν…μŠ€νŠΈ 생성
curl -X POST http://localhost:8001/generate \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "prompt=μ•ˆλ…•ν•˜μ„Έμš”!&model_id=polyglot-ko-1.3b-chat&max_length=100"
```

## πŸ€– κΈ°λ³Έ κΈ°λŠ₯

### 1. ν…μŠ€νŠΈ 생성

#### λ‹¨μˆœ ν…μŠ€νŠΈ 생성

```python
import requests

def generate_text(prompt, model_id="polyglot-ko-1.3b-chat"):
    url = "http://localhost:8001/generate"
    data = {
        "prompt": prompt,
        "model_id": model_id,
        "max_length": 200,
        "temperature": 0.7,
        "top_p": 0.9,
        "do_sample": True
    }
    
    response = requests.post(url, data=data)
    return response.json()

# μ‚¬μš© 예제
result = generate_text("인곡지λŠ₯의 λ―Έλž˜μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”.")
print(result["generated_text"])
```

#### νŒŒλΌλ―Έν„° μ„€λͺ…

| νŒŒλΌλ―Έν„° | μ„€λͺ… | κΈ°λ³Έκ°’ | λ²”μœ„ |
|----------|------|--------|------|
| `prompt` | μž…λ ₯ ν…μŠ€νŠΈ | ν•„μˆ˜ | - |
| `model_id` | μ‚¬μš©ν•  λͺ¨λΈ | polyglot-ko-1.3b-chat | μ‚¬μš© κ°€λŠ₯ν•œ λͺ¨λΈ λͺ©λ‘ |
| `max_length` | μ΅œλŒ€ 토큰 수 | 200 | 1-4000 |
| `temperature` | μ°½μ˜μ„± 쑰절 | 0.7 | 0.0-2.0 |
| `top_p` | λˆ„μ  ν™•λ₯  μž„κ³„κ°’ | 0.9 | 0.0-1.0 |
| `do_sample` | μƒ˜ν”Œλ§ μ‚¬μš© μ—¬λΆ€ | True | True/False |

### 2. λ©€ν‹°λͺ¨λ‹¬ 처리

#### 이미지와 ν…μŠ€νŠΈ ν•¨κ»˜ 처리

```python
def generate_multimodal(prompt, image_files, model_id="kanana-1.5-v-3b-instruct"):
    url = "http://localhost:8001/generate-multimodal"
    
    files = []
    for i, image_file in enumerate(image_files):
        files.append(('image_files', (f'image_{i}.jpg', open(image_file, 'rb'), 'image/jpeg')))
    
    data = {
        "prompt": prompt,
        "model_id": model_id,
        "max_length": 200,
        "temperature": 0.7
    }
    
    response = requests.post(url, files=files, data=data)
    return response.json()

# μ‚¬μš© 예제
result = generate_multimodal(
    "이 이미지에 λŒ€ν•΄ μ„€λͺ…ν•΄μ£Όμ„Έμš”.",
    ["image1.jpg", "image2.jpg"]
)
print(result["generated_text"])
```

### 3. μ‚¬μš©μž 관리

#### μ‚¬μš©μž 등둝 및 둜그인

```python
def register_user(username, email, password):
    url = "http://localhost:8001/auth/register"
    data = {
        "username": username,
        "email": email,
        "password": password
    }
    
    response = requests.post(url, data=data)
    return response.json()

def login_user(username, password):
    url = "http://localhost:8001/auth/login"
    data = {
        "username": username,
        "password": password
    }
    
    response = requests.post(url, data=data)
    return response.json()

# μ‚¬μš© 예제
# 1. μ‚¬μš©μž 등둝
register_result = register_user("testuser", "test@example.com", "password123")
access_token = register_result["access_token"]

# 2. 둜그인
login_result = login_user("testuser", "password123")
access_token = login_result["access_token"]
```

#### 인증이 ν•„μš”ν•œ μš”μ²­

```python
def authenticated_request(url, data, token):
    headers = {"Authorization": f"Bearer {token}"}
    response = requests.post(url, data=data, headers=headers)
    return response.json()

# μ‚¬μš© 예제
result = authenticated_request(
    "http://localhost:8001/generate",
    {"prompt": "μ•ˆλ…•ν•˜μ„Έμš”!", "model_id": "polyglot-ko-1.3b-chat"},
    access_token
)
```

## πŸ“„ κ³ κΈ‰ κΈ°λŠ₯

### 1. λ¬Έμ„œ 처리 (RAG)

#### λ¬Έμ„œ μ—…λ‘œλ“œ

```python
def upload_document(file_path, user_id, token=None):
    url = "http://localhost:8001/document/upload"
    
    with open(file_path, 'rb') as f:
        files = {'file': f}
        data = {'user_id': user_id}
        headers = {"Authorization": f"Bearer {token}"} if token else {}
        
        response = requests.post(url, files=files, data=data, headers=headers)
        return response.json()

# μ‚¬μš© 예제
result = upload_document("document.pdf", "user123", access_token)
document_id = result["document_id"]
```

#### RAG 쿼리

```python
def rag_query(query, user_id, token=None):
    url = "http://localhost:8001/rag/generate"
    
    data = {
        "query": query,
        "user_id": user_id,
        "max_length": 300,
        "temperature": 0.7
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.post(url, data=data, headers=headers)
    return response.json()

# μ‚¬μš© 예제
result = rag_query("인곡지λŠ₯의 λ―Έλž˜μ— λŒ€ν•΄ μ•Œλ €μ£Όμ„Έμš”.", "user123", access_token)
print(result["response"])
print("좜처:", result["sources"])
```

#### ν•˜μ΄λΈŒλ¦¬λ“œ RAG (이미지 + λ¬Έμ„œ)

```python
def hybrid_rag_query(query, image_files, user_id, token=None):
    url = "http://localhost:8001/rag/generate-hybrid"
    
    files = []
    for i, image_file in enumerate(image_files):
        files.append(('image_files', (f'image_{i}.jpg', open(image_file, 'rb'), 'image/jpeg')))
    
    data = {
        "query": query,
        "user_id": user_id,
        "max_length": 300,
        "temperature": 0.7
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.post(url, files=files, data=data, headers=headers)
    return response.json()
```

### 2. μ±„νŒ… μ„Έμ…˜ 관리

#### μ„Έμ…˜ 생성 및 λ©”μ‹œμ§€ 관리

```python
def create_chat_session(user_id, session_name, token=None):
    url = "http://localhost:8001/session/create"
    
    data = {
        "user_id": user_id,
        "session_name": session_name
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.post(url, data=data, headers=headers)
    return response.json()

def add_chat_message(session_id, user_id, content, token=None):
    url = "http://localhost:8001/chat/message"
    
    data = {
        "session_id": session_id,
        "user_id": user_id,
        "message_type": "text",
        "content": content
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.post(url, data=data, headers=headers)
    return response.json()

def get_chat_history(session_id, token=None):
    url = f"http://localhost:8001/chat/history/{session_id}"
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.get(url, headers=headers)
    return response.json()

# μ‚¬μš© 예제
# 1. μ„Έμ…˜ 생성
session_result = create_chat_session("user123", "AI 상담", access_token)
session_id = session_result["session_id"]

# 2. λ©”μ‹œμ§€ μΆ”κ°€
add_chat_message(session_id, "user123", "μ•ˆλ…•ν•˜μ„Έμš”!", access_token)

# 3. μ±„νŒ… 기둝 쑰회
history = get_chat_history(session_id, access_token)
for message in history:
    print(f"{message['timestamp']}: {message['content']}")
```

### 3. λ°±κ·ΈλΌμš΄λ“œ μž‘μ—…

#### λ¬Έμ„œ 처리 μž‘μ—…

```python
def start_document_processing(file_path, user_id, token=None):
    url = "http://localhost:8001/tasks/document/process"
    
    data = {
        "file_path": file_path,
        "user_id": user_id
    }
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.post(url, data=data, headers=headers)
    return response.json()

def check_task_status(task_id, token=None):
    url = f"http://localhost:8001/tasks/{task_id}"
    headers = {"Authorization": f"Bearer {token}"} if token else {}
    
    response = requests.get(url, headers=headers)
    return response.json()

# μ‚¬μš© 예제
# 1. μž‘μ—… μ‹œμž‘
task_result = start_document_processing("/path/to/document.pdf", "user123", access_token)
task_id = task_result["task_id"]

# 2. μž‘μ—… μƒνƒœ 확인
import time
while True:
    status = check_task_status(task_id, access_token)
    print(f"μƒνƒœ: {status['status']}, μ§„ν–‰λ₯ : {status.get('progress', 0)}%")
    
    if status['status'] in ['SUCCESS', 'FAILURE']:
        break
    
    time.sleep(5)
```

### 4. λͺ¨λ‹ˆν„°λ§

#### μ„±λŠ₯ λͺ¨λ‹ˆν„°λ§

```python
def start_monitoring():
    url = "http://localhost:8001/monitoring/start"
    response = requests.post(url)
    return response.json()

def get_monitoring_status():
    url = "http://localhost:8001/monitoring/status"
    response = requests.get(url)
    return response.json()

def get_system_health():
    url = "http://localhost:8001/monitoring/health"
    response = requests.get(url)
    return response.json()

# μ‚¬μš© 예제
# 1. λͺ¨λ‹ˆν„°λ§ μ‹œμž‘
start_monitoring()

# 2. μƒνƒœ 확인
status = get_monitoring_status()
print(f"CPU μ‚¬μš©λ₯ : {status['current_metrics']['cpu_percent']}%")
print(f"λ©”λͺ¨λ¦¬ μ‚¬μš©λ₯ : {status['current_metrics']['memory_percent']}%")

# 3. μ‹œμŠ€ν…œ 건강 μƒνƒœ
health = get_system_health()
print(f"μ‹œμŠ€ν…œ μƒνƒœ: {health['status']}")
for recommendation in health['recommendations']:
    print(f"ꢌμž₯사항: {recommendation}")
```

## πŸ”Œ WebSocket μ‹€μ‹œκ°„ μ±„νŒ…

### WebSocket ν΄λΌμ΄μ–ΈνŠΈ

```javascript
class LilyLLMWebSocket {
    constructor(userId) {
        this.userId = userId;
        this.ws = null;
        this.messageHandlers = [];
    }
    
    connect() {
        this.ws = new WebSocket(`ws://localhost:8001/ws/${this.userId}`);
        
        this.ws.onopen = () => {
            console.log('WebSocket 연결됨');
        };
        
        this.ws.onmessage = (event) => {
            const data = JSON.parse(event.data);
            this.handleMessage(data);
        };
        
        this.ws.onclose = () => {
            console.log('WebSocket μ—°κ²° μ’…λ£Œ');
        };
        
        this.ws.onerror = (error) => {
            console.error('WebSocket 였λ₯˜:', error);
        };
    }
    
    sendMessage(message, sessionId) {
        if (this.ws && this.ws.readyState === WebSocket.OPEN) {
            this.ws.send(JSON.stringify({
                type: 'chat',
                message: message,
                session_id: sessionId
            }));
        }
    }
    
    addMessageHandler(handler) {
        this.messageHandlers.push(handler);
    }
    
    handleMessage(data) {
        this.messageHandlers.forEach(handler => handler(data));
    }
    
    disconnect() {
        if (this.ws) {
            this.ws.close();
        }
    }
}

// μ‚¬μš© 예제
const wsClient = new LilyLLMWebSocket('user123');
wsClient.connect();

wsClient.addMessageHandler((data) => {
    console.log('λ©”μ‹œμ§€ μˆ˜μ‹ :', data);
});

wsClient.sendMessage('μ•ˆλ…•ν•˜μ„Έμš”!', 'session123');
```

## 🚨 문제 ν•΄κ²°

### 일반적인 λ¬Έμ œλ“€

#### 1. μ„œλ²„ μ—°κ²° μ‹€νŒ¨

**증상**: `Connection refused` λ˜λŠ” `Failed to establish a new connection`

**ν•΄κ²° 방법**:
```bash
# μ„œλ²„ μƒνƒœ 확인
curl http://localhost:8001/health

# μ„œλ²„ μž¬μ‹œμž‘
./scripts/deploy.sh restart

# 둜그 확인
./scripts/deploy.sh logs
```

#### 2. λ©”λͺ¨λ¦¬ λΆ€μ‘±

**증상**: `Out of memory` λ˜λŠ” 응닡 속도 μ €ν•˜

**ν•΄κ²° 방법**:
```bash
# λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰ 확인
docker stats

# λΆˆν•„μš”ν•œ μ»¨ν…Œμ΄λ„ˆ 정리
docker system prune -f

# λ¦¬μ†ŒμŠ€ μ œν•œ μ„€μ • (docker-compose.yml)
services:
  lily-llm-api:
    deploy:
      resources:
        limits:
          memory: 4G
```

#### 3. λͺ¨λΈ λ‘œλ”© μ‹€νŒ¨

**증상**: `Model not found` λ˜λŠ” λͺ¨λΈ κ΄€λ ¨ 였λ₯˜

**ν•΄κ²° 방법**:
```bash
# λͺ¨λΈ λͺ©λ‘ 확인
curl http://localhost:8001/models

# λͺ¨λΈ 파일 확인
ls -la models/

# μ„œλ²„ μž¬μ‹œμž‘
./scripts/deploy.sh restart
```

#### 4. 인증 였λ₯˜

**증상**: `401 Unauthorized` λ˜λŠ” `403 Forbidden`

**ν•΄κ²° 방법**:
```python
# 토큰 κ°±μ‹ 
def refresh_token(refresh_token):
    url = "http://localhost:8001/auth/refresh"
    data = {"refresh_token": refresh_token}
    response = requests.post(url, data=data)
    return response.json()

# μƒˆλ‘œμš΄ ν† ν°μœΌλ‘œ μš”μ²­
new_tokens = refresh_token(old_refresh_token)
access_token = new_tokens["access_token"]
```

### μ„±λŠ₯ μ΅œμ ν™”

#### 1. 배치 처리

```python
def batch_generate_texts(prompts, model_id="polyglot-ko-1.3b-chat"):
    results = []
    for prompt in prompts:
        result = generate_text(prompt, model_id)
        results.append(result)
    return results

# μ‚¬μš© 예제
prompts = [
    "첫 번째 μ§ˆλ¬Έμž…λ‹ˆλ‹€.",
    "두 번째 μ§ˆλ¬Έμž…λ‹ˆλ‹€.",
    "μ„Έ 번째 μ§ˆλ¬Έμž…λ‹ˆλ‹€."
]
results = batch_generate_texts(prompts)
```

#### 2. 캐싱 ν™œμš©

```python
import redis
import json

class CachedLilyLLMClient:
    def __init__(self, base_url="http://localhost:8001"):
        self.base_url = base_url
        self.redis_client = redis.Redis(host='localhost', port=6379, db=0)
    
    def generate_text_with_cache(self, prompt, model_id="polyglot-ko-1.3b-chat"):
        # μΊμ‹œ ν‚€ 생성
        cache_key = f"text_gen:{hash(prompt + model_id)}"
        
        # μΊμ‹œμ—μ„œ 확인
        cached_result = self.redis_client.get(cache_key)
        if cached_result:
            return json.loads(cached_result)
        
        # API 호좜
        result = generate_text(prompt, model_id)
        
        # μΊμ‹œμ— μ €μž₯ (1μ‹œκ°„)
        self.redis_client.setex(cache_key, 3600, json.dumps(result))
        
        return result
```

## πŸ“š λͺ¨λ²” 사둀

### 1. μ—λŸ¬ 처리

```python
import requests
from requests.exceptions import RequestException

def safe_api_call(func, *args, **kwargs):
    try:
        return func(*args, **kwargs)
    except RequestException as e:
        print(f"λ„€νŠΈμ›Œν¬ 였λ₯˜: {e}")
        return None
    except Exception as e:
        print(f"μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜: {e}")
        return None

# μ‚¬μš© 예제
result = safe_api_call(generate_text, "μ•ˆλ…•ν•˜μ„Έμš”!")
if result:
    print(result["generated_text"])
```

### 2. μž¬μ‹œλ„ 둜직

```python
import time
from functools import wraps

def retry_on_failure(max_retries=3, delay=1):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            for attempt in range(max_retries):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    if attempt == max_retries - 1:
                        raise e
                    print(f"μ‹œλ„ {attempt + 1} μ‹€νŒ¨, {delay}초 ν›„ μž¬μ‹œλ„...")
                    time.sleep(delay)
            return None
        return wrapper
    return decorator

# μ‚¬μš© 예제
@retry_on_failure(max_retries=3, delay=2)
def robust_generate_text(prompt):
    return generate_text(prompt)
```

### 3. 비동기 처리

```python
import asyncio
import aiohttp

async def async_generate_text(session, prompt, model_id="polyglot-ko-1.3b-chat"):
    url = "http://localhost:8001/generate"
    data = {
        "prompt": prompt,
        "model_id": model_id,
        "max_length": 200,
        "temperature": 0.7
    }
    
    async with session.post(url, data=data) as response:
        return await response.json()

async def batch_generate_async(prompts):
    async with aiohttp.ClientSession() as session:
        tasks = [async_generate_text(session, prompt) for prompt in prompts]
        results = await asyncio.gather(*tasks)
        return results

# μ‚¬μš© 예제
prompts = ["질문1", "질문2", "질문3"]
results = asyncio.run(batch_generate_async(prompts))
```

### 4. λ‘œκΉ…

```python
import logging

# λ‘œκΉ… μ„€μ •
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('lily_llm_client.log'),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger(__name__)

def generate_text_with_logging(prompt, model_id="polyglot-ko-1.3b-chat"):
    logger.info(f"ν…μŠ€νŠΈ 생성 μ‹œμž‘: {prompt[:50]}...")
    
    try:
        result = generate_text(prompt, model_id)
        logger.info(f"ν…μŠ€νŠΈ 생성 성곡: {len(result['generated_text'])} 문자")
        return result
    except Exception as e:
        logger.error(f"ν…μŠ€νŠΈ 생성 μ‹€νŒ¨: {e}")
        raise
```

## πŸ“ž 지원

### 도움말 λ¦¬μ†ŒμŠ€

- **API λ¬Έμ„œ**: `http://localhost:8001/docs`
- **ReDoc λ¬Έμ„œ**: `http://localhost:8001/redoc`
- **GitHub Issues**: ν”„λ‘œμ νŠΈ μ €μž₯μ†Œμ˜ Issues μ„Ήμ…˜
- **둜그 파일**: `./logs/` 디렉토리

### 디버깅 팁

1. **둜그 확인**: 항상 둜그λ₯Ό λ¨Όμ € ν™•μΈν•˜μ„Έμš”
2. **단계별 ν…ŒμŠ€νŠΈ**: λ³΅μž‘ν•œ μš”μ²­μ„ μž‘μ€ λ‹¨μœ„λ‘œ λ‚˜λˆ„μ–΄ ν…ŒμŠ€νŠΈν•˜μ„Έμš”
3. **λ„€νŠΈμ›Œν¬ 확인**: λ°©ν™”λ²½μ΄λ‚˜ ν”„λ‘μ‹œ 섀정을 ν™•μΈν•˜μ„Έμš”
4. **λ¦¬μ†ŒμŠ€ λͺ¨λ‹ˆν„°λ§**: CPU, λ©”λͺ¨λ¦¬, λ””μŠ€ν¬ μ‚¬μš©λŸ‰μ„ 주기적으둜 ν™•μΈν•˜μ„Έμš”

### μ„±λŠ₯ 팁

1. **μ μ ˆν•œ λͺ¨λΈ 선택**: μž‘μ—…μ— λ§žλŠ” λͺ¨λΈμ„ μ„ νƒν•˜μ„Έμš”
2. **배치 처리**: μ—¬λŸ¬ μš”μ²­μ„ ν•œ λ²ˆμ— μ²˜λ¦¬ν•˜μ„Έμš”
3. **캐싱 ν™œμš©**: λ°˜λ³΅λ˜λŠ” μš”μ²­μ€ μΊμ‹œλ₯Ό μ‚¬μš©ν•˜μ„Έμš”
4. **비동기 처리**: λŒ€λŸ‰μ˜ μš”μ²­μ€ λΉ„λ™κΈ°λ‘œ μ²˜λ¦¬ν•˜μ„Έμš”