Files
aitbc/apps/stubs/ai-service/src/ai_service/main.py
aitbc 3897bcbf24
Some checks failed
CLI Tests / test-cli (push) Failing after 4s
Deploy to Testnet / deploy-testnet (push) Successful in 1m40s
Documentation Validation / validate-docs (push) Failing after 12s
Documentation Validation / validate-policies-strict (push) Successful in 4s
Integration Tests / test-service-integration (push) Successful in 2m42s
Package Tests / Python package - aitbc-agent-sdk (push) Failing after 34s
Package Tests / Python package - aitbc-core (push) Successful in 27s
Package Tests / Python package - aitbc-crypto (push) Successful in 13s
Package Tests / Python package - aitbc-sdk (push) Successful in 16s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 8s
Package Tests / JavaScript package - aitbc-token (push) Successful in 18s
Python Tests / test-python (push) Failing after 50s
Security Scanning / security-scan (push) Failing after 43s
Multi-Node Stress Testing / stress-test (push) Successful in 12s
Cross-Node Transaction Testing / transaction-test (push) Successful in 9s
refactor: move version to separate module and improve logging
- Created aitbc/_version.py with centralized version definition
- Updated aitbc/__init__.py to import __version__ from _version module
- Updated constants.py to use __version__ for PACKAGE_VERSION
- Replaced print() calls with logger in decorators.py, events.py, queue_manager.py, and state.py
- Added logger initialization using get_logger(__name__) in config.py, decorators.py, events.py, queue_manager.py, and state.py
- Added cli/commands
2026-05-11 20:12:01 +02:00

407 lines
12 KiB
Python

"""AI Service for job operations."""
from __future__ import annotations
import logging
from datetime import datetime, timezone
from typing import Annotated
from fastapi import FastAPI, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import Field, SQLModel, select
from .storage import get_session
from .domain.jobs import Job, JobState
logger = logging.getLogger(__name__)
app = FastAPI(
title="AITBC AI Service",
description="AI job operations service",
version="1.0.0"
)
@app.get("/health")
async def health():
"""Health check endpoint."""
return {"status": "healthy", "service": "ai-service"}
@app.get("/")
async def root():
"""Root endpoint."""
return {
"service": "AITBC AI Service",
"version": "1.0.0",
"status": "operational"
}
async def get_session_dep():
"""Dependency for database session."""
async with get_session() as session:
yield session
# Request/Response models
class JobCreate(SQLModel):
task_type: str
task_data: dict = Field(default_factory=dict)
payment_amount: float = 0.0
payment_currency: str = "aitbc_token"
priority: int = 0
class JobView(SQLModel):
id: str
client_id: str
task_type: str
state: str
created_at: datetime
started_at: datetime | None = None
completed_at: datetime | None = None
result: dict | None = None
error: str | None = None
payment_status: str = "none"
class JobResult(SQLModel):
id: str
result: dict | None = None
error: str | None = None
completed_at: datetime | None = None
receipt: dict | None = None
@app.post("/jobs", response_model=JobView, status_code=status.HTTP_201_CREATED)
async def submit_job(
session: Annotated[AsyncSession, Depends(get_session_dep)],
req: JobCreate,
client_id: str = "default_client",
):
"""Submit a job for execution."""
try:
job = Job(
client_id=client_id,
task_type=req.task_type,
task_data=req.task_data,
payment_amount=req.payment_amount,
priority=req.priority,
state=JobState.PENDING,
created_at=datetime.now(timezone.utc)
)
session.add(job)
await session.commit()
await session.refresh(job)
return JobView(
id=job.id,
client_id=job.client_id,
task_type=job.task_type,
state=job.state,
created_at=job.created_at,
started_at=job.started_at,
completed_at=job.completed_at,
result=job.result,
error=job.error,
payment_status=job.payment_status
)
except Exception as e:
logger.error(f"Submit job error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/jobs/{job_id}", response_model=JobView)
async def get_job(
session: Annotated[AsyncSession, Depends(get_session_dep)],
job_id: str,
client_id: str = "default_client",
):
"""Get job status."""
try:
result = await session.execute(
select(Job).where(Job.id == job_id, Job.client_id == client_id)
)
job = result.scalar_one_or_none()
if not job:
raise HTTPException(status_code=404, detail="Job not found")
return JobView(
id=job.id,
client_id=job.client_id,
task_type=job.task_type,
state=job.state,
created_at=job.created_at,
started_at=job.started_at,
completed_at=job.completed_at,
result=job.result,
error=job.error,
payment_status=job.payment_status
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Get job error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/jobs/{job_id}/result", response_model=JobResult)
async def get_job_result(
session: Annotated[AsyncSession, Depends(get_session_dep)],
job_id: str,
client_id: str = "default_client",
):
"""Get job result."""
try:
result = await session.execute(
select(Job).where(Job.id == job_id, Job.client_id == client_id)
)
job = result.scalar_one_or_none()
if not job:
raise HTTPException(status_code=404, detail="Job not found")
if job.state not in {JobState.COMPLETED, JobState.FAILED, JobState.CANCELED, JobState.EXPIRED}:
raise HTTPException(status_code=425, detail="Job not ready")
return JobResult(
id=job.id,
result=job.result,
error=job.error,
completed_at=job.completed_at,
receipt=job.receipt
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Get job result error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/jobs/{job_id}/cancel", response_model=JobView)
async def cancel_job(
session: Annotated[AsyncSession, Depends(get_session_dep)],
job_id: str,
client_id: str = "default_client",
):
"""Cancel a job."""
try:
result = await session.execute(
select(Job).where(Job.id == job_id, Job.client_id == client_id)
)
job = result.scalar_one_or_none()
if not job:
raise HTTPException(status_code=404, detail="Job not found")
if job.state in {JobState.COMPLETED, JobState.FAILED, JobState.CANCELED, JobState.EXPIRED}:
raise HTTPException(status_code=400, detail="Job already completed")
job.state = JobState.CANCELED
job.completed_at = datetime.now(timezone.utc)
await session.commit()
await session.refresh(job)
return JobView(
id=job.id,
client_id=job.client_id,
task_type=job.task_type,
state=job.state,
created_at=job.created_at,
started_at=job.started_at,
completed_at=job.completed_at,
result=job.result,
error=job.error,
payment_status=job.payment_status
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Cancel job error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/jobs")
async def list_jobs(
session: Annotated[AsyncSession, Depends(get_session_dep)],
client_id: str = "default_client",
limit: int = 10,
state: str | None = None,
):
"""List jobs with filtering."""
try:
query = select(Job).where(Job.client_id == client_id)
if state:
query = query.where(Job.state == state)
query = query.order_by(Job.created_at.desc()).limit(limit)
result = await session.execute(query)
jobs = result.scalars().all()
return {
"jobs": [
JobView(
id=job.id,
client_id=job.client_id,
task_type=job.task_type,
state=job.state,
created_at=job.created_at,
started_at=job.started_at,
completed_at=job.completed_at,
result=job.result,
error=job.error,
payment_status=job.payment_status
)
for job in jobs
],
"limit": limit,
"total": len(jobs)
}
except Exception as e:
logger.error(f"List jobs error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/multimodal/process")
async def process_multimodal(request: dict[str, Any]) -> dict[str, Any]:
"""Process multimodal AI requests (text, image, audio, video)"""
return {
"task_id": "multimodal_123",
"modality": request.get("modality", "text"),
"status": "processing",
"result": "multimodal processing initiated"
}
@app.post("/multimodal/benchmark")
async def benchmark_multimodal(request: dict[str, Any]) -> dict[str, Any]:
"""Benchmark multimodal AI performance"""
return {
"benchmark_id": "bench_456",
"modality": request.get("modality", "text"),
"performance_score": 95.5,
"latency_ms": 150,
"throughput": "high"
}
@app.get("/multimodal/agents")
async def list_multimodal_agents() -> dict[str, Any]:
"""List available multimodal AI agents"""
return {
"agents": [
{"id": "agent_1", "name": "Text-Image Agent", "capabilities": ["text", "image"]},
{"id": "agent_2", "name": "Audio-Video Agent", "capabilities": ["audio", "video"]},
],
"total": 2
}
@app.get("/multimodal/health")
async def multimodal_health() -> dict[str, Any]:
"""Multi-Modal Agent Service Health"""
return {
"status": "healthy",
"service": "multimodal-agent",
"timestamp": datetime.now(timezone.utc).isoformat(),
"capabilities": {
"text_processing": True,
"image_processing": True,
"audio_processing": True,
"video_processing": True,
"tabular_processing": True,
"graph_processing": True,
},
"performance": {
"text_processing_time": "0.02s",
"image_processing_time": "0.15s",
"audio_processing_time": "0.22s",
"video_processing_time": "0.35s",
"tabular_processing_time": "0.05s",
"graph_processing_time": "0.08s",
"average_accuracy": "94%",
}
}
@app.get("/multimodal/health/deep")
async def multimodal_deep_health() -> dict[str, Any]:
"""Deep Multi-Modal Service Health with modality tests"""
return {
"status": "healthy",
"service": "multimodal-agent",
"timestamp": datetime.now(timezone.utc).isoformat(),
"modality_tests": {
"text": {"status": "pass", "processing_time": "0.02s", "accuracy": "92%"},
"image": {"status": "pass", "processing_time": "0.15s", "accuracy": "87%"},
"audio": {"status": "pass", "processing_time": "0.22s", "accuracy": "89%"},
"video": {"status": "pass", "processing_time": "0.35s", "accuracy": "85%"},
},
"overall_health": "pass"
}
@app.post("/optimization/tune")
async def tune_optimization(request: dict[str, Any]) -> dict[str, Any]:
"""Tune AI model optimization parameters"""
return {
"tuning_id": "tune_789",
"model": request.get("model", "default"),
"parameters": {"learning_rate": 0.001, "batch_size": 32},
"status": "tuned"
}
@app.post("/optimization/predict")
async def predict_optimization(request: dict[str, Any]) -> dict[str, Any]:
"""Predict optimal model performance"""
return {
"prediction_id": "pred_101",
"model": request.get("model", "default"),
"expected_performance": "high",
"estimated_accuracy": 95.5
}
@app.get("/optimization/agents")
async def list_optimization_agents() -> dict[str, Any]:
"""List available optimization agents"""
return {
"agents": [
{"id": "opt_1", "name": "Gradient Descent Optimizer", "type": "gradient"},
{"id": "opt_2", "name": "Genetic Algorithm", "type": "evolutionary"},
],
"total": 2
}
@app.get("/optimization/health")
async def optimization_health() -> dict[str, Any]:
"""Optimization Service Health"""
return {
"status": "healthy",
"service": "modality-optimization",
"timestamp": datetime.now(timezone.utc).isoformat(),
"capabilities": {
"text_optimization": True,
"image_optimization": True,
"audio_optimization": True,
"video_optimization": True,
},
"performance": {
"optimization_speedup": "150x average",
"memory_reduction": "60% average",
"accuracy_retention": "95% average",
}
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8106)