Files
breakpilot-compliance/ai-compliance-sdk/internal/api/handlers/rag_handlers.go
T
Benjamin Admin cd3e0b15ad
CI / detect-changes (push) Successful in 6s
CI / branch-name (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 / guardrail-integrity (push) Has been skipped
CI / build-sha-integrity (push) Successful in 7s
CI / validate-canonical-controls (push) Successful in 6s
CI / loc-budget (push) Successful in 19s
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / nodejs-build (push) Successful in 3m4s
CI / test-go (push) Successful in 58s
CI / iace-gt-coverage (push) Successful in 16s
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
fix(advisor): Compliance-Advisor auf prod reparieren — RAG via ai-sdk (bge-m3) + OVH-LLM
Der Floating-Compliance-Advisor war auf prod kaputt (502): RAG ging ueber
rag-service:8097 (auf prod nicht vorhanden) und der Chat ueber
OLLAMA_URL=ollama-embed (embedding-only, kein qwen2.5vl).

- RAG laeuft jetzt ueber die ai-compliance-sdk /sdk/v1/rag/search (bge-m3,
  prod-erreichbar) statt rag-service -> profitiert vom reicheren Embedding.
  (lib/sdk/agents/advisor-rag.ts)
- LLM-Kaskade: OVH/LiteLLM (gpt-oss-120b) zuerst, Ollama als Dev-Fallback.
  (lib/sdk/agents/advisor-llm.ts; OVH-Env via orca-infra admin-Block)
- ai-sdk: bp_compliance_recht in AllowedCollections ergaenzt (Whitelist war
  inkonsistent — die Fehlermeldung listete es bereits als erlaubt).
- Route auf die Module umgestellt (duenn); Controls-Augmentation unveraendert.
- Tests: advisor-rag + advisor-llm.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-19 09:22:44 +02:00

209 lines
6.0 KiB
Go

package handlers
import (
"net/http"
"strconv"
"github.com/breakpilot/ai-compliance-sdk/internal/ucca"
"github.com/gin-gonic/gin"
)
// RAGHandlers handles RAG search API endpoints.
type RAGHandlers struct {
ragClient *ucca.LegalRAGClient
corpusVersionStore *ucca.CorpusVersionStore
}
// NewRAGHandlers creates new RAG handlers.
func NewRAGHandlers(corpusVersionStore *ucca.CorpusVersionStore) *RAGHandlers {
return &RAGHandlers{
ragClient: ucca.NewLegalRAGClient(),
corpusVersionStore: corpusVersionStore,
}
}
// AllowedCollections is the whitelist of Qdrant collections that can be queried.
var AllowedCollections = map[string]bool{
"bp_compliance_ce": true,
"bp_compliance_gesetze": true,
"bp_compliance_datenschutz": true,
"bp_compliance_recht": true,
"bp_compliance_gdpr": true,
"bp_dsfa_corpus": true,
"bp_dsfa_templates": true,
"bp_dsfa_risks": true,
"bp_legal_templates": true,
"bp_iace_libraries": true,
"bp_iace_accident_stats": true,
"bp_iace_safety_kb": true,
"bp_iace_fmea_kb": true,
}
// SearchRequest represents a RAG search request.
type SearchRequest struct {
Query string `json:"query" binding:"required"`
Collection string `json:"collection,omitempty"`
Regulations []string `json:"regulations,omitempty"`
TopK int `json:"top_k,omitempty"`
}
// Search performs a semantic search across the compliance regulation corpus.
// POST /sdk/v1/rag/search
func (h *RAGHandlers) Search(c *gin.Context) {
var req SearchRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.TopK <= 0 || req.TopK > 20 {
req.TopK = 5
}
// Validate collection if specified
if req.Collection != "" {
if !AllowedCollections[req.Collection] {
c.JSON(http.StatusBadRequest, gin.H{"error": "Unknown collection: " + req.Collection + ". Allowed: bp_compliance_ce, bp_compliance_recht, bp_compliance_gesetze, bp_compliance_datenschutz, bp_dsfa_corpus, bp_legal_templates"})
return
}
}
results, err := h.ragClient.SearchCollection(c.Request.Context(), req.Collection, req.Query, req.Regulations, req.TopK)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "RAG search failed: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"query": req.Query,
"results": results,
"count": len(results),
})
}
// ListRegulations returns the list of available regulations in the corpus.
// GET /sdk/v1/rag/regulations
func (h *RAGHandlers) ListRegulations(c *gin.Context) {
regs := h.ragClient.ListAvailableRegulations()
// Optionally filter by category
category := c.Query("category")
if category != "" {
filtered := make([]ucca.CERegulationInfo, 0)
for _, r := range regs {
if r.Category == category {
filtered = append(filtered, r)
}
}
regs = filtered
}
c.JSON(http.StatusOK, gin.H{
"regulations": regs,
"count": len(regs),
})
}
// CorpusStatus returns the current version status of all RAG collections.
// GET /sdk/v1/rag/corpus-status
func (h *RAGHandlers) CorpusStatus(c *gin.Context) {
if h.corpusVersionStore == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "corpus version store not configured"})
return
}
versions, err := h.corpusVersionStore.GetAllLatestVersions(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch corpus versions: " + err.Error()})
return
}
collections := make(map[string]gin.H)
for _, v := range versions {
collections[v.CollectionName] = gin.H{
"id": v.ID,
"current_version": v.Version,
"documents_count": v.DocumentsCount,
"chunks_count": v.ChunksCount,
"regulations": v.Regulations,
"last_updated": v.CreatedAt,
"digest": v.Digest,
}
}
c.JSON(http.StatusOK, gin.H{
"collections": collections,
})
}
// CorpusVersionHistory returns the version history for a specific collection.
// GET /sdk/v1/rag/corpus-versions/:collection
func (h *RAGHandlers) CorpusVersionHistory(c *gin.Context) {
if h.corpusVersionStore == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "corpus version store not configured"})
return
}
collection := c.Param("collection")
if collection == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "collection name required"})
return
}
versions, err := h.corpusVersionStore.ListCorpusVersions(c.Request.Context(), collection)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch corpus versions: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"collection": collection,
"versions": versions,
"count": len(versions),
})
}
// HandleScrollChunks scrolls/lists all chunks in a Qdrant collection with pagination.
// GET /sdk/v1/rag/scroll?collection=...&offset=...&limit=...
func (h *RAGHandlers) HandleScrollChunks(c *gin.Context) {
collection := c.Query("collection")
if collection == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "query parameter 'collection' is required"})
return
}
if !AllowedCollections[collection] {
c.JSON(http.StatusBadRequest, gin.H{"error": "Unknown collection: " + collection})
return
}
// Parse limit (default 100, max 500)
limit := 100
if limitStr := c.Query("limit"); limitStr != "" {
parsed, err := strconv.Atoi(limitStr)
if err != nil || parsed < 1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "limit must be a positive integer"})
return
}
limit = parsed
}
if limit > 500 {
limit = 500
}
// Offset is optional (empty string = start from beginning)
offset := c.Query("offset")
chunks, nextOffset, err := h.ragClient.ScrollChunks(c.Request.Context(), collection, offset, limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "scroll failed: " + err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"chunks": chunks,
"next_offset": nextOffset,
"total": len(chunks),
})
}