Add fluent getters to Chat Completion

This commit is contained in:
maver 2023-06-01 18:50:33 +02:00
parent bc041d8fb3
commit febf4018b8
1 changed files with 15 additions and 0 deletions

View File

@ -2,24 +2,39 @@ import {countTokens} from "./openai.js";
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js"; import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
import {substituteParams} from "../script.js"; import {substituteParams} from "../script.js";
// OpenAI API chat message handling
// const map = [{identifier: 'example', message: {role: 'system', content: 'exampleContent'}}, ...];
const ChatCompletion = { const ChatCompletion = {
new() { new() {
return { return {
map: [], map: [],
add(identifier, message) { add(identifier, message) {
this.map.push({identifier: identifier, message: message}) this.map.push({identifier: identifier, message: message})
return this;
}, },
insertBefore(identifier, insertIdentifier, insert) { insertBefore(identifier, insertIdentifier, insert) {
const index = this.getMessageIndex(identifier); const index = this.getMessageIndex(identifier);
if (index === -1) throw new Error(`Identifier ${identifier} not found`);
this.map.splice(index, 0, {identifier: insertIdentifier, message: insert}); this.map.splice(index, 0, {identifier: insertIdentifier, message: insert});
return this;
}, },
insertAfter(identifier, insertIdentifier, insert) { insertAfter(identifier, insertIdentifier, insert) {
const index = this.getMessageIndex(identifier); const index = this.getMessageIndex(identifier);
if (index === -1) throw new Error(`Identifier ${identifier} not found`);
this.map.splice(index + 1, 0, {identifier: insertIdentifier, message: insert}); this.map.splice(index + 1, 0, {identifier: insertIdentifier, message: insert});
return this;
}, },
replace(identifier, replacement) { replace(identifier, replacement) {
const index = this.getMessageIndex(identifier); const index = this.getMessageIndex(identifier);
if (index === -1) throw new Error(`Identifier ${identifier} not found`);
this.map[index] = {identifier: identifier, message: replacement}; this.map[index] = {identifier: identifier, message: replacement};
return this;
},
remove(identifier) {
const index = this.getMessageIndex(identifier);
if (index === -1) throw new Error(`Identifier ${identifier} not found`);
this.map.splice(index, 1);
return this;
}, },
getMessageIndex(identifier) { getMessageIndex(identifier) {
const index = this.map.findIndex(message => message.identifier === identifier) const index = this.map.findIndex(message => message.identifier === identifier)