Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from datetime import datetime | |
| import os | |
| from dotenv import load_dotenv | |
| from typing import List, Optional | |
| from uuid import uuid4 | |
| from rag import RAGSystem, load_config | |
| # Load environment variables | |
| load_dotenv() | |
| # Load configuration | |
| CONFIG = load_config() | |
| MOB_MAPPINGS = CONFIG['mob_mappings'] | |
| # Get API key | |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
| if not OPENAI_API_KEY: | |
| raise ValueError("OPENAI_API_KEY environment variable is not set") | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title="CubixAI API", | |
| description="API for CubixAI - Minecraft bot with AI capabilities", | |
| version="0.1.0", | |
| ) | |
| # Initialize RAG system | |
| rag_system = RAGSystem(openai_api_key=OPENAI_API_KEY) | |
| # Status response model | |
| class StatusResponse(BaseModel): | |
| status: str | |
| version: str | |
| timestamp: str | |
| # Message request model | |
| class MessageRequest(BaseModel): | |
| message: str | |
| user_id: Optional[str] = None | |
| # Document URL model | |
| class DocumentURLs(BaseModel): | |
| urls: List[str] | |
| app.get("/") | |
| def read_root(): | |
| return {"Hello": "World"} | |
| async def get_status(): | |
| """Get the current status of the API.""" | |
| return StatusResponse( | |
| status="online", | |
| version=app.version, | |
| timestamp=datetime.now().isoformat() | |
| ) | |
| async def process_message(request: MessageRequest): | |
| """Process a message from a player and generate a structured JSON response.""" | |
| user_id = request.user_id or str(uuid4()) | |
| response = rag_system.generate_response(user_id, request.message) | |
| # Just add user_id to response without other modifications | |
| if isinstance(response, dict): | |
| response["user_id"] = user_id | |
| return response | |
| async def get_mob_mappings(): | |
| """Get the mapping of mob names to entity IDs.""" | |
| return MOB_MAPPINGS | |
| async def get_command_specs(): | |
| """Get the command specifications.""" | |
| return CONFIG['command_specs'] | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |