GeminiGuard Backend Architecture

Production-ready Python 3.11 + FastAPI structure with Redis, PostgreSQL and FAISS integration

Directory Structure

backend/
main.py
routes/
chat.py
faq.py
escalate.py
services/
gemini_service.py
faiss_service.py
redis_service.py
models/
chat_models.py
escalation_models.py
database/
postgres.py
redis.py
requirements.txt
Dockerfile

Core Components

Gemini Service

Handles all Gemini API interactions:

  • Streaming chat completions
  • Embedding generation
  • Token counting and rate limiting

FAISS Service

Manages vector operations:

  • Index creation from FAQs
  • Nearest neighbor search
  • Index persistence to disk

Redis Service

Session state management:

  • WebSocket session storage
  • Rate limiting counters
  • Cache for frequent queries

PostgreSQL Service

Ticket persistence:

  • Escalation ticket storage
  • Status tracking
  • Audit logging

Code Highlights

WebSocket Chat Endpoint (routes/chat.py)

@router.websocket("/chat/stream")
async def websocket_chat(websocket: WebSocket):
    await websocket.accept()
    session_id = str(uuid.uuid4())
    
    try:
        while True:
            data = await websocket.receive_json()
            
            if data["type"] == "user_message":
                # Generate stream from Gemini with RAG context
                async for chunk in gemini_service.stream_response(
                    message=data["content"],
                    session_id=session_id
                ):
                    await websocket.send_json({
                        "type": "assistant_chunk",
                        "chunk": chunk
                    })
                
                await websocket.send_json({
                    "type": "assistant_done",
                    "content": "Stream complete"
                })
    except WebSocketDisconnect:
        await redis_service.clear_session(session_id)

FAISS Index Creation (services/faiss_service.py)

async def create_index_from_documents(docs: List[str]):
    # Generate embeddings using Gemini
    embeddings = await gemini_service.generate_embeddings(docs)
    
    # Convert to numpy array
    vectors = np.array(embeddings).astype('float32')
    
    # Create FAISS index
    dimension = vectors.shape[1]
    index = faiss.IndexFlatL2(dimension)
    index.add(vectors)
    
    # Save index to disk
    faiss.write_index(index, "faiss_index.index")
    
    # Store document metadata in Redis
    await redis_service.store_documents(
        [{"id": str(uuid.uuid4()), "text": doc} for doc in docs]
    )

Ready to Implement?

This architecture is optimized for high-performance AI support with minimal latency.