mirror of
https://github.com/ONLYOFFICE/onlyoffice.github.io.git
synced 2026-04-07 14:04:30 +08:00
Fix working with CustomFunctions & Fix system role in providers
This commit is contained in:
@ -20,6 +20,25 @@ function clearChatState() {
|
||||
window.localStorage.removeItem(key);
|
||||
}
|
||||
|
||||
async function GetOldCustomFunctions() {
|
||||
let data = await Asc.Editor.callMethod("GetCustomFunctions");
|
||||
let obj = {
|
||||
macrosArray : [],
|
||||
current : -1
|
||||
};
|
||||
if (data) {
|
||||
try {
|
||||
obj = JSON.parse(data);
|
||||
} catch (err) {
|
||||
obj = {
|
||||
macrosArray : [],
|
||||
current : -1
|
||||
};
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
window.Asc.plugin.init = async function() {
|
||||
initWithTranslate();
|
||||
clearChatState();
|
||||
@ -49,13 +68,10 @@ window.Asc.plugin.init = async function() {
|
||||
});
|
||||
|
||||
if ("cell" === window.Asc.plugin.info.editorType) {
|
||||
let CustomFunctions = {
|
||||
current : 0,
|
||||
macrosArray : [
|
||||
{
|
||||
guid : "e8ea2fb288054deaa6b82158c04dee37",
|
||||
name : "AI",
|
||||
value : "\
|
||||
let AIFunc = {
|
||||
guid : "e8ea2fb288054deaa6b82158c04dee37",
|
||||
name : "AI",
|
||||
value : "\
|
||||
(function()\n\
|
||||
{\n\
|
||||
/**\n\
|
||||
@ -65,7 +81,7 @@ window.Asc.plugin.init = async function() {
|
||||
* @returns {string} Answer value.\n\
|
||||
*/\n\
|
||||
async function AI(value) {\n\
|
||||
let systemMessage = \"As an Excel formula expert, your job is to provide advanced Excel formulas that perform complex calculations or data manipulations as described by the user. Keep your answers as brief as possible. If the user asks for formulas, return only the formula. If the user asks for something, answer briefly and only the result, without descriptions or reflections.\";\n\
|
||||
let systemMessage = \"As an Excel formula expert, your job is to provide advanced Excel formulas that perform complex calculations or data manipulations as described by the user. Keep your answers as brief as possible. If the user asks for formulas, return only the formula. If the user asks for something, answer briefly and only the result, without descriptions or reflections. If you received a request that is not based on Excel formulas, then simply answer the text request as briefly as possible, without descriptions or reflections\";\n\
|
||||
return new Promise(resolve => (function(){\n\
|
||||
Api.AI({ type : \"text\", data : [{role: \"system\", content: systemMessage}, {role:\"user\", content: value}] }, function(data){\n\
|
||||
if (data.error)\n\
|
||||
@ -87,10 +103,32 @@ window.Asc.plugin.init = async function() {
|
||||
}\n\
|
||||
Api.AddCustomFunction(AI);\n\
|
||||
})();"
|
||||
}
|
||||
]
|
||||
};
|
||||
await Asc.Editor.callMethod("SetCustomFunctions", [JSON.stringify(CustomFunctions)]);
|
||||
|
||||
let oldCF = await GetOldCustomFunctions();
|
||||
let isFound = false;
|
||||
let isUpdate = false;
|
||||
|
||||
for (let i = 0, len = oldCF.macrosArray.length; i < len; i++) {
|
||||
let item = oldCF.macrosArray[i];
|
||||
if (item.name === AIFunc.name) {
|
||||
isFound = true;
|
||||
|
||||
if (item.guid === AIFunc.guid) {
|
||||
if (item.value !== AIFunc.value) {
|
||||
isUpdate = true;
|
||||
item.value = AIFunc.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isFound) {
|
||||
oldCF.macrosArray.push(AIFunc);
|
||||
isUpdate = true;
|
||||
}
|
||||
|
||||
if (isUpdate)
|
||||
await Asc.Editor.callMethod("SetCustomFunctions", [JSON.stringify(oldCF)]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -49,4 +49,13 @@ class Provider extends AI.Provider {
|
||||
return headers;
|
||||
}
|
||||
|
||||
getChatCompletions(message, model) {
|
||||
let systemPrompt = this.getSystemMessage(message, true);
|
||||
let result = super.getChatCompletions(message, model);
|
||||
if (systemPrompt !== "") {
|
||||
result.system = systemPrompt;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -82,6 +82,10 @@ class Provider extends AI.Provider {
|
||||
};
|
||||
if (rec.role === "assistant")
|
||||
rec.role = "model";
|
||||
else if (rec.role === "system") {
|
||||
body.system_instruction = rec;
|
||||
continue;
|
||||
}
|
||||
body.contents.push(rec);
|
||||
}
|
||||
return body;
|
||||
|
||||
@ -259,6 +259,28 @@
|
||||
this.modelsUI.push(modelUI);
|
||||
}
|
||||
}
|
||||
|
||||
getSystemMessage(message, isRemove) {
|
||||
let messages = message.messages;
|
||||
let isFound = false;
|
||||
if (!messages)
|
||||
return "";
|
||||
let result = "";
|
||||
for (let i = 0; i < messages.length; ++i) {
|
||||
if (messages[i].role === "system") {
|
||||
if (isFound) {
|
||||
messages.splice(i, 1);
|
||||
} else {
|
||||
isFound = true;
|
||||
result = messages[i].content;
|
||||
if (isRemove === true) {
|
||||
messages.splice(i, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
window.AI.Provider = Provider;
|
||||
|
||||
Reference in New Issue
Block a user