GenAI Service Audit
Architecture Overview
The GenAI service in Open4Goods is a distributed system involving the Frontend, Front-API, and the Back-Office API (Core).
Flow Diagram
Quota Management
IP-based quotas are enforced at the Front-API level to protect the back-office from abuse.
- Service:
QuotaService - Configuration:
ReviewGenerationProperties(default: 3 requests per IP per 24h). - Enforcement:
ProductMappingServicechecksQuotaServicebefore calling theReviewGenerationClient. - Frontend:
AccountPrivacyCard.vueusesuseIpQuota.tsto display remaining quota to the user.
Prompt Engineering
The system uses a "POJO-oriented" prompt engineering approach, combining external templates with code-defined instructions.
- Template (
review-generation.yml): Defines the base System and User prompts. It uses Thymeleaf for variable substitution (e.g.,${PRODUCT_NAME},${sources}). - Code Instructions (
@AiGeneratedField): specific instructions for each field of theAiReviewobject are defined directly in the Java class using annotations.@AiGeneratedField(instruction = "Description du produit, 150 mots maximum") private String description; - Concatenation (
PromptService):- Calls
AiFieldScanner.getGenAiInstruction(type)to extract all annotated instructions. - Appends these instructions to the System Prompt:
"En complément du schéma JSON, voici les instructions concernant chaque champs que tu dois fournir…"
- Calls
Benefits & Risks
- Benefits: Keeps instructions close to the data model (Documentation as Code). Ensures the AI knows exactly what to generate for each JSON field.
- Risks:
- Localization: Hardcoded French text in
PromptServicelimits specific support to other languages. - Determinism: Field order depends on reflection (
getDeclaredFields), which isn't guaranteed deterministic without explicit sorting.
- Localization: Hardcoded French text in
ReviewGenerationService Analysis
The ReviewGenerationService orchestrates the complex flow of gathering context before calling the AI.
Workflow
- Check:
shouldGenerateReview(cache validity). - Preprocessing:
- Search: Google Search for Brand + Model.
- Fetch: Concurrent scraping of top results.
- Filter: Discard PDF, low token count, or preferred domain logic.
- Count: Accumulate tokens up to
maxTotalTokens.
- Prompt Prep:
- Injects
sources,tokens,PRODUCT_DETAILS. - Manually constructs the
ATTRIBUTESlist string.
- Injects
- Generation: Calls
PromptServicewith "review-generation" key. - Post-processing:
updateAiReviewReferences: Replaces[1]with HTML links.populateAttributes: Extracts dynamic attributes.