From ab5eab1a9b95924c520ef6ce31fd34eccaebae51 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 13 Jul 2023 18:18:08 +0800 Subject: [PATCH] chatgpt.ts add role type check --- src/chatgpt.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/chatgpt.ts b/src/chatgpt.ts index c384e0e..373a364 100644 --- a/src/chatgpt.ts +++ b/src/chatgpt.ts @@ -83,6 +83,29 @@ class Chat { } _fetch(stream = false) { + // perform role type check + let hasNonSystemMessage = false; + for (const msg of this.messages) { + if (msg.role === "system" && !hasNonSystemMessage) { + continue; + } + if (!hasNonSystemMessage) { + hasNonSystemMessage = true; + continue; + } + if (msg.role === "system") { + console.log( + "Warning: detected system message in the middle of history" + ); + } + } + for (const msg of this.messages) { + if (msg.name && msg.role !== "system") { + console.log( + "Warning: detected message where name field set but role is system" + ); + } + } return fetch(this.apiEndpoint, { method: "POST", headers: {