Cortex rework in progress
This commit is contained in:
@@ -1,63 +1,84 @@
|
||||
from fastapi import APIRouter
|
||||
# router.py
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List, Any
|
||||
|
||||
from reasoning.reasoning import reason_check
|
||||
from reasoning.reflection import reflect_notes
|
||||
from reasoning.refine import refine_answer
|
||||
from persona.speak import apply_persona
|
||||
from persona.speak import speak
|
||||
from ingest.intake_client import IntakeClient
|
||||
|
||||
router = APIRouter()
|
||||
# -----------------------------
|
||||
# Router (NOT FastAPI app)
|
||||
# -----------------------------
|
||||
cortex_router = APIRouter()
|
||||
|
||||
# Initialize Intake client once
|
||||
intake_client = IntakeClient()
|
||||
|
||||
|
||||
# ------------------------------------------------------
|
||||
# Request schema
|
||||
# ------------------------------------------------------
|
||||
# -----------------------------
|
||||
# Pydantic models
|
||||
# -----------------------------
|
||||
class ReasonRequest(BaseModel):
|
||||
session_id: Optional[str]
|
||||
session_id: str
|
||||
user_prompt: str
|
||||
temperature: float = 0.7
|
||||
temperature: float | None = None
|
||||
|
||||
|
||||
# ------------------------------------------------------
|
||||
# -----------------------------
|
||||
# /reason endpoint
|
||||
# ------------------------------------------------------
|
||||
@router.post("/reason")
|
||||
# -----------------------------
|
||||
@cortex_router.post("/reason")
|
||||
async def run_reason(req: ReasonRequest):
|
||||
|
||||
# 1. Summaries from Intake (context memory)
|
||||
intake = IntakeClient()
|
||||
intake_summary = await intake.get_context(req.session_id)
|
||||
# 1. Pull context from Intake
|
||||
try:
|
||||
intake_summary = await intake_client.get_context(req.session_id)
|
||||
except Exception:
|
||||
intake_summary = "(no context available)"
|
||||
|
||||
# 2. Internal reflection notes
|
||||
reflection = await reflect_notes(intake_summary, identity_block=None)
|
||||
reflection_notes: List[str] = reflection.get("notes", [])
|
||||
# 2. Reflection
|
||||
try:
|
||||
reflection = await reflect_notes(intake_summary, identity_block=None)
|
||||
reflection_notes = reflection.get("notes", [])
|
||||
except Exception:
|
||||
reflection_notes = []
|
||||
|
||||
# 3. Draft answer (weak, unfiltered)
|
||||
# 3. First-pass reasoning draft
|
||||
draft = await reason_check(
|
||||
user_prompt=req.user_prompt,
|
||||
req.user_prompt,
|
||||
identity_block=None,
|
||||
rag_block=None,
|
||||
reflection_notes=reflection_notes,
|
||||
reflection_notes=reflection_notes
|
||||
)
|
||||
|
||||
# 4. Refine the answer (structured self-correction)
|
||||
refined_packet: dict[str, Any] = refine_answer(
|
||||
# 4. Refinement
|
||||
result = refine_answer(
|
||||
draft_output=draft,
|
||||
reflection_notes=reflection_notes,
|
||||
identity_block=None,
|
||||
rag_block=None,
|
||||
)
|
||||
refined_text = refined_packet.get("final_output", draft)
|
||||
final_neutral = result["final_output"]
|
||||
|
||||
# 5. Persona styling (Lyra voice)
|
||||
final_output = apply_persona(refined_text)
|
||||
# 5. Persona layer
|
||||
persona_answer = await speak(final_neutral)
|
||||
|
||||
# 6. Return full bundle
|
||||
return {
|
||||
"draft": draft,
|
||||
"refined": refined_text,
|
||||
"final": final_output,
|
||||
"reflection_notes": reflection_notes,
|
||||
"neutral": final_neutral,
|
||||
"persona": persona_answer,
|
||||
"reflection": reflection_notes,
|
||||
"session_id": req.session_id,
|
||||
}
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Intake ingest passthrough
|
||||
# -----------------------------
|
||||
@cortex_router.post("/ingest")
|
||||
async def ingest_stub():
|
||||
return {"status": "ok"}
|
||||
|
||||
Reference in New Issue
Block a user