llm.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. LLM (Large Language Model) endpoints
  14. """
  15. from fastapi import APIRouter, HTTPException
  16. from loguru import logger
  17. from api.dependencies import PixelleVideoDep
  18. from api.schemas.llm import LLMChatRequest, LLMChatResponse
  19. router = APIRouter(prefix="/llm", tags=["Basic Services"])
  20. @router.post("/chat", response_model=LLMChatResponse)
  21. async def llm_chat(
  22. request: LLMChatRequest,
  23. pixelle_video: PixelleVideoDep
  24. ):
  25. """
  26. LLM chat endpoint
  27. Generate text response using configured LLM.
  28. - **prompt**: User prompt/question
  29. - **temperature**: Creativity level (0.0-2.0, lower = more deterministic)
  30. - **max_tokens**: Maximum response length
  31. Returns generated text response.
  32. """
  33. try:
  34. logger.info(f"LLM chat request: {request.prompt[:50]}...")
  35. # Call LLM service
  36. response = await pixelle_video.llm(
  37. prompt=request.prompt,
  38. temperature=request.temperature,
  39. max_tokens=request.max_tokens
  40. )
  41. return LLMChatResponse(
  42. content=response,
  43. tokens_used=None # Can add token counting if needed
  44. )
  45. except Exception as e:
  46. logger.error(f"LLM chat error: {e}")
  47. raise HTTPException(status_code=500, detail=str(e))