- Add mcp_server package with 7 tools proxying to FastAPI: - council_query (full 3-stage process) - council_stage1_collect, stage2_rank, stage3_synthesize - council_conversation_create, list, get - Add individual stage endpoints to FastAPI (/api/council/stage1, stage2, stage3) - Update council models to use valid OpenRouter identifiers - Add mcp>=1.0.0 dependency
259 lines
8.3 KiB
Python
259 lines
8.3 KiB
Python
"""FastAPI backend for LLM Council."""
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import StreamingResponse
|
|
from pydantic import BaseModel
|
|
from typing import List, Dict, Any
|
|
import uuid
|
|
import json
|
|
import asyncio
|
|
|
|
from . import storage
|
|
from .council import run_full_council, generate_conversation_title, stage1_collect_responses, stage2_collect_rankings, stage3_synthesize_final, calculate_aggregate_rankings
|
|
|
|
app = FastAPI(title="LLM Council API")
|
|
|
|
# Enable CORS for local development
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["http://localhost:5173", "http://localhost:3000"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
class CreateConversationRequest(BaseModel):
|
|
"""Request to create a new conversation."""
|
|
pass
|
|
|
|
|
|
class SendMessageRequest(BaseModel):
|
|
"""Request to send a message in a conversation."""
|
|
content: str
|
|
|
|
|
|
class ConversationMetadata(BaseModel):
|
|
"""Conversation metadata for list view."""
|
|
id: str
|
|
created_at: str
|
|
title: str
|
|
message_count: int
|
|
|
|
|
|
class Conversation(BaseModel):
|
|
"""Full conversation with all messages."""
|
|
id: str
|
|
created_at: str
|
|
title: str
|
|
messages: List[Dict[str, Any]]
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Health check endpoint."""
|
|
return {"status": "ok", "service": "LLM Council API"}
|
|
|
|
|
|
@app.get("/api/conversations", response_model=List[ConversationMetadata])
|
|
async def list_conversations():
|
|
"""List all conversations (metadata only)."""
|
|
return storage.list_conversations()
|
|
|
|
|
|
@app.post("/api/conversations", response_model=Conversation)
|
|
async def create_conversation(request: CreateConversationRequest):
|
|
"""Create a new conversation."""
|
|
conversation_id = str(uuid.uuid4())
|
|
conversation = storage.create_conversation(conversation_id)
|
|
return conversation
|
|
|
|
|
|
@app.get("/api/conversations/{conversation_id}", response_model=Conversation)
|
|
async def get_conversation(conversation_id: str):
|
|
"""Get a specific conversation with all its messages."""
|
|
conversation = storage.get_conversation(conversation_id)
|
|
if conversation is None:
|
|
raise HTTPException(status_code=404, detail="Conversation not found")
|
|
return conversation
|
|
|
|
|
|
@app.post("/api/conversations/{conversation_id}/message")
|
|
async def send_message(conversation_id: str, request: SendMessageRequest):
|
|
"""
|
|
Send a message and run the 3-stage council process.
|
|
Returns the complete response with all stages.
|
|
"""
|
|
# Check if conversation exists
|
|
conversation = storage.get_conversation(conversation_id)
|
|
if conversation is None:
|
|
raise HTTPException(status_code=404, detail="Conversation not found")
|
|
|
|
# Check if this is the first message
|
|
is_first_message = len(conversation["messages"]) == 0
|
|
|
|
# Add user message
|
|
storage.add_user_message(conversation_id, request.content)
|
|
|
|
# If this is the first message, generate a title
|
|
if is_first_message:
|
|
title = await generate_conversation_title(request.content)
|
|
storage.update_conversation_title(conversation_id, title)
|
|
|
|
# Run the 3-stage council process
|
|
stage1_results, stage2_results, stage3_result, metadata = await run_full_council(
|
|
request.content
|
|
)
|
|
|
|
# Add assistant message with all stages
|
|
storage.add_assistant_message(
|
|
conversation_id,
|
|
stage1_results,
|
|
stage2_results,
|
|
stage3_result
|
|
)
|
|
|
|
# Return the complete response with metadata
|
|
return {
|
|
"stage1": stage1_results,
|
|
"stage2": stage2_results,
|
|
"stage3": stage3_result,
|
|
"metadata": metadata
|
|
}
|
|
|
|
|
|
@app.post("/api/conversations/{conversation_id}/message/stream")
|
|
async def send_message_stream(conversation_id: str, request: SendMessageRequest):
|
|
"""
|
|
Send a message and stream the 3-stage council process.
|
|
Returns Server-Sent Events as each stage completes.
|
|
"""
|
|
# Check if conversation exists
|
|
conversation = storage.get_conversation(conversation_id)
|
|
if conversation is None:
|
|
raise HTTPException(status_code=404, detail="Conversation not found")
|
|
|
|
# Check if this is the first message
|
|
is_first_message = len(conversation["messages"]) == 0
|
|
|
|
async def event_generator():
|
|
try:
|
|
# Add user message
|
|
storage.add_user_message(conversation_id, request.content)
|
|
|
|
# Start title generation in parallel (don't await yet)
|
|
title_task = None
|
|
if is_first_message:
|
|
title_task = asyncio.create_task(generate_conversation_title(request.content))
|
|
|
|
# Stage 1: Collect responses
|
|
yield f"data: {json.dumps({'type': 'stage1_start'})}\n\n"
|
|
stage1_results = await stage1_collect_responses(request.content)
|
|
yield f"data: {json.dumps({'type': 'stage1_complete', 'data': stage1_results})}\n\n"
|
|
|
|
# Stage 2: Collect rankings
|
|
yield f"data: {json.dumps({'type': 'stage2_start'})}\n\n"
|
|
stage2_results, label_to_model = await stage2_collect_rankings(request.content, stage1_results)
|
|
aggregate_rankings = calculate_aggregate_rankings(stage2_results, label_to_model)
|
|
yield f"data: {json.dumps({'type': 'stage2_complete', 'data': stage2_results, 'metadata': {'label_to_model': label_to_model, 'aggregate_rankings': aggregate_rankings}})}\n\n"
|
|
|
|
# Stage 3: Synthesize final answer
|
|
yield f"data: {json.dumps({'type': 'stage3_start'})}\n\n"
|
|
stage3_result = await stage3_synthesize_final(request.content, stage1_results, stage2_results)
|
|
yield f"data: {json.dumps({'type': 'stage3_complete', 'data': stage3_result})}\n\n"
|
|
|
|
# Wait for title generation if it was started
|
|
if title_task:
|
|
title = await title_task
|
|
storage.update_conversation_title(conversation_id, title)
|
|
yield f"data: {json.dumps({'type': 'title_complete', 'data': {'title': title}})}\n\n"
|
|
|
|
# Save complete assistant message
|
|
storage.add_assistant_message(
|
|
conversation_id,
|
|
stage1_results,
|
|
stage2_results,
|
|
stage3_result
|
|
)
|
|
|
|
# Send completion event
|
|
yield f"data: {json.dumps({'type': 'complete'})}\n\n"
|
|
|
|
except Exception as e:
|
|
# Send error event
|
|
yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n"
|
|
|
|
return StreamingResponse(
|
|
event_generator(),
|
|
media_type="text/event-stream",
|
|
headers={
|
|
"Cache-Control": "no-cache",
|
|
"Connection": "keep-alive",
|
|
}
|
|
)
|
|
|
|
|
|
# ============================================================================
|
|
# INDIVIDUAL STAGE ENDPOINTS (for MCP granular control)
|
|
# ============================================================================
|
|
|
|
class Stage1Request(BaseModel):
|
|
"""Request for Stage 1."""
|
|
query: str
|
|
|
|
|
|
class Stage2Request(BaseModel):
|
|
"""Request for Stage 2."""
|
|
query: str
|
|
stage1_results: List[Dict[str, Any]]
|
|
|
|
|
|
class Stage3Request(BaseModel):
|
|
"""Request for Stage 3."""
|
|
query: str
|
|
stage1_results: List[Dict[str, Any]]
|
|
stage2_results: List[Dict[str, Any]]
|
|
|
|
|
|
@app.post("/api/council/stage1")
|
|
async def run_stage1(request: Stage1Request):
|
|
"""
|
|
Run Stage 1 independently - collect individual responses from all council models.
|
|
"""
|
|
results = await stage1_collect_responses(request.query)
|
|
return results
|
|
|
|
|
|
@app.post("/api/council/stage2")
|
|
async def run_stage2(request: Stage2Request):
|
|
"""
|
|
Run Stage 2 independently - collect rankings with anonymization.
|
|
"""
|
|
stage2_results, label_to_model = await stage2_collect_rankings(
|
|
request.query, request.stage1_results
|
|
)
|
|
aggregate_rankings = calculate_aggregate_rankings(stage2_results, label_to_model)
|
|
|
|
return {
|
|
"rankings": stage2_results,
|
|
"label_to_model": label_to_model,
|
|
"aggregate_rankings": aggregate_rankings
|
|
}
|
|
|
|
|
|
@app.post("/api/council/stage3")
|
|
async def run_stage3(request: Stage3Request):
|
|
"""
|
|
Run Stage 3 independently - chairman synthesis.
|
|
"""
|
|
result = await stage3_synthesize_final(
|
|
request.query, request.stage1_results, request.stage2_results
|
|
)
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8001)
|