Spaces:
Running
Running
axeronis commited on
Commit ·
1f1df4a
1
Parent(s): f42875b
feat: Quick correction endpoint, demo-chat saves corrections to DB
Browse files
backend/app/api/ai_training.py
CHANGED
|
@@ -424,6 +424,52 @@ async def delete_learning_rule(
|
|
| 424 |
return None
|
| 425 |
|
| 426 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
# ============================================================================
|
| 428 |
# Stats Endpoints
|
| 429 |
# ============================================================================
|
|
|
|
| 424 |
return None
|
| 425 |
|
| 426 |
|
| 427 |
+
# ============================================================================
|
| 428 |
+
# Quick Corrections (from Demo Chat)
|
| 429 |
+
# ============================================================================
|
| 430 |
+
|
| 431 |
+
class QuickCorrectionRequest(BaseModel):
|
| 432 |
+
"""Simple correction from demo chat"""
|
| 433 |
+
correction: str
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
@router.post("/corrections/quick", status_code=status.HTTP_201_CREATED)
|
| 437 |
+
async def add_quick_correction(
|
| 438 |
+
request: QuickCorrectionRequest,
|
| 439 |
+
current_user: User = Depends(get_current_user),
|
| 440 |
+
session: AsyncSession = Depends(get_async_session)
|
| 441 |
+
):
|
| 442 |
+
"""
|
| 443 |
+
Quickly add a correction/rule for AI from demo chat.
|
| 444 |
+
Simplified endpoint - just the correction text.
|
| 445 |
+
"""
|
| 446 |
+
if not request.correction.strip():
|
| 447 |
+
raise HTTPException(status_code=400, detail="Correction text is required")
|
| 448 |
+
|
| 449 |
+
# Create learning rule
|
| 450 |
+
rule = AILearningRule(
|
| 451 |
+
tenant_id=current_user.tenant_id,
|
| 452 |
+
rule_name=f"Правка: {request.correction[:30]}...",
|
| 453 |
+
rule_description=request.correction,
|
| 454 |
+
trigger_keywords=None,
|
| 455 |
+
apply_to_category=None,
|
| 456 |
+
instruction=request.correction,
|
| 457 |
+
priority=8, # High priority for user corrections
|
| 458 |
+
source="demo_chat",
|
| 459 |
+
created_by_user_id=current_user.id
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
+
session.add(rule)
|
| 463 |
+
await session.commit()
|
| 464 |
+
await session.refresh(rule)
|
| 465 |
+
|
| 466 |
+
return {
|
| 467 |
+
"ok": True,
|
| 468 |
+
"id": str(rule.id),
|
| 469 |
+
"message": "Правка сохранена"
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
|
| 473 |
# ============================================================================
|
| 474 |
# Stats Endpoints
|
| 475 |
# ============================================================================
|
frontend/app/demo-chat/page.tsx
CHANGED
|
@@ -46,14 +46,29 @@ export default function DemoChatPage() {
|
|
| 46 |
if (userInput.startsWith('/правка:') || userInput.startsWith('/fix:')) {
|
| 47 |
const correction = userInput.replace(/^\/(правка|fix):?\s*/i, '').trim()
|
| 48 |
if (correction) {
|
|
|
|
| 49 |
setCorrections(prev => [...prev, correction])
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
return
|
| 59 |
}
|
|
|
|
| 46 |
if (userInput.startsWith('/правка:') || userInput.startsWith('/fix:')) {
|
| 47 |
const correction = userInput.replace(/^\/(правка|fix):?\s*/i, '').trim()
|
| 48 |
if (correction) {
|
| 49 |
+
// Save to local state for this session
|
| 50 |
setCorrections(prev => [...prev, correction])
|
| 51 |
+
|
| 52 |
+
// Also save to database for persistence
|
| 53 |
+
try {
|
| 54 |
+
await api.post('/api/v1/ai-training/corrections/quick', { correction })
|
| 55 |
+
setMessages(prev => [...prev, {
|
| 56 |
+
id: crypto.randomUUID(),
|
| 57 |
+
role: 'ai',
|
| 58 |
+
content: `✅ Правка сохранена: "${correction}"\n\n💾 Сохранено в настройках AI. Теперь бот будет учитывать это в реальных диалогах.\n\nВсего правок в этой сессии: ${corrections.length + 1}`,
|
| 59 |
+
timestamp: new Date()
|
| 60 |
+
}])
|
| 61 |
+
toast.success('Правка сохранена в настройки AI!')
|
| 62 |
+
} catch (error) {
|
| 63 |
+
// Still apply locally even if DB save fails
|
| 64 |
+
setMessages(prev => [...prev, {
|
| 65 |
+
id: crypto.randomUUID(),
|
| 66 |
+
role: 'ai',
|
| 67 |
+
content: `⚠️ Правка применена локально: "${correction}"\n\nНе удалось сохранить в БД, но я учту это в текущей сессии.`,
|
| 68 |
+
timestamp: new Date()
|
| 69 |
+
}])
|
| 70 |
+
toast.warning('Правка применена локально')
|
| 71 |
+
}
|
| 72 |
}
|
| 73 |
return
|
| 74 |
}
|