From fe0f078353c1f6ee621ceca793747fda84cd1b81 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sun, 19 Nov 2023 19:49:52 +0700 Subject: [PATCH] Feat ChatGPT LLM Api [Console Log] [Text Moderation] [Azure] [+] fix(openai.ts): fix parsing error in ChatGPTApi's message handler [+] feat(openai.ts): add logging for flagged categories in text moderation --- app/client/platforms/openai.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 8ea864692..5ca7d43b5 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -197,19 +197,21 @@ export class ChatGPTApi implements LLMApi { } const text = msg.data; try { - const json = JSON.parse(text) as { - choices: Array<{ - delta: { - content: string; - }; - }>; - }; - const delta = json.choices[0]?.delta?.content; + const json = JSON.parse(text); + const choices = json.choices as Array<{ delta: { content: string } }>; + const delta = choices[0]?.delta?.content; + const textmoderation = json?.prompt_filter_results; + if (delta) { remainText += delta; } + + if (textmoderation && textmoderation.length > 0 && ServiceProvider.Azure) { + const contentFilterResults = textmoderation[0]?.content_filter_results; + console.log(`[${ServiceProvider.Azure}] [Text Moderation] flagged categories result:`, contentFilterResults); + } } catch (e) { - console.error("[Request] parse error", text); + console.error("[Request] parse error", text, msg); } }, onclose() {