ThinkOV2 / app.py
Ayush-003's picture
Upload 28 files
9be2851 verified
Raw History Blame Contribute Delete
11.5 kB
"""
Enhanced FastAPI application for Grocify Chat
Includes chat endpoints, tool execution, voice support, and multilingual capabilities
"""
from fastapi import FastAPI, HTTPException, Depends, Header, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, JSONResponse
from contextlib import asynccontextmanager
import logging
import uuid
import base64
from typing import Optional
from schemas import ChatRequest, ChatResponse, ChatContext, VoiceRequest, VoiceResponse
from config import settings
from agent import agent
from memory import session_manager
from documentProcessor import DocumentProcessor
from embedding_indexer import EmbeddingIndexer
from rag_chain import RAGChain
# Configure logging
logging.basicConfig(
level=getattr(logging, settings.LOG_LEVEL),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Global variables for RAG components
vectorstore = None
rag_chain = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifecycle management for FastAPI app"""
global vectorstore, rag_chain
# Startup
logger.info("Starting Grocify Chat Agent...")
try:
# Validate settings
settings.validate()
# Initialize RAG components
logger.info("Initializing RAG components...")
processor = DocumentProcessor("ecommerce_faq.txt")
texts = processor.load_and_split()
indexer = EmbeddingIndexer()
# Try to load existing vectorstore, otherwise create new
vectorstore = indexer.load_vectorstore()
if vectorstore is None:
logger.info("Creating new vector store...")
vectorstore = indexer.create_vectorstore(texts)
indexer.save_vectorstore(vectorstore)
rag_chain = RAGChain(vectorstore)
logger.info("RAG components initialized successfully")
except Exception as e:
logger.error(f"Startup error: {e}", exc_info=True)
raise
yield
# Shutdown
logger.info("Shutting down Grocify Chat Agent...")
app = FastAPI(
title="Grocify Chat Agent",
description="Intelligent e-commerce customer support chatbot with RAG, tool calling, and multilingual support",
version="2.0.0",
lifespan=lifespan
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ============ AUTHENTICATION ============
async def verify_api_key(x_api_key: Optional[str] = Header(None)):
"""Simple API key verification (optional)"""
# In production, implement proper API key validation
if settings.ENABLE_DEBUG_LOGS:
logger.debug(f"API key verification: {x_api_key is not None}")
return True
# ============ ENDPOINTS ============
@app.get("/")
async def root():
"""Health check endpoint"""
return {
"service": "Grocify Chat Agent",
"version": "2.0.0",
"status": "healthy",
"features": {
"rag": True,
"tool_calling": settings.ENABLE_TOOL_CALLING,
"voice": settings.ENABLE_VOICE,
"multilingual": True,
"sentiment_analysis": settings.ENABLE_SENTIMENT_ANALYSIS
}
}
@app.get("/health")
async def health():
"""Detailed health check"""
return {
"status": "healthy",
"vectorstore_loaded": vectorstore is not None,
"rag_chain_loaded": rag_chain is not None,
"llm_model": settings.LLM_MODEL
}
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest, authenticated: bool = Depends(verify_api_key)):
"""
Main chat endpoint
Process user message with RAG, tool calling, and intelligent escalation
"""
try:
# Generate session ID if not provided
session_id = request.session_id or str(uuid.uuid4())
# Get or create session
session = await session_manager.get_or_create_session(session_id, request.user_id)
# Get conversation history
convo_history = await session_manager.get_conversation_history(session_id)
# Retrieve relevant documents using RAG
retrieved_docs = []
if rag_chain:
retrieved_docs = await rag_chain.retrieve_documents(request.user_message)
# Build chat context
context = ChatContext(
user_message=request.user_message,
convo_history=convo_history,
session_id=session_id,
user_id=request.user_id,
auth_state=request.auth_state,
locale=request.locale,
timezone=request.timezone,
retrieved_docs=retrieved_docs,
tool_responses={},
available_tools=["product_search", "order_lookup", "promotions_check", "sentiment_analysis", "crm_query"],
memory_summary=session.memory_summary
)
# Process with agent
agent_response = await agent.process_message(context)
# Build API response
response = ChatResponse(
session_id=session_id,
response=agent_response,
metadata={
"retrieved_docs_count": len(retrieved_docs),
"locale": request.locale
}
)
logger.info(f"Chat processed for session {session_id}, confidence: {agent_response.confidence}")
return response
except Exception as e:
logger.error(f"Chat endpoint error: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post("/chat/stream")
async def chat_stream(request: ChatRequest, authenticated: bool = Depends(verify_api_key)):
"""
Streaming chat endpoint (for future implementation)
Returns server-sent events for real-time response streaming
"""
# TODO: Implement streaming with LangChain streaming callbacks
raise HTTPException(status_code=501, detail="Streaming not yet implemented")
@app.post("/voice/chat", response_model=VoiceResponse)
async def voice_chat(request: VoiceRequest, authenticated: bool = Depends(verify_api_key)):
"""
Voice chat endpoint
Accepts audio input, transcribes, processes, and returns text + optional audio response
"""
if not settings.ENABLE_VOICE:
raise HTTPException(status_code=403, detail="Voice feature is disabled")
try:
# TODO: Implement STT (Speech-to-Text)
# For now, return placeholder
transcribed_text = "[Voice transcription not yet implemented]"
# Process as regular chat
chat_request = ChatRequest(
user_message=transcribed_text,
session_id=request.session_id,
user_id=request.user_id,
locale=request.locale
)
# Get chat response (reuse chat endpoint logic)
# For simplicity, calling chat endpoint internally
# In production, refactor shared logic
return VoiceResponse(
transcribed_text=transcribed_text,
chat_response=None, # Placeholder
audio_response=None
)
except Exception as e:
logger.error(f"Voice chat error: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.get("/session/{session_id}")
async def get_session(session_id: str, authenticated: bool = Depends(verify_api_key)):
"""Get session details and conversation history"""
try:
session = await session_manager.store.get_session(session_id)
if not session:
raise HTTPException(status_code=404, detail="Session not found")
return {
"session_id": session.session_id,
"user_id": session.user_id,
"created_at": session.created_at,
"updated_at": session.updated_at,
"conversation": session.conversation,
"memory_summary": session.memory_summary,
"escalation_count": session.escalation_count,
"failed_attempts": session.failed_attempts
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Get session error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.delete("/session/{session_id}")
async def delete_session(session_id: str, authenticated: bool = Depends(verify_api_key)):
"""Delete a session"""
try:
success = await session_manager.delete_session(session_id)
if not success:
raise HTTPException(status_code=404, detail="Session not found")
return {"message": "Session deleted successfully", "session_id": session_id}
except HTTPException:
raise
except Exception as e:
logger.error(f"Delete session error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/documents/add")
async def add_documents(
file: UploadFile = File(...),
authenticated: bool = Depends(verify_api_key)
):
"""
Add new documents to vector store
Upload a text file to be indexed for RAG
"""
try:
global vectorstore
# Read file content
content = await file.read()
text_content = content.decode('utf-8')
# Process document
processor = DocumentProcessor(None)
texts = processor.split_text(text_content)
# Add to vector store
indexer = EmbeddingIndexer()
vectorstore = indexer.add_documents(vectorstore, texts)
# Save updated vectorstore
indexer.save_vectorstore(vectorstore)
logger.info(f"Added {len(texts)} document chunks from {file.filename}")
return {
"message": "Documents added successfully",
"filename": file.filename,
"chunks_added": len(texts)
}
except Exception as e:
logger.error(f"Add documents error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/tools")
async def list_tools():
"""List available tools and their descriptions"""
from tools import tool_registry
return {
"tools": tool_registry.get_tool_descriptions()
}
@app.get("/config")
async def get_config():
"""Get public configuration"""
return {
"llm_model": settings.LLM_MODEL,
"supported_locales": settings.SUPPORTED_LOCALES,
"default_locale": settings.DEFAULT_LOCALE,
"features": {
"voice": settings.ENABLE_VOICE,
"sentiment_analysis": settings.ENABLE_SENTIMENT_ANALYSIS,
"tool_calling": settings.ENABLE_TOOL_CALLING,
"auto_translation": settings.ENABLE_AUTO_TRANSLATION
}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app:app",
host="0.0.0.0",
port=8000,
reload=settings.ENABLE_DEBUG_LOGS
)