fix(drafting): Drafting-Engine auf prod reparieren — RAG via ai-sdk + OVH-LLM-Kaskade
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / iace-gt-coverage (push) Has been skipped
CI / test-python-backend (push) Has been skipped
CI / test-python-document-crawler (push) Has been skipped
CI / test-python-dsms-gateway (push) Has been skipped
CI / detect-changes (push) Successful in 7s
CI / branch-name (push) Has been skipped
CI / guardrail-integrity (push) Has been skipped
CI / secret-scan (push) Has been skipped
CI / dep-audit (push) Has been skipped
CI / sbom-scan (push) Has been skipped
CI / build-sha-integrity (push) Successful in 5s
CI / validate-canonical-controls (push) Successful in 4s
CI / loc-budget (push) Successful in 17s
CI / go-lint (push) Has been skipped
CI / nodejs-build (push) Successful in 3m2s
CI / test-go (push) Has been skipped

Die Drafting-Engine (Dokument-Entwurf, v2-Pipeline, Validierung, Drafting-Chat,
Vendor-Vertragspruefung) war auf prod doppelt tot:
- RAG ueber bp-core-rag-service:8097 (existiert auf prod nicht)
- LLM ueber OLLAMA_URL/api/chat mit qwen2.5vl (prod = ollama-embed, kein Chat-Modell)

Fix (analog zum Compliance-Advisor):
- rag-query.ts -> ai-compliance-sdk /sdk/v1/rag/search (bge-m3, prod-erreichbar).
- Neue lib/sdk/drafting-engine/llm-cascade.ts: OVH/LiteLLM (gpt-oss-120b) zuerst,
  Ollama als Dev-Fallback; cascadeComplete (JSON) + cascadeStream. Das Backend nutzt
  OVH+JSON bereits erfolgreich auf prod (extract-datasheet).
- 5 Aufrufstellen (draft-helpers, draft-helpers-v2, validate, chat, vendor-review)
  auf die Kaskade umgestellt; keine direkten Ollama-Calls mehr.
- Tests: llm-cascade + rag-query aktualisiert.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-06-19 10:02:06 +02:00
parent cd3e0b15ad
commit 90a70c8404
9 changed files with 398 additions and 203 deletions
@@ -11,9 +11,7 @@ import { queryRAG } from '@/lib/sdk/drafting-engine/rag-query'
import { DOCUMENT_RAG_CONFIG } from '@/lib/sdk/drafting-engine/rag-config'
import { readSoulFile } from '@/lib/sdk/agents/soul-reader'
import type { ScopeDocumentType } from '@/lib/sdk/compliance-scope-types'
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://host.docker.internal:11434'
const LLM_MODEL = process.env.COMPLIANCE_LLM_MODEL || 'qwen2.5vl:32b'
import { cascadeStream } from '@/lib/sdk/drafting-engine/llm-cascade'
// Fallback SOUL prompt (used when .soul.md file is unavailable)
const FALLBACK_DRAFTING_PROMPT = `# Drafting Agent - Compliance-Dokumententwurf
@@ -81,66 +79,20 @@ export async function POST(request: NextRequest) {
]
// 4. Call LLM with streaming
const ollamaResponse = await fetch(`${OLLAMA_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: LLM_MODEL,
messages,
stream: true,
think: false,
options: {
temperature: mode === 'draft' ? 0.2 : 0.3,
num_predict: mode === 'draft' ? 16384 : 8192,
num_ctx: 8192,
},
}),
signal: AbortSignal.timeout(120000),
// 4. LLM-Kaskade (OVH -> Ollama) -> Plain-Text-Stream
const stream = await cascadeStream(messages, {
temperature: mode === 'draft' ? 0.2 : 0.3,
maxTokens: mode === 'draft' ? 16384 : 8192,
timeoutMs: 120000,
})
if (!ollamaResponse.ok) {
const errorText = await ollamaResponse.text()
console.error('LLM error:', ollamaResponse.status, errorText)
if (!stream) {
return NextResponse.json(
{ error: `LLM nicht erreichbar (Status ${ollamaResponse.status})` },
{ error: 'LLM nicht erreichbar (weder OVH noch Ollama)' },
{ status: 502 }
)
}
// 5. Stream response back
const encoder = new TextEncoder()
const stream = new ReadableStream({
async start(controller) {
const reader = ollamaResponse.body!.getReader()
const decoder = new TextDecoder()
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
const chunk = decoder.decode(value, { stream: true })
const lines = chunk.split('\n').filter((l) => l.trim())
for (const line of lines) {
try {
const json = JSON.parse(line)
if (json.message?.content) {
controller.enqueue(encoder.encode(json.message.content))
}
} catch {
// Partial JSON, skip
}
}
}
} catch (error) {
console.error('Stream error:', error)
} finally {
controller.close()
}
},
})
return new NextResponse(stream, {
headers: {
'Content-Type': 'text/plain; charset=utf-8',
@@ -17,6 +17,7 @@ import { executeRepairLoop, type ProseBlockOutput, type RepairAudit } from '@/li
import { computeChecksumSync, type CacheKeyParams } from '@/lib/sdk/drafting-engine/cache'
import { queryRAG } from '@/lib/sdk/drafting-engine/rag-query'
import { DOCUMENT_RAG_CONFIG } from '@/lib/sdk/drafting-engine/rag-config'
import { cascadeComplete } from '@/lib/sdk/drafting-engine/llm-cascade'
import {
constraintEnforcer,
proseCache,
@@ -27,7 +28,6 @@ import {
buildPromptForDocumentType,
} from './draft-helpers'
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://host.docker.internal:11434'
const LLM_MODEL = process.env.COMPLIANCE_LLM_MODEL || 'qwen2.5vl:32b'
// ============================================================================
@@ -171,29 +171,15 @@ Keine neuen Fakten erfinden — nur das Profil wuerdigen.`
}
export async function callOllama(systemPrompt: string, userPrompt: string): Promise<string> {
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: LLM_MODEL,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
],
stream: false,
think: false,
options: { temperature: 0.15, num_predict: 4096, num_ctx: 8192 },
format: 'json',
}),
signal: AbortSignal.timeout(120000),
})
if (!response.ok) {
throw new Error(`Ollama error: ${response.status}`)
}
const result = await response.json()
return result.message?.content || ''
const llm = await cascadeComplete(
[
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt },
],
{ json: true, temperature: 0.15, maxTokens: 8192, timeoutMs: 120000 },
)
if (!llm) throw new Error('LLM nicht erreichbar (weder OVH noch Ollama)')
return llm.content
}
export async function handleV2Draft(body: Record<string, unknown>): Promise<NextResponse> {
@@ -17,9 +17,7 @@ import { ConstraintEnforcer } from '@/lib/sdk/drafting-engine/constraint-enforce
import { ProseCacheManager } from '@/lib/sdk/drafting-engine/cache'
import { queryRAG } from '@/lib/sdk/drafting-engine/rag-query'
import { DOCUMENT_RAG_CONFIG } from '@/lib/sdk/drafting-engine/rag-config'
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://host.docker.internal:11434'
const LLM_MODEL = process.env.COMPLIANCE_LLM_MODEL || 'qwen2.5vl:32b'
import { cascadeComplete } from '@/lib/sdk/drafting-engine/llm-cascade'
export const constraintEnforcer = new ConstraintEnforcer()
export const proseCache = new ProseCacheManager({ maxEntries: 200, ttlHours: 24 })
@@ -105,29 +103,21 @@ export async function handleV1Draft(body: Record<string, unknown>): Promise<Next
{ role: 'user', content: draftPrompt },
]
const ollamaResponse = await fetch(`${OLLAMA_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: LLM_MODEL,
messages,
stream: false,
think: false,
options: { temperature: 0.15, num_predict: 16384, num_ctx: 8192 },
format: 'json',
}),
signal: AbortSignal.timeout(180000),
const llm = await cascadeComplete(messages, {
json: true,
temperature: 0.15,
maxTokens: 16384,
timeoutMs: 180000,
})
if (!ollamaResponse.ok) {
if (!llm) {
return NextResponse.json(
{ error: `LLM nicht erreichbar (Status ${ollamaResponse.status})` },
{ error: 'LLM nicht erreichbar (weder OVH noch Ollama)' },
{ status: 502 }
)
}
const result = await ollamaResponse.json()
const content = result.message?.content || ''
const content = llm.content
let sections: DraftSection[] = []
try {
@@ -153,7 +143,7 @@ export async function handleV1Draft(body: Record<string, unknown>): Promise<Next
return NextResponse.json({
draft,
constraintCheck,
tokensUsed: result.eval_count || 0,
tokensUsed: llm.tokensUsed,
} satisfies DraftResponse)
}
@@ -10,9 +10,7 @@ import { DOCUMENT_SCOPE_MATRIX_CORE, DOCUMENT_TYPE_LABELS, getDepthLevelNumeric
import type { ScopeDocumentType, ComplianceDepthLevel } from '@/lib/sdk/compliance-scope-types'
import type { ValidationContext, ValidationResult, ValidationFinding } from '@/lib/sdk/drafting-engine/types'
import { buildCrossCheckPrompt } from '@/lib/sdk/drafting-engine/prompts/validate-cross-check'
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://host.docker.internal:11434'
const LLM_MODEL = process.env.COMPLIANCE_LLM_MODEL || 'qwen2.5vl:32b'
import { cascadeComplete } from '@/lib/sdk/drafting-engine/llm-cascade'
/**
* Anti-Fake-Evidence: Verbotene Formulierungen
@@ -244,30 +242,17 @@ export async function POST(request: NextRequest) {
context: validationContext,
})
const ollamaResponse = await fetch(`${OLLAMA_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: LLM_MODEL,
messages: [
{
role: 'system',
content: 'Du bist ein DSGVO-Compliance-Validator. Antworte NUR im JSON-Format.',
},
{ role: 'user', content: crossCheckPrompt },
],
stream: false,
think: false,
options: { temperature: 0.1, num_predict: 8192, num_ctx: 8192 },
format: 'json',
}),
signal: AbortSignal.timeout(120000),
})
const llm = await cascadeComplete(
[
{ role: 'system', content: 'Du bist ein DSGVO-Compliance-Validator. Antworte NUR im JSON-Format.' },
{ role: 'user', content: crossCheckPrompt },
],
{ json: true, temperature: 0.1, maxTokens: 8192, timeoutMs: 120000 },
)
if (ollamaResponse.ok) {
const result = await ollamaResponse.json()
if (llm) {
try {
const parsed = JSON.parse(result.message?.content || '{}')
const parsed = JSON.parse(llm.content || '{}')
llmFindings = [
...(parsed.errors || []),
...(parsed.warnings || []),
@@ -6,9 +6,7 @@ import {
} from '@/lib/sdk/vendor-compliance'
import { queryRAG } from '@/lib/sdk/drafting-engine/rag-query'
import { transformAnalysisResponse } from '@/lib/sdk/vendor-compliance/contract-review/analyzer'
const OLLAMA_URL = process.env.OLLAMA_URL || 'http://host.docker.internal:11434'
const LLM_MODEL = process.env.COMPLIANCE_LLM_MODEL || 'qwen2.5vl:32b'
import { cascadeComplete } from '@/lib/sdk/drafting-engine/llm-cascade'
/**
* POST /api/sdk/v1/vendor-compliance/contracts/[id]/review
@@ -47,29 +45,19 @@ export async function POST(
}
// Call Ollama
const ollamaResponse = await fetch(`${OLLAMA_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: LLM_MODEL,
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: `Analysiere den folgenden Vertrag auf DSGVO-Konformitaet:\n\n${documentText}` },
],
stream: false,
options: { temperature: 0.1, num_predict: 16384 },
format: 'json',
}),
signal: AbortSignal.timeout(180000),
})
const llm = await cascadeComplete(
[
{ role: 'system', content: systemPrompt },
{ role: 'user', content: `Analysiere den folgenden Vertrag auf DSGVO-Konformitaet:\n\n${documentText}` },
],
{ json: true, temperature: 0.1, maxTokens: 16384, timeoutMs: 180000 },
)
if (!ollamaResponse.ok) {
throw new Error(`LLM nicht erreichbar (Status ${ollamaResponse.status})`)
if (!llm) {
throw new Error('LLM nicht erreichbar (weder OVH noch Ollama)')
}
const result = await ollamaResponse.json()
const content = result.message?.content || ''
const llmResponse = JSON.parse(content)
const llmResponse = JSON.parse(llm.content)
// Transform LLM response to typed findings
const analysisResult = transformAnalysisResponse(llmResponse, {