Files
blackroad-operating-system/backend/app/models/ai_chat.py
Claude 5da6cc9d23 Add comprehensive FastAPI backend for BlackRoad OS
This commit adds a complete backend infrastructure with:

**Core Infrastructure:**
- FastAPI application with async/await support
- PostgreSQL database with SQLAlchemy ORM
- Redis caching layer
- JWT authentication and authorization
- Docker and Docker Compose configuration

**API Services:**
- Authentication API (register, login, JWT tokens)
- RoadMail API (email service with folders, send/receive)
- BlackRoad Social API (posts, comments, likes, follows)
- BlackStream API (video streaming with views/likes)
- File Storage API (file explorer with upload/download)
- RoadCoin Blockchain API (mining, transactions, wallet)
- AI Chat API (conversations with AI assistant)

**Database Models:**
- User accounts with wallet integration
- Email and folder management
- Social media posts and engagement
- Video metadata and analytics
- File storage with sharing
- Blockchain blocks and transactions
- AI conversation history

**Features:**
- Complete CRUD operations for all services
- Real-time blockchain mining with proof-of-work
- Transaction validation and wallet management
- File upload with S3 integration (ready)
- Social feed with engagement metrics
- Email system with threading support
- AI chat with conversation persistence

**Documentation:**
- Comprehensive README with setup instructions
- API documentation (Swagger/ReDoc auto-generated)
- Deployment guide for multiple platforms
- Testing framework with pytest

**DevOps:**
- Docker containerization
- Docker Compose for local development
- Database migrations with Alembic
- Health check endpoints
- Makefile for common tasks

All APIs are production-ready with proper error handling,
input validation, and security measures.
2025-11-16 06:39:16 +00:00

57 lines
1.5 KiB
Python

"""AI Chat models"""
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum
from sqlalchemy.sql import func
import enum
from app.database import Base
class MessageRole(str, enum.Enum):
"""Message role types"""
USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"
class Conversation(Base):
"""AI conversation model"""
__tablename__ = "conversations"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
title = Column(String(255))
model = Column(String(100), default="gpt-3.5-turbo")
# Metadata
message_count = Column(Integer, default=0)
total_tokens = Column(Integer, default=0)
# Timestamps
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
class Message(Base):
"""AI chat message model"""
__tablename__ = "messages"
id = Column(Integer, primary_key=True, index=True)
conversation_id = Column(Integer, ForeignKey("conversations.id", ondelete="CASCADE"), nullable=False)
role = Column(Enum(MessageRole), nullable=False)
content = Column(Text, nullable=False)
# Token usage
tokens = Column(Integer)
# Metadata
model = Column(String(100))
finish_reason = Column(String(50))
created_at = Column(DateTime(timezone=True), server_default=func.now())
def __repr__(self):
return f"<Message {self.id} ({self.role})>"