File size: 2,067 Bytes
ac0cbd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from llama_cpp import Llama
import os
import uvicorn

app = FastAPI(title="Atlas Chat Assistant")

# Load model at startup
model_path = "/app/models/atlas-chat-9b-merged-q4_k_s-imat.gguf"
llm = Llama(
    model_path=model_path,
    n_ctx=2048,
    n_threads=2,  # Adjust for 2 vCPU
    n_gpu_layers=0,
    verbose=False
)

SYSTEM_PROMPT = """You are Atlas, a helpful customer service assistant for an e-commerce store selling all kinds of products.
You help customers with product questions, orders, delivery, returns, and promotions.

CRITICAL RULES:
1. Detect the customer's language (Darija, French, or English) and ALWAYS reply in the same language
2. For DARIJA (Algerian dialect): use words like "sahbi", "labas", "3ndna", "wakha", "mliha", "zedma"
3. Never start your reply with "Assistant:"
4. Be short (2-3 sentences max), friendly, and helpful
5. If customer asks about QUALITY, reassure them about product quality
6. If customer wants to ORDER, ask for: name, phone, address

PRODUCTS:
- Robe Florale: 9800 DA, Rose, S/M/L - Disponible
- Robe Aspirine: 5200 DA, Blanc, S/M/L - En rupture
- Chaussures Cuir: 12000 DA, Noir/Marron, 38-44 - Disponible"""

class CompletionRequest(BaseModel):
    prompt: str
    max_tokens: int = 150
    temperature: float = 0.7

@app.get("/")
def root():
    return {"status": "Atlas is running"}

@app.get("/health")
def health():
    return {"status": "healthy"}

@app.post("/v1/completions")
def completions(request: CompletionRequest):
    full_prompt = f"{SYSTEM_PROMPT}\n\nClient: {request.prompt}\nAssistant:"
    
    response = llm(
        full_prompt,
        max_tokens=request.max_tokens,
        temperature=request.temperature,
        stop=["Client:", "\n\nClient", "User:", "\n\nUser"]
    )
    
    reply = response['choices'][0]['text'].strip()
    reply = reply.replace("Assistant:", "").replace("assistant:", "").strip()
    
    return {"text": reply}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)