Update main.py
Browse files
main.py
CHANGED
|
@@ -2,15 +2,15 @@ from fastapi import FastAPI, Header, HTTPException
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Optional
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
llm = Llama.from_pretrained(
|
| 10 |
-
repo_id="
|
| 11 |
-
filename="gemma-4-
|
| 12 |
-
n_ctx=
|
| 13 |
-
n_threads=
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
class Message(BaseModel):
|
|
@@ -20,7 +20,7 @@ class Message(BaseModel):
|
|
| 20 |
class ChatRequest(BaseModel):
|
| 21 |
messages: List[Message]
|
| 22 |
system_prompt: Optional[str] = "You are a helpful assistant."
|
| 23 |
-
max_tokens: Optional[int] =
|
| 24 |
temperature: Optional[float] = 0.7
|
| 25 |
|
| 26 |
API_KEY = "bazi2281488133767695242"
|
|
@@ -30,17 +30,20 @@ def chat(request: ChatRequest, x_api_key: str = Header(None)):
|
|
| 30 |
if x_api_key != API_KEY:
|
| 31 |
raise HTTPException(status_code=403, detail="Go away")
|
| 32 |
|
| 33 |
-
prompt = f"<start_of_turn>
|
| 34 |
for msg in request.messages:
|
| 35 |
-
|
|
|
|
| 36 |
prompt += "<start_of_turn>model\n"
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List, Optional
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id="unsloth/gemma-4-E2B-it-GGUF",
|
| 10 |
+
filename="gemma-4-E2B-it-Q4_K_M.gguf",
|
| 11 |
+
n_ctx=128000
|
| 12 |
+
n_threads=2,
|
| 13 |
+
n_gpu_layers=0
|
| 14 |
)
|
| 15 |
|
| 16 |
class Message(BaseModel):
|
|
|
|
| 20 |
class ChatRequest(BaseModel):
|
| 21 |
messages: List[Message]
|
| 22 |
system_prompt: Optional[str] = "You are a helpful assistant."
|
| 23 |
+
max_tokens: Optional[int] = 128000
|
| 24 |
temperature: Optional[float] = 0.7
|
| 25 |
|
| 26 |
API_KEY = "bazi2281488133767695242"
|
|
|
|
| 30 |
if x_api_key != API_KEY:
|
| 31 |
raise HTTPException(status_code=403, detail="Go away")
|
| 32 |
|
| 33 |
+
prompt = f"<start_of_turn>user\n{request.system_prompt}\n\n"
|
| 34 |
for msg in request.messages:
|
| 35 |
+
role = "model" if msg.role == "assistant" else "user"
|
| 36 |
+
prompt += f"{msg.content}<end_of_turn>\n<start_of_turn>{role}\n"
|
| 37 |
prompt += "<start_of_turn>model\n"
|
| 38 |
|
| 39 |
+
try:
|
| 40 |
+
output = llm(
|
| 41 |
+
prompt,
|
| 42 |
+
max_tokens=request.max_tokens,
|
| 43 |
+
temperature=request.temperature,
|
| 44 |
+
stop=["<end_of_turn>"]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return {"response": output["choices"][0]["text"].strip()}
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise HTTPException(status_code=500, detail=str(e))
|