File size: 1,867 Bytes
3e66fd0 |
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 |
import os
from fastapi import FastAPI
from pydantic import BaseModel
from llama_cpp import Llama
# GGUF model path (Hugging Face Space'te bu modele yüklemen gerek)
MODEL_PATH = "./model/Turkish-Llama-3-8B-function-calling.Q4_K_M.gguf"
# System prompt (hard-coded test promptumuz)
SYSTEM_PROMPT = """
Siz bir görev tabanlı asistan botsunuz. Kullanıcının doğal dildeki mesajlarını anlayabilir, niyetlerini (intent) tespit edebilir, eksik bilgileri sorabilir ve backend API'lerine tetikleme hazırlığı yapabilirsiniz.
❗ Cevaplarınızda mutlaka aşağıdaki formatlı blokları döndürmelisiniz ve bunların dışında hiçbir metin, açıklama veya selamlama eklememelisiniz.
✅ Format:
#ANSWER: <cevap metni veya NONE>
#INTENT: <intent_adı> (veya NONE)
#PARAMS: {parametre_adı: değer, ...}
#MISSING: [eksik_parametre_adı, ...]
#ACTION_JSON: {api için gönderilecek json, eksikse boş bırak}
✅ Desteklenen intent'ler:
- doviz-kuru-intent → parametre: currency (dolar, euro, TL)
- yol-durumu-intent → parametreler: from_location, to_location (Ankara, İstanbul, İzmir)
- hava-durumu-intent → parametre: city (Ankara, İstanbul, İzmir)
❗ Kullanıcıya hitap ederken formal bir dil kullanınız, sadece bu formatlı blokları döndürünüz.
"""
app = FastAPI()
llm = None
class ChatRequest(BaseModel):
prompt: str
@app.on_event("startup")
def load_model():
global llm
llm = Llama(model_path=MODEL_PATH, n_gpu_layers=-1, n_ctx=4096)
print("✅ Model yüklendi.")
@app.post("/chat")
def chat(req: ChatRequest):
prompt = f"{SYSTEM_PROMPT}\n\nKullanıcı: {req.prompt}\nAsistan:"
response = llm(prompt, max_tokens=512, stop=["Kullanıcı:", "Asistan:"], echo=False)
answer = response["choices"][0]["text"].strip()
return {"response": answer}
@app.get("/")
def health():
return {"status": "ok"}
|