FANA LLM v0.1.1 | BE Architecture and FastAPI Migration

More details and features of a recent migration from aioHTTP to FastAPI, enhancing backend services for high-performance and maintainable architecture.

Frontend Implementation:

  • The frontend utilizes modern HTML5, CSS3, and JavaScript to provide a responsive user interface.

Image Previews in Chat Interface:

  • The system detects URLs or file uploads within the chat and automatically displays image previews.

  • This is achieved using JavaScript which parses chat messages for URLs, fetches the images, and integrates them into the chat flow as previews.

Migration to FastAPI

Reason for Migration:

  • Performance and Scalability: FastAPI provides automatic interactive API documentation (using Swagger UI) and is based on standard Python type hints, which boosts performance and simplifies the management of asynchronous requests.

  • Enhanced Developer Experience: Features such as automatic data validation, simplified routing, and enhanced error handling improve developer productivity and application robustness.

Backend Setup

FastAPI and Socket.IO Integration

  • Real-Time Communication: Utilizes Socket.IO for WebSocket connections to enable real-time data transmission without polling.

  • ASGI Compatibility: FastAPI's ASGI support ensures that the application can handle asynchronous tasks efficiently, which is essential for real-time operations.

API Endpoints

  • Base Endpoint: GET at http://localhost:8080/api/v1/

  • Image Generation: POST at http://localhost:8080/api/v1/generate-image/

  • LLM Interaction: POST at http://localhost:8080/aimagine/api/v1/interact-with-llm/

  • Image Upload: POST at http://localhost:8080/api/v1/upload-image/

  • Image Analysis: POST at http://localhost:8080/api/v1/analyze-image/

Frontend Implementation

  • Technologies: Utilizes HTML5, CSS3, and JavaScript to deliver a responsive and interactive user experience.

  • Image Previews in Chat Interface: Automatically displays image previews within the chat interface when URLs or file uploads are detected.

Production and Deployment

  • Containerization: Uses Docker to containerize the application, facilitating consistent deployment across various environments.

  • CI/CD: Implements Continuous Integration and Continuous Deployment pipelines to streamline testing and deployment processes.

Documentation and Accessibility

  • API Documentation: Available at FANA LLM API Docs, providing comprehensive details on API usage, parameters, and responses.

  • Extended Documentation: Additional project documentation, including scope and next steps, is maintained in the /docs/ directory.

Security and Access Controls

  • API Security: Incorporates API key authentication and HTTPS to safeguard data transmission and ensure integrity.

  • CORS Configuration: Configured CORS through FastAPI middleware to allow cross-origin requests safely.


Code Snippets

CORS and Middleware Setup in FastAPI

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

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

Socket.IO Event Handling

from socketio import AsyncServer, ASGIApp

sio = AsyncServer(async_mode="asgi")
socket_app = ASGIApp(sio)

@sio.event
async def connect(sid, environ):
    print(f"Client connected: {sid}")

@sio.event
async def disconnect(sid):
    print(f"Client disconnected: {sid}")

Defining API Routes

from fastapi import APIRouter

router = APIRouter(prefix="/api/v1")

@router.get("/", tags=["api_v1"])
async def read_main():
    return {"msg": "Hello from API V1"}

Security Implementation Using HTTPBearer

from fastapi.security import HTTPBearer

security = HTTPBearer()

async def get_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
    # Verify API key

Last updated