File size: 2,057 Bytes
984e1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import json
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
from huggingface_hub import hf_hub_download
from llama_cpp import Llama

app = FastAPI()

# Enable CORS for frontend integration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], 
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Download the Gemma model from Hugging Face Hub if not cached locally
MODEL_FILE = "gemma-4-E2B-it-IQ4_NL.gguf"
if not os.path.exists(MODEL_FILE):
    print("Downloading Gemma model, please wait...")
    hf_hub_download(
        repo_id="unsloth/gemma-4-E2B-it-GGUF", 
        filename=MODEL_FILE, 
        local_dir="."
    )

# Initialize local LLM instance with 2048 context length
llm = Llama(model_path=f"./{MODEL_FILE}", n_ctx=2048, n_threads=2)

@app.post("/v1/chat/completions")
async def chat_completion(request: Request):
    # Secure the endpoint using a custom API Bearer Token
    api_key = request.headers.get("Authorization")
    if api_key != f"Bearer {os.environ.get('MY_SECRET_KEY', 'default_pass')}":
        raise HTTPException(status_code=401, detail="Unauthorized access.")

    body = await request.json()
    messages = body.get("messages", [])
    
    # Format chat history into standard LLM prompt template
    prompt = ""
    for msg in messages:
        role = msg.get("role")
        content = msg.get("content")
        prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n"
    prompt += "<|im_start|>assistant\n"

    # Generate streaming response from the model
    output = llm(prompt, max_tokens=512, stream=True)

    def stream_generator():
        for chunk in output:
            token = chunk['choices'][0]['text']
            data = {"choices": [{"delta": {"content": token}, "finish_reason": None}]}
            yield f"data: {json.dumps(data)}\n\n"
        yield "data: [DONE]\n\n"

    return StreamingResponse(stream_generator(), media_type="text/event-stream")