support both fetch and stream mode

This commit is contained in:
2023-03-15 02:33:49 +08:00
parent a68e4b9dbd
commit 5eb070a62f
2 changed files with 62 additions and 28 deletions

View File

@@ -39,7 +39,7 @@ class Chat {
this.apiEndpoint = apiEndPoint;
}
_fetch() {
_fetch(stream = false) {
return fetch(this.apiEndpoint, {
method: "POST",
headers: {
@@ -52,7 +52,7 @@ class Chat {
{ role: "system", content: this.sysMessageContent },
...this.messages,
],
stream: true,
stream,
}),
});
}
@@ -102,11 +102,10 @@ class Chat {
}
completeWithSteam() {
this.total_tokens =
this.messages
.map((msg) => this.calculate_token_length(msg.content) + 20)
.reduce((a, v) => a + v);
return this._fetch();
this.total_tokens = this.messages
.map((msg) => this.calculate_token_length(msg.content) + 20)
.reduce((a, v) => a + v);
return this._fetch(true);
}
// https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them