Enable Chat Completion to create messages for itself

And return their token count
This commit is contained in:
maver 2023-06-01 18:51:05 +02:00
parent febf4018b8
commit 9beefca3f5
1 changed files with 20 additions and 0 deletions

View File

@ -40,6 +40,26 @@ const ChatCompletion = {
const index = this.map.findIndex(message => message.identifier === identifier) const index = this.map.findIndex(message => message.identifier === identifier)
return -1 === index ? false : index; return -1 === index ? false : index;
}, },
makeSystemMessage(content) {
return this.makeMessage('system', content);
},
makeUserMessage(content) {
return this.makeMessage('user', content);
},
makeAssistantMessage(content) {
return this.makeMessage('assistant', content);
},
makeMessage(role, content) {
return {role: role, content: content}
},
getPromptsWithTokenCount() {
return this.map.map((message) => {
return { identifier: message.identifier, calculated_tokens: message.message ? countTokens(message.message) : 0}
});
},
getTotalTokenCount() {
return this.getPromptsWithTokenCount().reduce((acc, message) => acc += message.calculated_tokens, 0)
},
getChat() { getChat() {
return this.map.reduce((chat, item) => { return this.map.reduce((chat, item) => {
if (!item || !item.message || (false === Array.isArray(item.message) && !item.message.content)) return chat; if (!item || !item.message || (false === Array.isArray(item.message) && !item.message.content)) return chat;