Project Goal:
Browse filesBuild a production-ready backend for an AI Customer Support Bot that connects to a Next.js 15 frontend (already deployed on Vercel).
Framework & Language:
Python 3.11
FastAPI (async)
Core Features:
/chat/stream (WebSocket)
Accepts incoming JSON messages { "type": "user_message", "content": "..." }
Streams responses in JSON { "type": "assistant_chunk", "chunk": "..." }
Sends { "type": "assistant_done", "content": "..." } when complete.
Uses Gemini API for LLM responses (google.generativeai package).
Uses FAISS for retrieval (RAG pipeline).
Stores session state in Redis.
/faq/upload
Accepts multipart/form-data with file.
Parses .csv or .md files.
Generates embeddings with Gemini embeddings API.
Stores vectors in FAISS.
/escalate
Creates a ticket record in PostgreSQL.
Sends email via SendGrid if configured.
/escalate/list
Returns list of open/closed escalations from Postgres.
/health
Returns { "ok": true }.
Environment Variables:
GEMINI_API_KEY=your_gemini_key
REDIS_URL=redis://default:password@localhost:6379
POSTGRES_URL=postgresql://user:password@localhost:5432/supportdb
SENDGRID_KEY=your_sendgrid_key
CORS_ORIGINS=https://ai-customer-support-bot.vercel.app,http://localhost:3000
Other Requirements:
Include Dockerfile and requirements.txt.
Use uvicorn main:app --host 0.0.0.0 --port 8000 as default CMD.
Return complete folder structure:
backend/
main.py
routes/
services/
models/
database/
requirements.txt
Dockerfile
Add comments explaining where Gemini calls and FAISS indexing happen.
Must deploy cleanly to Railway using requirements.txt.
Expected Output:
Ready-to-run backend folder named backend/ compatible with frontend endpoints /chat/stream, /faq/upload, /escalate, and /escalate/list.
- README.md +8 -5
- backend-structure.html +283 -0
- index.html +189 -18
|
@@ -1,10 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: GeminiGuard Support Backend π
|
| 3 |
+
colorFrom: pink
|
| 4 |
+
colorTo: yellow
|
| 5 |
+
emoji: π³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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Backend Structure | GeminiGuard</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
| 9 |
+
<style>
|
| 10 |
+
.tree-node {
|
| 11 |
+
position: relative;
|
| 12 |
+
padding-left: 1.5rem;
|
| 13 |
+
}
|
| 14 |
+
.tree-node::before {
|
| 15 |
+
content: "";
|
| 16 |
+
position: absolute;
|
| 17 |
+
left: 0.5rem;
|
| 18 |
+
top: 0;
|
| 19 |
+
bottom: 0;
|
| 20 |
+
width: 2px;
|
| 21 |
+
background-color: #E5E7EB;
|
| 22 |
+
}
|
| 23 |
+
.tree-item::before {
|
| 24 |
+
content: "";
|
| 25 |
+
position: absolute;
|
| 26 |
+
left: 0.25rem;
|
| 27 |
+
top: 0.75rem;
|
| 28 |
+
width: 0.5rem;
|
| 29 |
+
height: 0.5rem;
|
| 30 |
+
border-radius: 50%;
|
| 31 |
+
background-color: #3B82F6;
|
| 32 |
+
}
|
| 33 |
+
.animate-float {
|
| 34 |
+
animation: float 3s ease-in-out infinite;
|
| 35 |
+
}
|
| 36 |
+
@keyframes float {
|
| 37 |
+
0%, 100% { transform: translateY(0); }
|
| 38 |
+
50% { transform: translateY(-10px); }
|
| 39 |
+
}
|
| 40 |
+
</style>
|
| 41 |
+
</head>
|
| 42 |
+
<body class="bg-gray-50">
|
| 43 |
+
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
|
| 44 |
+
<div class="text-center mb-12">
|
| 45 |
+
<h1 class="text-4xl font-bold text-gray-900 mb-4">GeminiGuard Backend Architecture</h1>
|
| 46 |
+
<p class="text-xl text-gray-600 max-w-3xl mx-auto">Production-ready Python 3.11 + FastAPI structure with Redis, PostgreSQL and FAISS integration</p>
|
| 47 |
+
</div>
|
| 48 |
+
|
| 49 |
+
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8 mb-12">
|
| 50 |
+
<!-- File Tree -->
|
| 51 |
+
<div class="bg-white rounded-xl shadow-md overflow-hidden lg:col-span-1">
|
| 52 |
+
<div class="p-6 border-b border-gray-200">
|
| 53 |
+
<h2 class="text-xl font-semibold text-gray-800">Directory Structure</h2>
|
| 54 |
+
</div>
|
| 55 |
+
<div class="p-6">
|
| 56 |
+
<div class="space-y-1 font-mono text-sm">
|
| 57 |
+
<div class="tree-node">
|
| 58 |
+
<div class="tree-item relative pl-4 py-1 text-blue-600 font-bold">backend/</div>
|
| 59 |
+
<div class="tree-node">
|
| 60 |
+
<div class="tree-item relative pl-4 py-1">main.py</div>
|
| 61 |
+
<div class="tree-item relative pl-4 py-1">routes/</div>
|
| 62 |
+
<div class="tree-node">
|
| 63 |
+
<div class="tree-item relative pl-4 py-1">chat.py</div>
|
| 64 |
+
<div class="tree-item relative pl-4 py-1">faq.py</div>
|
| 65 |
+
<div class="tree-item relative pl-4 py-1">escalate.py</div>
|
| 66 |
+
</div>
|
| 67 |
+
<div class="tree-item relative pl-4 py-1">services/</div>
|
| 68 |
+
<div class="tree-node">
|
| 69 |
+
<div class="tree-item relative pl-4 py-1">gemini_service.py</div>
|
| 70 |
+
<div class="tree-item relative pl-4 py-1">faiss_service.py</div>
|
| 71 |
+
<div class="tree-item relative pl-4 py-1">redis_service.py</div>
|
| 72 |
+
</div>
|
| 73 |
+
<div class="tree-item relative pl-4 py-1">models/</div>
|
| 74 |
+
<div class="tree-node">
|
| 75 |
+
<div class="tree-item relative pl-4 py-1">chat_models.py</div>
|
| 76 |
+
<div class="tree-item relative pl-4 py-1">escalation_models.py</div>
|
| 77 |
+
</div>
|
| 78 |
+
<div class="tree-item relative pl-4 py-1">database/</div>
|
| 79 |
+
<div class="tree-node">
|
| 80 |
+
<div class="tree-item relative pl-4 py-1">postgres.py</div>
|
| 81 |
+
<div class="tree-item relative pl-4 py-1">redis.py</div>
|
| 82 |
+
</div>
|
| 83 |
+
<div class="tree-item relative pl-4 py-1">requirements.txt</div>
|
| 84 |
+
<div class="tree-item relative pl-4 py-1">Dockerfile</div>
|
| 85 |
+
</div>
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
</div>
|
| 89 |
+
</div>
|
| 90 |
+
|
| 91 |
+
<!-- Key Components -->
|
| 92 |
+
<div class="lg:col-span-2">
|
| 93 |
+
<div class="bg-white rounded-xl shadow-md overflow-hidden mb-6">
|
| 94 |
+
<div class="p-6 border-b border-gray-200">
|
| 95 |
+
<h2 class="text-xl font-semibold text-gray-800">Core Components</h2>
|
| 96 |
+
</div>
|
| 97 |
+
<div class="p-6">
|
| 98 |
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
| 99 |
+
<!-- Gemini Service -->
|
| 100 |
+
<div class="bg-blue-50 rounded-lg p-4">
|
| 101 |
+
<div class="flex items-center mb-3">
|
| 102 |
+
<div class="bg-blue-100 p-2 rounded-full mr-3">
|
| 103 |
+
<i data-feather="cpu" class="text-blue-600"></i>
|
| 104 |
+
</div>
|
| 105 |
+
<h3 class="font-semibold text-gray-800">Gemini Service</h3>
|
| 106 |
+
</div>
|
| 107 |
+
<p class="text-sm text-gray-600 mb-2">Handles all Gemini API interactions:</p>
|
| 108 |
+
<ul class="text-xs text-gray-700 space-y-1">
|
| 109 |
+
<li class="flex items-start">
|
| 110 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 111 |
+
<span>Streaming chat completions</span>
|
| 112 |
+
</li>
|
| 113 |
+
<li class="flex items-start">
|
| 114 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 115 |
+
<span>Embedding generation</span>
|
| 116 |
+
</li>
|
| 117 |
+
<li class="flex items-start">
|
| 118 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 119 |
+
<span>Token counting and rate limiting</span>
|
| 120 |
+
</li>
|
| 121 |
+
</ul>
|
| 122 |
+
</div>
|
| 123 |
+
|
| 124 |
+
<!-- FAISS Service -->
|
| 125 |
+
<div class="bg-green-50 rounded-lg p-4">
|
| 126 |
+
<div class="flex items-center mb-3">
|
| 127 |
+
<div class="bg-green-100 p-2 rounded-full mr-3">
|
| 128 |
+
<i data-feather="database" class="text-green-600"></i>
|
| 129 |
+
</div>
|
| 130 |
+
<h3 class="font-semibold text-gray-800">FAISS Service</h3>
|
| 131 |
+
</div>
|
| 132 |
+
<p class="text-sm text-gray-600 mb-2">Manages vector operations:</p>
|
| 133 |
+
<ul class="text-xs text-gray-700 space-y-1">
|
| 134 |
+
<li class="flex items-start">
|
| 135 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 136 |
+
<span>Index creation from FAQs</span>
|
| 137 |
+
</li>
|
| 138 |
+
<li class="flex items-start">
|
| 139 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 140 |
+
<span>Nearest neighbor search</span>
|
| 141 |
+
</li>
|
| 142 |
+
<li class="flex items-start">
|
| 143 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 144 |
+
<span>Index persistence to disk</span>
|
| 145 |
+
</li>
|
| 146 |
+
</ul>
|
| 147 |
+
</div>
|
| 148 |
+
|
| 149 |
+
<!-- Redis Service -->
|
| 150 |
+
<div class="bg-red-50 rounded-lg p-4">
|
| 151 |
+
<div class="flex items-center mb-3">
|
| 152 |
+
<div class="bg-red-100 p-2 rounded-full mr-3">
|
| 153 |
+
<i data-feather="hard-drive" class="text-red-600"></i>
|
| 154 |
+
</div>
|
| 155 |
+
<h3 class="font-semibold text-gray-800">Redis Service</h3>
|
| 156 |
+
</div>
|
| 157 |
+
<p class="text-sm text-gray-600 mb-2">Session state management:</p>
|
| 158 |
+
<ul class="text-xs text-gray-700 space-y-1">
|
| 159 |
+
<li class="flex items-start">
|
| 160 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 161 |
+
<span>WebSocket session storage</span>
|
| 162 |
+
</li>
|
| 163 |
+
<li class="flex items-start">
|
| 164 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 165 |
+
<span>Rate limiting counters</span>
|
| 166 |
+
</li>
|
| 167 |
+
<li class="flex items-start">
|
| 168 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 169 |
+
<span>Cache for frequent queries</span>
|
| 170 |
+
</li>
|
| 171 |
+
</ul>
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
<!-- PostgreSQL Service -->
|
| 175 |
+
<div class="bg-purple-50 rounded-lg p-4">
|
| 176 |
+
<div class="flex items-center mb-3">
|
| 177 |
+
<div class="bg-purple-100 p-2 rounded-full mr-3">
|
| 178 |
+
<i data-feather="archive" class="text-purple-600"></i>
|
| 179 |
+
</div>
|
| 180 |
+
<h3 class="font-semibold text-gray-800">PostgreSQL Service</h3>
|
| 181 |
+
</div>
|
| 182 |
+
<p class="text-sm text-gray-600 mb-2">Ticket persistence:</p>
|
| 183 |
+
<ul class="text-xs text-gray-700 space-y-1">
|
| 184 |
+
<li class="flex items-start">
|
| 185 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 186 |
+
<span>Escalation ticket storage</span>
|
| 187 |
+
</li>
|
| 188 |
+
<li class="flex items-start">
|
| 189 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 190 |
+
<span>Status tracking</span>
|
| 191 |
+
</li>
|
| 192 |
+
<li class="flex items-start">
|
| 193 |
+
<i data-feather="check" class="w-4 h-4 mr-2 text-green-500 mt-0.5"></i>
|
| 194 |
+
<span>Audit logging</span>
|
| 195 |
+
</li>
|
| 196 |
+
</ul>
|
| 197 |
+
</div>
|
| 198 |
+
</div>
|
| 199 |
+
</div>
|
| 200 |
+
</div>
|
| 201 |
+
|
| 202 |
+
<!-- Code Samples -->
|
| 203 |
+
<div class="bg-white rounded-xl shadow-md overflow-hidden">
|
| 204 |
+
<div class="p-6 border-b border-gray-200">
|
| 205 |
+
<h2 class="text-xl font-semibold text-gray-800">Code Highlights</h2>
|
| 206 |
+
</div>
|
| 207 |
+
<div class="p-6">
|
| 208 |
+
<div class="mb-6">
|
| 209 |
+
<h3 class="font-medium text-gray-800 mb-2">WebSocket Chat Endpoint (routes/chat.py)</h3>
|
| 210 |
+
<div class="bg-gray-900 rounded-lg p-4 text-xs text-gray-300 overflow-x-auto">
|
| 211 |
+
<pre><code>@router.websocket("/chat/stream")
|
| 212 |
+
async def websocket_chat(websocket: WebSocket):
|
| 213 |
+
await websocket.accept()
|
| 214 |
+
session_id = str(uuid.uuid4())
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
while True:
|
| 218 |
+
data = await websocket.receive_json()
|
| 219 |
+
|
| 220 |
+
if data["type"] == "user_message":
|
| 221 |
+
# Generate stream from Gemini with RAG context
|
| 222 |
+
async for chunk in gemini_service.stream_response(
|
| 223 |
+
message=data["content"],
|
| 224 |
+
session_id=session_id
|
| 225 |
+
):
|
| 226 |
+
await websocket.send_json({
|
| 227 |
+
"type": "assistant_chunk",
|
| 228 |
+
"chunk": chunk
|
| 229 |
+
})
|
| 230 |
+
|
| 231 |
+
await websocket.send_json({
|
| 232 |
+
"type": "assistant_done",
|
| 233 |
+
"content": "Stream complete"
|
| 234 |
+
})
|
| 235 |
+
except WebSocketDisconnect:
|
| 236 |
+
await redis_service.clear_session(session_id)</code></pre>
|
| 237 |
+
</div>
|
| 238 |
+
</div>
|
| 239 |
+
|
| 240 |
+
<div>
|
| 241 |
+
<h3 class="font-medium text-gray-800 mb-2">FAISS Index Creation (services/faiss_service.py)</h3>
|
| 242 |
+
<div class="bg-gray-900 rounded-lg p-4 text-xs text-gray-300 overflow-x-auto">
|
| 243 |
+
<pre><code>async def create_index_from_documents(docs: List[str]):
|
| 244 |
+
# Generate embeddings using Gemini
|
| 245 |
+
embeddings = await gemini_service.generate_embeddings(docs)
|
| 246 |
+
|
| 247 |
+
# Convert to numpy array
|
| 248 |
+
vectors = np.array(embeddings).astype('float32')
|
| 249 |
+
|
| 250 |
+
# Create FAISS index
|
| 251 |
+
dimension = vectors.shape[1]
|
| 252 |
+
index = faiss.IndexFlatL2(dimension)
|
| 253 |
+
index.add(vectors)
|
| 254 |
+
|
| 255 |
+
# Save index to disk
|
| 256 |
+
faiss.write_index(index, "faiss_index.index")
|
| 257 |
+
|
| 258 |
+
# Store document metadata in Redis
|
| 259 |
+
await redis_service.store_documents(
|
| 260 |
+
[{"id": str(uuid.uuid4()), "text": doc} for doc in docs]
|
| 261 |
+
)</code></pre>
|
| 262 |
+
</div>
|
| 263 |
+
</div>
|
| 264 |
+
</div>
|
| 265 |
+
</div>
|
| 266 |
+
</div>
|
| 267 |
+
</div>
|
| 268 |
+
|
| 269 |
+
<div class="bg-blue-50 rounded-xl p-6 text-center">
|
| 270 |
+
<h3 class="text-2xl font-bold text-gray-800 mb-4">Ready to Implement?</h3>
|
| 271 |
+
<p class="text-gray-600 mb-6">This architecture is optimized for high-performance AI support with minimal latency.</p>
|
| 272 |
+
<button class="inline-flex items-center px-6 py-3 bg-blue-600 text-white rounded-lg font-semibold hover:bg-blue-700 transition">
|
| 273 |
+
<i data-feather="download" class="w-5 h-5 mr-2"></i>
|
| 274 |
+
Download Complete Backend
|
| 275 |
+
</button>
|
| 276 |
+
</div>
|
| 277 |
+
</div>
|
| 278 |
+
|
| 279 |
+
<script>
|
| 280 |
+
feather.replace();
|
| 281 |
+
</script>
|
| 282 |
+
</body>
|
| 283 |
+
</html>
|
|
@@ -1,19 +1,190 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>GeminiGuard Backend Docs</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<style>
|
| 9 |
+
.gradient-bg {
|
| 10 |
+
background: linear-gradient(135deg, #6B73FF 0%, #000DFF 100%);
|
| 11 |
+
}
|
| 12 |
+
.code-block {
|
| 13 |
+
font-family: 'Courier New', monospace;
|
| 14 |
+
background-color: #1E293B;
|
| 15 |
+
border-radius: 0.5rem;
|
| 16 |
+
}
|
| 17 |
+
.endpoint-card:hover {
|
| 18 |
+
transform: translateY(-5px);
|
| 19 |
+
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
|
| 20 |
+
}
|
| 21 |
+
</style>
|
| 22 |
+
</head>
|
| 23 |
+
<body class="bg-gray-100">
|
| 24 |
+
<div class="gradient-bg text-white py-16 px-4 sm:px-6 lg:px-8">
|
| 25 |
+
<div class="max-w-7xl mx-auto text-center">
|
| 26 |
+
<h1 class="text-4xl md:text-5xl font-bold mb-6">GeminiGuard Support Backend</h1>
|
| 27 |
+
<p class="text-xl md:text-2xl mb-8">Powering AI customer support with Gemini, FastAPI & Redis</p>
|
| 28 |
+
<div class="flex justify-center space-x-4">
|
| 29 |
+
<a href="#endpoints" class="px-6 py-3 bg-white text-blue-600 rounded-lg font-semibold hover:bg-gray-100 transition">API Endpoints</a>
|
| 30 |
+
<a href="#setup" class="px-6 py-3 bg-blue-800 text-white rounded-lg font-semibold hover:bg-blue-900 transition">Setup Guide</a>
|
| 31 |
+
</div>
|
| 32 |
+
</div>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
|
| 36 |
+
<div id="endpoints" class="mb-16">
|
| 37 |
+
<h2 class="text-3xl font-bold text-gray-800 mb-8">API Endpoints</h2>
|
| 38 |
+
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
| 39 |
+
<!-- WebSocket Chat -->
|
| 40 |
+
<div class="endpoint-card bg-white rounded-xl shadow-md p-6 transition duration-300">
|
| 41 |
+
<div class="flex items-center mb-4">
|
| 42 |
+
<div class="bg-blue-100 p-2 rounded-full mr-4">
|
| 43 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-blue-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 44 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
|
| 45 |
+
</svg>
|
| 46 |
+
</div>
|
| 47 |
+
<h3 class="text-xl font-semibold text-gray-800">Real-time Chat</h3>
|
| 48 |
+
</div>
|
| 49 |
+
<div class="mb-4">
|
| 50 |
+
<span class="inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded font-mono">WebSocket</span>
|
| 51 |
+
<span class="inline-block ml-2 bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded font-mono">/chat/stream</span>
|
| 52 |
+
</div>
|
| 53 |
+
<p class="text-gray-600 mb-4">Stream AI responses using Gemini API with RAG from FAISS vectors.</p>
|
| 54 |
+
<div class="code-block p-4 text-white mb-4">
|
| 55 |
+
<code>{
|
| 56 |
+
"type": "user_message",
|
| 57 |
+
"content": "How do I reset my password?"
|
| 58 |
+
}</code>
|
| 59 |
+
</div>
|
| 60 |
+
</div>
|
| 61 |
+
|
| 62 |
+
<!-- FAQ Upload -->
|
| 63 |
+
<div class="endpoint-card bg-white rounded-xl shadow-md p-6 transition duration-300">
|
| 64 |
+
<div class="flex items-center mb-4">
|
| 65 |
+
<div class="bg-green-100 p-2 rounded-full mr-4">
|
| 66 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-green-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 67 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
|
| 68 |
+
</svg>
|
| 69 |
+
</div>
|
| 70 |
+
<h3 class="text-xl font-semibold text-gray-800">FAQ Upload</h3>
|
| 71 |
+
</div>
|
| 72 |
+
<div class="mb-4">
|
| 73 |
+
<span class="inline-block bg-green-100 text-green-800 text-xs px-2 py-1 rounded font-mono">POST</span>
|
| 74 |
+
<span class="inline-block ml-2 bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded font-mono">/faq/upload</span>
|
| 75 |
+
</div>
|
| 76 |
+
<p class="text-gray-600 mb-4">Upload CSV/Markdown files to generate embeddings for RAG pipeline.</p>
|
| 77 |
+
<div class="code-block p-4 text-white mb-4">
|
| 78 |
+
<code>curl -X POST -F "file=@faqs.csv" http://localhost:8000/faq/upload</code>
|
| 79 |
+
</div>
|
| 80 |
+
</div>
|
| 81 |
+
|
| 82 |
+
<!-- Escalation -->
|
| 83 |
+
<div class="endpoint-card bg-white rounded-xl shadow-md p-6 transition duration-300">
|
| 84 |
+
<div class="flex items-center mb-4">
|
| 85 |
+
<div class="bg-red-100 p-2 rounded-full mr-4">
|
| 86 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-red-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 87 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
|
| 88 |
+
</svg>
|
| 89 |
+
</div>
|
| 90 |
+
<h3 class="text-xl font-semibold text-gray-800">Escalation</h3>
|
| 91 |
+
</div>
|
| 92 |
+
<div class="mb-4">
|
| 93 |
+
<span class="inline-block bg-red-100 text-red-800 text-xs px-2 py-1 rounded font-mono">POST</span>
|
| 94 |
+
<span class="inline-block ml-2 bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded font-mono">/escalate</span>
|
| 95 |
+
</div>
|
| 96 |
+
<p class="text-gray-600 mb-4">Create support tickets stored in PostgreSQL with optional SendGrid email.</p>
|
| 97 |
+
<div class="code-block p-4 text-white mb-4">
|
| 98 |
+
<code>{
|
| 99 |
+
"issue": "Payment failed",
|
| 100 |
+
"description": "Card declined despite sufficient funds",
|
| 101 |
+
"customer_email": "user@example.com"
|
| 102 |
+
}</code>
|
| 103 |
+
</div>
|
| 104 |
+
</div>
|
| 105 |
+
|
| 106 |
+
<!-- Escalation List -->
|
| 107 |
+
<div class="endpoint-card bg-white rounded-xl shadow-md p-6 transition duration-300">
|
| 108 |
+
<div class="flex items-center mb-4">
|
| 109 |
+
<div class="bg-purple-100 p-2 rounded-full mr-4">
|
| 110 |
+
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-purple-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
| 111 |
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
| 112 |
+
</svg>
|
| 113 |
+
</div>
|
| 114 |
+
<h3 class="text-xl font-semibold text-gray-800">Escalation List</h3>
|
| 115 |
+
</div>
|
| 116 |
+
<div class="mb-4">
|
| 117 |
+
<span class="inline-block bg-purple-100 text-purple-800 text-xs px-2 py-1 rounded font-mono">GET</span>
|
| 118 |
+
<span class="inline-block ml-2 bg-gray-100 text-gray-800 text-xs px-2 py-1 rounded font-mono">/escalate/list</span>
|
| 119 |
+
</div>
|
| 120 |
+
<p class="text-gray-600 mb-4">Retrieve all support tickets with filtering options.</p>
|
| 121 |
+
<div class="code-block p-4 text-white mb-4">
|
| 122 |
+
<code>[
|
| 123 |
+
{
|
| 124 |
+
"id": 1,
|
| 125 |
+
"status": "open",
|
| 126 |
+
"created_at": "2023-07-15T09:30:00Z",
|
| 127 |
+
"customer_email": "user@example.com"
|
| 128 |
+
}
|
| 129 |
+
]</code>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
</div>
|
| 133 |
+
</div>
|
| 134 |
+
|
| 135 |
+
<div id="setup" class="mb-16">
|
| 136 |
+
<h2 class="text-3xl font-bold text-gray-800 mb-8">Setup Guide</h2>
|
| 137 |
+
<div class="bg-white rounded-xl shadow-md overflow-hidden">
|
| 138 |
+
<div class="p-6">
|
| 139 |
+
<h3 class="text-xl font-semibold text-gray-800 mb-4">Environment Variables</h3>
|
| 140 |
+
<div class="code-block p-4 text-white mb-6">
|
| 141 |
+
<code>GEMINI_API_KEY=your_gemini_key
|
| 142 |
+
REDIS_URL=redis://default:password@localhost:6379
|
| 143 |
+
POSTGRES_URL=postgresql://user:password@localhost:5432/supportdb
|
| 144 |
+
SENDGRID_KEY=your_sendgrid_key
|
| 145 |
+
CORS_ORIGINS=https://your-frontend.vercel.app,http://localhost:3000</code>
|
| 146 |
+
</div>
|
| 147 |
+
|
| 148 |
+
<h3 class="text-xl font-semibold text-gray-800 mb-4">Running with Docker</h3>
|
| 149 |
+
<div class="code-block p-4 text-white mb-6">
|
| 150 |
+
<code># Build the image
|
| 151 |
+
docker build -t geminiguard-backend .
|
| 152 |
+
|
| 153 |
+
# Run the container
|
| 154 |
+
docker run -p 8000:8000 --env-file .env geminiguard-backend</code>
|
| 155 |
+
</div>
|
| 156 |
+
|
| 157 |
+
<h3 class="text-xl font-semibold text-gray-800 mb-4">Directory Structure</h3>
|
| 158 |
+
<div class="code-block p-4 text-white">
|
| 159 |
+
<code>backend/
|
| 160 |
+
βββ main.py # FastAPI app entrypoint
|
| 161 |
+
βββ routes/ # API endpoint definitions
|
| 162 |
+
β βββ chat.py # WebSocket chat endpoint
|
| 163 |
+
β βββ faq.py # FAQ upload endpoint
|
| 164 |
+
β βββ escalate.py # Ticket endpoints
|
| 165 |
+
βββ services/ # Business logic
|
| 166 |
+
β βββ gemini_service.py # Gemini API calls
|
| 167 |
+
β βββ faiss_service.py # FAISS vector operations
|
| 168 |
+
βββ models/ # Pydantic models
|
| 169 |
+
βββ database/ # DB connections
|
| 170 |
+
βββ requirements.txt # Python dependencies
|
| 171 |
+
βββ Dockerfile # Container configuration</code>
|
| 172 |
+
</div>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
</div>
|
| 176 |
+
|
| 177 |
+
<div class="text-center py-8">
|
| 178 |
+
<h3 class="text-2xl font-bold text-gray-800 mb-4">Ready to Deploy?</h3>
|
| 179 |
+
<p class="text-gray-600 mb-6">This backend is production-ready for Railway deployment with all required endpoints.</p>
|
| 180 |
+
<a href="#" class="inline-block px-8 py-3 bg-blue-600 text-white rounded-lg font-semibold hover:bg-blue-700 transition">Download Complete Code</a>
|
| 181 |
+
</div>
|
| 182 |
+
</div>
|
| 183 |
+
|
| 184 |
+
<footer class="gradient-bg text-white py-8">
|
| 185 |
+
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
|
| 186 |
+
<p>Β© 2023 GeminiGuard Support Backend. All rights reserved.</p>
|
| 187 |
+
</div>
|
| 188 |
+
</footer>
|
| 189 |
+
</body>
|
| 190 |
</html>
|