| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | LLM (Large Language Model) endpoints |
| 15 | """ |
| 16 | |
| 17 | from fastapi import APIRouter, HTTPException |
| 18 | from loguru import logger |
| 19 | |
| 20 | from api.dependencies import PixelleVideoDep |
| 21 | from api.schemas.llm import LLMChatRequest, LLMChatResponse |
| 22 | |
| 23 | router = APIRouter(prefix="/llm", tags=["Basic Services"]) |
| 24 | |
| 25 | |
| 26 | @router.post("/chat", response_model=LLMChatResponse) |
| 27 | async def llm_chat( |
| 28 | request: LLMChatRequest, |
| 29 | pixelle_video: PixelleVideoDep |
| 30 | ): |
| 31 | """ |
| 32 | LLM chat endpoint |
| 33 | |
| 34 | Generate text response using configured LLM. |
| 35 | |
| 36 | - **prompt**: User prompt/question |
| 37 | - **temperature**: Creativity level (0.0-2.0, lower = more deterministic) |
| 38 | - **max_tokens**: Maximum response length |
| 39 | |
| 40 | Returns generated text response. |
| 41 | """ |
| 42 | try: |
| 43 | logger.info(f"LLM chat request: {request.prompt[:50]}...") |
| 44 | |
| 45 | # Call LLM service |
| 46 | response = await pixelle_video.llm( |
| 47 | prompt=request.prompt, |
| 48 | temperature=request.temperature, |
| 49 | max_tokens=request.max_tokens |
| 50 | ) |
| 51 | |
| 52 | return LLMChatResponse( |
| 53 | content=response, |
| 54 | tokens_used=None # Can add token counting if needed |
| 55 | ) |
| 56 | |
| 57 | except Exception as e: |
| 58 | logger.error(f"LLM chat error: {e}") |
| 59 | raise HTTPException(status_code=500, detail=str(e)) |
| 60 | |
| 61 |