fix: ignore empty system message

This commit is contained in:
2023-10-24 22:53:31 +08:00
parent aba4e42202
commit e1172600c1

View File

@@ -54,7 +54,7 @@ class Chat {
constructor( constructor(
OPENAI_API_KEY: string | undefined, OPENAI_API_KEY: string | undefined,
{ {
systemMessage = "你是一个有用的人工智能助理", systemMessage = "",
max_tokens = 4096, max_tokens = 4096,
tokens_margin = 1024, tokens_margin = 1024,
apiEndPoint = "https://api.openai.com/v1/chat/completions", apiEndPoint = "https://api.openai.com/v1/chat/completions",
@@ -106,6 +106,11 @@ class Chat {
); );
} }
} }
const messages = [];
if (this.sysMessageContent.trim()) {
messages.push({ role: "system", content: this.sysMessageContent });
}
messages.push(...this.messages);
return fetch(this.apiEndpoint, { return fetch(this.apiEndpoint, {
method: "POST", method: "POST",
headers: { headers: {
@@ -114,10 +119,7 @@ class Chat {
}, },
body: JSON.stringify({ body: JSON.stringify({
model: this.model, model: this.model,
messages: [ messages,
{ role: "system", content: this.sysMessageContent },
...this.messages,
],
stream, stream,
temperature: this.temperature, temperature: this.temperature,
top_p: this.top_p, top_p: this.top_p,