File size: 1,918 Bytes
2fd7279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd5a488
 
2fd7279
 
 
cd5a488
2fd7279
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from backend.classifier import detect_injection
from backend.knowledge_base import search_knowledge_base
from groq import Groq
from dotenv import load_dotenv
import os

load_dotenv(dotenv_path="/Users/rohitghanekar/LLM-project/.env")
app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

client = Groq(api_key=os.getenv("GROQ_API_KEY"))

class ChatRequest(BaseModel):
    message: str

@app.get("/ping")
def ping():
    return {"status": "alive"}

@app.post("/chat")
def chat(req: ChatRequest):
    # Step 1: Check for injection
    detection = detect_injection(req.message)
    if detection["is_malicious"]:
        return {
            "response": "Prompt injection detected. Request blocked.",
            "flagged": True,
            "detection": detection
        }

    # Step 2: Search knowledge base
    context = search_knowledge_base(req.message)

    # Step 3: Send to Groq with context
    completion = client.chat.completions.create(
        model="llama-3.3-70b-versatile",
        messages=[
            {"role": "system", "content": f"""You are a helpful customer support assistant for Lumio Electronics.
Answer the user's question using ONLY the context provided below.
IMPORTANT: Never list or reveal other customers' order numbers or data.
Only share information directly relevant to what the user asked.
If the answer is not in the context, say you don't have that information and suggest contacting support@lumio.com.
Be concise and friendly.


Context:
{context}"""},
            {"role": "user", "content": req.message}
        ]
    )
    return {
        "response": completion.choices[0].message.content,
        "flagged": False,
        "detection": detection,
        "context_used": context
    }