From b7d4e666000474d078f5e648c2e2eef0f247f2a6 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Wed, 3 Jun 2026 00:40:42 +0700 Subject: [PATCH] refactor: remove json_schema response format, simplify to json_object only --- .../src/modules/ai-moderation/llmClient.ts | 54 +++++------------- .../ai-moderation/llmModerationClient.ts | 56 +------------------ 2 files changed, 14 insertions(+), 96 deletions(-) diff --git a/services/discord-gateway/src/modules/ai-moderation/llmClient.ts b/services/discord-gateway/src/modules/ai-moderation/llmClient.ts index dcefc8d..8f1fb0b 100644 --- a/services/discord-gateway/src/modules/ai-moderation/llmClient.ts +++ b/services/discord-gateway/src/modules/ai-moderation/llmClient.ts @@ -56,15 +56,8 @@ export interface LlmCallOpts { temperature?: number; /** Top-p (defaults to 0.95). */ top_p?: number; - /** Force JSON output. When true, wraps schema in json_schema response_format. */ - jsonResponse?: - | { type: "json_object" } - | { - type: "json_schema"; - name: string; - schema: Record; - strict: boolean; - }; + /** Force JSON output via response_format: { type: "json_object" }. */ + jsonResponse?: { type: "json_object" }; /** Extra retries beyond DEFAULT_RETRIES (default 2). */ retries?: number; } @@ -91,39 +84,18 @@ export async function llmChat( retries = DEFAULT_RETRIES, } = opts; - const responseFormat: - | { type: "json_object" } - | { - type: "json_schema"; - json_schema: { - name: string; - schema: Record; - strict: boolean; - }; - } - | undefined = jsonResponse - ? jsonResponse.type === "json_schema" - ? { - type: "json_schema", - json_schema: { - name: jsonResponse.name, - schema: jsonResponse.schema, - strict: jsonResponse.strict, - }, - } - : jsonResponse - : undefined; + const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { + model, + messages, + temperature, + top_p, + max_tokens, + stream: false, + }; - const params: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = - { - model, - messages, - temperature, - top_p, - max_tokens, - stream: false, - ...(responseFormat ? { response_format: responseFormat } : {}), - } as OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming; + if (jsonResponse) { + (params as unknown as Record).response_format = jsonResponse; + } return retryWithBackoff( async () => { diff --git a/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts b/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts index 97de6bb..795233f 100644 --- a/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts +++ b/services/discord-gateway/src/modules/ai-moderation/llmModerationClient.ts @@ -161,55 +161,6 @@ function deriveRecommendedAction( return "review"; } -/** - * JSON Schema for OpenAI's response_format: { type: "json_schema" }. - * This enforces the exact structure the LLM must output (R2). - */ -const MODERATION_JSON_SCHEMA = { - type: "object", - properties: { - results: { - type: "array", - items: { - type: "object", - properties: { - message_id: { type: "string" }, - status: { type: "string", enum: ["clean", "warn", "flagged"] }, - flags: { type: "array", items: { type: "string" } }, - score: { type: "number", minimum: 0, maximum: 1 }, - analysis: { type: "string" }, - categories: { type: "array", items: { type: "string" } }, - severity: { - type: "string", - enum: ["none", "low", "medium", "high", "critical"], - }, - confidence: { type: "number", minimum: 0, maximum: 1 }, - recommended_action: { - type: "string", - enum: ["none", "monitor", "warn", "review", "delete", "escalate"], - }, - policy_version: { type: "string" }, - evidence: { type: "array", items: { type: "string" } }, - }, - required: [ - "message_id", - "status", - "flags", - "score", - "severity", - "confidence", - "recommended_action", - "policy_version", - "evidence", - "analysis", - ], - additionalProperties: false, - }, - }, - }, - required: ["results"], - additionalProperties: false, -}; /** * Helper to extract JSON from a potentially conversational or markdown-wrapped string. @@ -736,12 +687,7 @@ async function callModerationLLM( const completion = await llmChat({ messages: [{ role: "user", content }], max_tokens: 16384, - jsonResponse: { - type: "json_schema", - name: "moderation_result", - schema: MODERATION_JSON_SCHEMA, - strict: true, - }, + jsonResponse: { type: "json_object" }, retries: 0, });