017c9b3c12
ai-sdk (legal_rag_client/scroll/types) liest die gepinnten Spec-Felder
article_label/regulation_code/article/paragraph/sub/citation_style/is_recital
mit Fallback auf alt-ingestierte Chunks (regulation_id, section); neuer getBool-Helfer.
Advisor + Drafting-Engine bilden die Quellenzeile primaer aus article_label
("BDSG § 38 Abs. 1"), sonst aus den strukturierten Feldern. 17 Tests gruen, tsc sauber.
Vertrag: docs-src/development/rag_reingest_spec.md (§2/§7). Deploy an den Re-Ingest
gekoppelt — neue Felder sind bis dahin leer (graceful Fallback).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
/**
|
|
* Shared RAG query utility for the Drafting Engine.
|
|
*
|
|
* Fragt die ai-compliance-sdk (`/sdk/v1/rag/search`, bge-m3) nach Rechtskontext.
|
|
* Frueher: bp-core-rag-service:8097 — der existiert auf prod NICHT (nur macmini/dev),
|
|
* dadurch lieferte die Drafting-Engine dort keinen RAG-Kontext. Die ai-sdk embeddet
|
|
* mit bge-m3 und ist prod-erreichbar. Genutzt von draft-, chat- und vendor-review-Routes.
|
|
*/
|
|
|
|
const SDK_URL =
|
|
process.env.SDK_API_URL || process.env.SDK_URL || 'http://ai-compliance-sdk:8090'
|
|
const DEFAULT_USER = '00000000-0000-0000-0000-000000000001'
|
|
const DEFAULT_TENANT =
|
|
process.env.DEFAULT_TENANT_ID || '9282a473-5c95-4b3a-bf78-0ecc0ec71d3e'
|
|
|
|
interface SdkRagResult {
|
|
text?: string
|
|
regulation_code?: string
|
|
regulation_name?: string
|
|
regulation_short?: string
|
|
article_label?: string
|
|
article?: string
|
|
paragraph?: string
|
|
sub?: string
|
|
// Rueckwaerts-kompatibel, falls eine Quelle noch das alte rag-service-Format liefert:
|
|
content?: string
|
|
source_name?: string
|
|
source_code?: string
|
|
}
|
|
|
|
/**
|
|
* Query the RAG corpus for relevant legal documents.
|
|
*
|
|
* @param query - The search query (e.g. "Art. 35 DSGVO Risikobewertung")
|
|
* @param topK - Number of results to return (default: 3)
|
|
* @param collection - Optional RAG collection name (e.g. "bp_dsfa_corpus")
|
|
* @returns Formatted string of legal context, or empty string on error
|
|
*/
|
|
export async function queryRAG(query: string, topK = 3, collection?: string): Promise<string> {
|
|
try {
|
|
const body: Record<string, unknown> = { query, top_k: topK }
|
|
if (collection) body.collection = collection
|
|
|
|
const res = await fetch(`${SDK_URL}/sdk/v1/rag/search`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-User-ID': DEFAULT_USER,
|
|
'X-Tenant-ID': DEFAULT_TENANT,
|
|
},
|
|
body: JSON.stringify(body),
|
|
signal: AbortSignal.timeout(10000),
|
|
})
|
|
|
|
if (!res.ok) return ''
|
|
|
|
const data = await res.json()
|
|
const results: SdkRagResult[] = data.results || []
|
|
if (results.length === 0) return ''
|
|
|
|
return results
|
|
.map((r, i) => {
|
|
const base =
|
|
r.regulation_short || r.regulation_name || r.regulation_code || r.source_name || r.source_code
|
|
// article_label = fertig formatierte Fundstelle aus der Ingestion ("BDSG § 38 Abs. 1");
|
|
// Fallback baut sie aus den strukturierten Feldern. Siehe rag_reingest_spec.md §2/§7.
|
|
const source =
|
|
(r.article_label && r.article_label.trim()) ||
|
|
[base, r.article, r.paragraph, r.sub].filter(Boolean).join(' ') ||
|
|
'Unbekannt'
|
|
const content = r.text || r.content || ''
|
|
return `[Quelle ${i + 1}: ${source}]\n${content}`
|
|
})
|
|
.join('\n\n---\n\n')
|
|
} catch {
|
|
return ''
|
|
}
|
|
}
|