feat: add POST /api/precedent-analysis for research "Analiz modu"
Takes the user's described situation plus the top search results and asks the AI model for a short legal assessment. Auth-protected since it's a paid AI call.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { AuthenticatedRequest } from '../middleware/auth';
|
||||
|
||||
export const searchPrecedents = async (req: Request, res: Response) => {
|
||||
try {
|
||||
@@ -78,6 +79,69 @@ SADECE anahtar kelimeleri aralarına boşluk koyarak yaz. Noktalama işareti, a
|
||||
}
|
||||
};
|
||||
|
||||
interface PrecedentSummary {
|
||||
birimAdi?: string;
|
||||
mahkeme?: string;
|
||||
kararTarihiStr?: string;
|
||||
tarih?: string;
|
||||
esasNo?: string;
|
||||
esas_no?: string;
|
||||
kararNo?: string;
|
||||
karar_no?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
// Analiz modu: bulunan kararları kullanıcının anlattığı durumla birlikte AI'ye
|
||||
// göndererek kısa bir hukuki değerlendirme üretir. Maliyetli bir AI çağrısı olduğu
|
||||
// için requireAuth ile korunuyor (bkz. routes/precedent.routes.ts).
|
||||
export const analyzePrecedents = async (req: AuthenticatedRequest, res: Response) => {
|
||||
try {
|
||||
const { situation, results } = req.body as { situation?: string; results?: PrecedentSummary[] };
|
||||
if (!situation || !Array.isArray(results) || results.length === 0) {
|
||||
return res.status(400).json({ error: 'situation ve results zorunludur.' });
|
||||
}
|
||||
|
||||
const listText = results.slice(0, 8).map((r, i) => {
|
||||
const mahkeme = r.birimAdi || r.mahkeme || 'Bilinmeyen Mahkeme';
|
||||
const tarih = r.kararTarihiStr || r.tarih || '';
|
||||
const esas = r.esasNo || r.esas_no || '';
|
||||
const karar = r.kararNo || r.karar_no || '';
|
||||
return `${i + 1}. ${mahkeme} (${tarih}) — Esas: ${esas}, Karar: ${karar}`;
|
||||
}).join('\n');
|
||||
|
||||
const systemPrompt = 'Sen uzman bir Türk avukatısın. Kullanıcının anlattığı hukuki durumu, aşağıda listelenen emsal karar arama sonuçları ışığında kısaca değerlendir. Hangi kararların duruma daha yakın olabileceğini belirt ve olası bir hukuki değerlendirme sun. Emin olmadığın noktalarda bunu açıkça belirt, uydurma bilgi verme. Cevabın yaklaşık 150-250 kelime olsun.';
|
||||
const userPrompt = `DURUM:\n${String(situation).slice(0, 3000)}\n\nBULUNAN KARARLAR:\n${listText}`;
|
||||
|
||||
const aiModel = process.env.AI_MODEL || 'mizan-fixed';
|
||||
const runpodUrl = process.env.RUNPOD_API_BASE_URL || 'https://q190env94stwis-11434.proxy.runpod.net';
|
||||
|
||||
const response = await fetch(`${runpodUrl}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model: aiModel,
|
||||
messages: [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
{ role: 'user', content: userPrompt },
|
||||
],
|
||||
think: false,
|
||||
stream: false,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`AI Model request failed: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const analysis = data.message?.content || 'Analiz üretilemedi.';
|
||||
res.json({ analysis });
|
||||
} catch (error: any) {
|
||||
console.error('Analyze Precedents Error:', error);
|
||||
res.status(500).json({ error: error.message || 'Internal Server Error' });
|
||||
}
|
||||
};
|
||||
|
||||
export const getPrecedentDocument = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const documentId = String(req.params.documentId || '');
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { Router } from 'express';
|
||||
import { searchPrecedents, getPrecedentDocument } from '../controllers/precedent.controller';
|
||||
import { requireAuth } from '../middleware/auth';
|
||||
import { searchPrecedents, getPrecedentDocument, analyzePrecedents } from '../controllers/precedent.controller';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post('/search-precedents', searchPrecedents);
|
||||
router.get('/precedent-document/:documentId', getPrecedentDocument);
|
||||
router.post('/precedent-analysis', requireAuth, analyzePrecedents);
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user