File size: 1,496 Bytes
07a91a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""FastAPI entrypoint for coding model."""

from fastapi import Depends, FastAPI, Request

from api.security import check_rate_limit, verify_api_key
from src.pipeline import CodingLLMPipeline
from src.schemas import GenerateRequest, GenerateResponse

app = FastAPI(title="Advanced Coding LLM API", version="1.0.0")
pipeline = CodingLLMPipeline()


@app.get("/health")
def health():
    model_name = pipeline.model_bundle.active_model_name if pipeline.model_bundle else "not_loaded_yet"
    return {"status": "ok", "model": model_name}


@app.post("/generate", response_model=GenerateResponse)
def generate(

    payload: GenerateRequest,

    request: Request,

    _: None = Depends(verify_api_key),

):
    client_host = request.client.host if request.client else "unknown"
    check_rate_limit(client_host)
    try:
        result = pipeline.run(payload.instruction, payload.input)
        return GenerateResponse(**result)
    except Exception as exc:
        # Keep response shape consistent with GenerateResponse model.
        return GenerateResponse(
            code="",
            explanation=(
                "Generation failed.\n\n"
                f"Error: {exc}\n"
                "Hint: Check model name/access and internet connection for first model download."
            ),
            confidence=0.0,
            important_tokens=[],
            relevancy_score=0.0,
            hallucination=True,
            latency_ms=0,
        )