Spaces:
Sleeping
Sleeping
| # 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/) |