File size: 6,748 Bytes
5402c84
5a69fb4
5402c84
 
2d8f511
5402c84
 
5a69fb4
 
 
5402c84
 
 
 
5a69fb4
4819b01
5402c84
 
 
 
 
 
 
 
 
 
5a69fb4
4819b01
5402c84
 
2d8f511
 
 
 
 
 
 
 
 
 
 
5402c84
 
4819b01
5402c84
4819b01
 
 
 
5402c84
 
4819b01
 
 
 
 
5a69fb4
5402c84
 
2d8f511
 
 
 
 
 
 
4819b01
2d8f511
 
 
 
 
 
4819b01
2d8f511
4819b01
 
2d8f511
4819b01
 
 
2d8f511
 
 
 
 
 
 
 
 
 
 
 
4819b01
 
2d8f511
 
4819b01
5402c84
 
 
2d8f511
5402c84
 
 
 
4819b01
5a69fb4
5402c84
 
4819b01
 
 
 
5402c84
2d8f511
 
 
 
 
 
 
 
5402c84
4819b01
 
 
 
2d8f511
4819b01
 
 
 
 
 
2d8f511
4819b01
 
2d8f511
4819b01
 
 
 
 
 
 
 
 
 
 
 
2d8f511
4819b01
 
 
 
 
 
 
2d8f511
4819b01
 
 
2d8f511
 
4819b01
 
 
 
 
 
 
 
 
 
 
 
2d8f511
4819b01
2d8f511
4819b01
 
 
 
 
 
 
2d8f511
4819b01
 
5402c84
2d8f511
 
 
 
 
5402c84
4819b01
5402c84
2d8f511
4819b01
 
 
 
2d8f511
4819b01
5402c84
2d8f511
5402c84
2d8f511
 
 
5402c84
edbc4d0
 
 
4819b01
edbc4d0
2d8f511
 
 
 
edbc4d0
4819b01
2d8f511
edbc4d0
 
 
 
 
 
 
2d8f511
edbc4d0
2d8f511
edbc4d0
2d8f511
edbc4d0
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import os
import json
import requests
from fastapi import FastAPI, Header, HTTPException
from fastapi.responses import StreamingResponse, PlainTextResponse, Response
from pydantic import BaseModel
from typing import List, Optional, Union
from dotenv import load_dotenv

load_dotenv()

app = FastAPI()

GEMMA_API_KEY = os.getenv("GEMMA_API_KEY")
APP_API_KEY = os.getenv("APP_API_KEY")
GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta"


class Message(BaseModel):
    role: str
    content: Union[str, List[dict]]


class ChatRequest(BaseModel):
    model: str
    messages: List[Message]
    stream: Optional[bool] = False
    plain: Optional[bool] = False


class UTF8JSONResponse(Response):
    media_type = "application/json; charset=utf-8"

    def render(self, content) -> bytes:
        return json.dumps(
            content,
            ensure_ascii=False,
            separators=(",", ":"),
        ).encode("utf-8")


def extract_text(messages):
    text = ""

    for msg in messages:
        content = msg.content

        if isinstance(content, list):
            for item in content:
                if item.get("type") == "text":
                    text += item.get("text", "") + "\n"
                else:
                    continue
        elif isinstance(content, str):
            text += content + "\n"

    return text.strip()


def fix_mojibake(text: str) -> str:
    """
    Repairs common UTF-8-as-latin1 mojibake such as:
    It’s -> It’s
    """
    if not isinstance(text, str):
        return text

    suspicious = ("’", "“", "”", "‘", "–", "—", "Ã")
    if any(s in text for s in suspicious):
        try:
            return text.encode("latin1").decode("utf-8")
        except UnicodeError:
            return text

    return text


def gemini_generate_url(model_name: str) -> str:
    return f"{GEMINI_BASE_URL}/models/{model_name}:generateContent?key={GEMMA_API_KEY}"


def gemini_stream_url(model_name: str) -> str:
    return f"{GEMINI_BASE_URL}/models/{model_name}:streamGenerateContent?alt=sse&key={GEMMA_API_KEY}"


def extract_gemini_text(payload: dict) -> str:
    candidates = payload.get("candidates") or []
    if not candidates:
        return ""

    content = candidates[0].get("content") or {}
    parts = content.get("parts") or []
    if not parts:
        return ""

    return parts[0].get("text", "") or ""


@app.post("/v1/chat/completions")
def chat_completions(
    request: ChatRequest,
    authorization: Optional[str] = Header(None),
):
    if not authorization:
        raise HTTPException(status_code=401, detail="Missing Authorization header")

    token = authorization.replace("Bearer ", "").strip()
    if token != APP_API_KEY:
        raise HTTPException(status_code=403, detail="Invalid API key")

    if not GEMMA_API_KEY:
        raise HTTPException(status_code=500, detail="GEMMA_API_KEY is not set")

    model_name = request.model or "gemma-3-27b-it"
    prompt = extract_text(request.messages)

    payload = {
        "contents": [
            {
                "parts": [{"text": prompt}]
            }
        ]
    }

    # -------- STREAM MODE --------
    if request.stream:
        def generate():
            try:
                url = gemini_stream_url(model_name)

                with requests.post(
                    url,
                    json=payload,
                    stream=True,
                    timeout=120,
                    headers={"Content-Type": "application/json"},
                ) as res:
                    res.raise_for_status()
                    res.encoding = "utf-8"

                    sent_role = False

                    for raw_line in res.iter_lines(decode_unicode=True):
                        if not raw_line:
                            continue

                        line = raw_line.strip()

                        if line.startswith("data:"):
                            line = line[5:].strip()

                        if not line or line == "[DONE]":
                            continue

                        try:
                            chunk_json = json.loads(line)
                        except json.JSONDecodeError:
                            continue

                        text = extract_gemini_text(chunk_json)
                        if not text:
                            continue

                        text = fix_mojibake(text)

                        delta = {"content": text}
                        if not sent_role:
                            delta["role"] = "assistant"
                            sent_role = True

                        openai_chunk = {
                            "id": "chatcmpl-gemma",
                            "object": "chat.completion.chunk",
                            "choices": [
                                {
                                    "index": 0,
                                    "delta": delta,
                                    "finish_reason": None,
                                }
                            ],
                        }

                        yield f"data: {json.dumps(openai_chunk, ensure_ascii=False)}\n\n"

                    yield "data: [DONE]\n\n"

            except Exception as e:
                error_chunk = {"error": str(e)}
                yield f"data: {json.dumps(error_chunk, ensure_ascii=False)}\n\n"
                yield "data: [DONE]\n\n"

        return StreamingResponse(
            generate(),
            media_type="text/event-stream; charset=utf-8",
            headers={"Cache-Control": "no-cache"},
        )

    # -------- NON-STREAM --------
    try:
        url = gemini_generate_url(model_name)
        res = requests.post(
            url,
            json=payload,
            timeout=120,
            headers={"Content-Type": "application/json"},
        )
        res.raise_for_status()
        res.encoding = "utf-8"

        data = res.json()
        output = extract_gemini_text(data)
        output = fix_mojibake(output)

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

    # -------- PLAIN TEXT --------
    if request.plain:
        return PlainTextResponse(
            output,
            media_type="text/plain; charset=utf-8",
        )

    # -------- OPENAI JSON --------
    return UTF8JSONResponse({
        "id": "chatcmpl-gemma",
        "object": "chat.completion",
        "choices": [
            {
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": output,
                },
                "finish_reason": "stop",
            }
        ],
    })