|
1 | 1 | import json |
| 2 | +import structlog |
2 | 3 | from typing import Optional |
3 | 4 |
|
4 | | -from fastapi import Request |
| 5 | +from fastapi import Request, HTTPException |
5 | 6 |
|
6 | 7 | from codegate.pipeline.base import SequentialPipelineProcessor |
7 | 8 | from codegate.pipeline.output import OutputPipelineProcessor |
|
10 | 11 | from codegate.providers.llamacpp.normalizer import LLamaCppInputNormalizer, LLamaCppOutputNormalizer |
11 | 12 |
|
12 | 13 |
|
13 | | -class LlamaCppProvider(BaseProvider): |
| 14 | +class LlamaCppProvider(BaseProvider): |
14 | 15 | def __init__( |
15 | 16 | self, |
16 | 17 | pipeline_processor: Optional[SequentialPipelineProcessor] = None, |
@@ -46,7 +47,24 @@ async def create_completion( |
46 | 47 | ): |
47 | 48 | body = await request.body() |
48 | 49 | data = json.loads(body) |
| 50 | + logger = structlog.get_logger("codegate") |
49 | 51 |
|
50 | 52 | is_fim_request = self._is_fim_request(request, data) |
51 | | - stream = await self.complete(data, None, is_fim_request=is_fim_request) |
| 53 | + try: |
| 54 | + stream = await self.complete(data, None, is_fim_request=is_fim_request) |
| 55 | + except RuntimeError as e: |
| 56 | + # propagate as error 500 |
| 57 | + logger.error("Error in LlamaCppProvider completion", error=str(e)) |
| 58 | + raise HTTPException(status_code=500, detail=str(e)) |
| 59 | + except ValueError as e: |
| 60 | + # capture well known exceptions |
| 61 | + logger.error("Error in LlamaCppProvider completion", error=str(e)) |
| 62 | + if str(e).startswith("Model path does not exist") or \ |
| 63 | + str(e).startswith("No file found"): |
| 64 | + raise HTTPException(status_code=404, detail=str(e)) |
| 65 | + elif "exceed" in str(e): |
| 66 | + raise HTTPException(status_code=429, detail=str(e)) |
| 67 | + else: |
| 68 | + # just continue raising the exception |
| 69 | + raise e |
52 | 70 | return self._completion_handler.create_response(stream) |
0 commit comments