跳到主要内容

rewriteText

This function rewrites or replaces text. If no text or paragraph number is specified, the current paragraph is used by default.

Prompts

  • Rewrite
  • Rephrase sentence
  • Make the text more emotional
  • Rephrase
  • Rewrite in official style

Function registration

let func = new RegisteredFunction();
func.name = "rewriteText";
func.params = [
"parNumber (number): the paragraph number to change",
"prompt (string): instructions on how to change the text",
"showDifference (boolean): whether to show the difference between the original and new text, or just replace it",
"type (string): which part of the text to be rewritten (e.g., 'sentence' or 'paragraph')"
];
func.description = "Use this function when you asked to rewrite or replace some text. If text or paragraph number is not specified assume that we are working with the current paragraph.";

func.examples = [
"if you need to rewrite, respond with:\n" +
"[functionCalling (rewriteText)]: {\"prompt\": \"Rewrite\", \"type\" : \"paragraph\"}",

"If you need to rephrase current sentence, respond with:\n" +
"[functionCalling (rewriteText)]: {\"prompt\": \"rephrase sentence\", \"type\" : \"sentence\"}",

"If you need to rephrase current sentence and show difference, respond with:\n" +
"[functionCalling (rewriteText)]: {\"prompt\": \"rephrase sentence\", \"type\" : \"sentence\", \"showDifference\" : true}",

"if you need to change paragraph 2 to be more emotional, respond with:\n" +
"[functionCalling (rewriteText)]: {\"parNumber\": 2, \"prompt\": \"make the text more emotional\", \"type\" : \"paragraph\"}",

"if you need to rewrite the first paragraph, respond with:\n" +
"[functionCalling (rewriteText)]: {\"parNumber\": 1, \"prompt\": \"Rephrase \", \"type\" : \"paragraph\"}",

"if you need to rewrite the current paragraph to be more official, respond with:\n" +
"[functionCalling (rewriteText)]: {\"prompt\": \"Rewrite in official style\", \"type\" : \"paragraph\"}"
];

Parameters

NameTypeExampleDescription
parNumbernumber1The number of the paragraph to change.
promptstring"Rewrite"The instructions on how to change the text.
showDifferencebooleantrueSpecifies whether to show the difference between the original and new text, or just replace it.
typestring"paragraph"The part of the text to be rewritten (e.g., "sentence" or "paragraph").

Function execution

func.call = async function(params) {

let text = "";
if ("paragraph" === params.type)
{
Asc.scope.parNumber = params.parNumber;
text = await Asc.Editor.callCommand(function(){
let doc = Api.GetDocument();
let par = undefined === Asc.scope.parNumber ? doc.GetCurrentParagraph() : doc.GetElement(Asc.scope.parNumber - 1);
if (!par)
return "";
par.Select();
return par.GetText();
});
}
else // if ("sentence" === params.type)
{
text = await Asc.Editor.callCommand(function(){
return Api.GetDocument().GetCurrentSentence();
});
}

let argPromt = params.prompt + ":\n" + text + "\n Answer with only the new one sentence, no need of any explanations";

let requestEngine = AI.Request.create(AI.ActionType.Chat);
if (!requestEngine)
return;

await Asc.Editor.callMethod("StartAction", ["GroupActions"]);

let turnOffTrackChanges = false;
if (params.showDifference)
{
let isTrackChanges = await Asc.Editor.callCommand(function(){
return Api.GetDocument().IsTrackRevisions();
});

if (!isTrackChanges)
{
await Asc.Editor.callCommand(function(){
Api.GetDocument().SetTrackRevisions(true);
});
turnOffTrackChanges = true;
}
}

await Asc.Editor.callMethod("StartAction", ["Block", "AI (" + requestEngine.modelUI.name + ")"]);

let isSendedEndLongAction = false;
async function checkEndAction() {
if (!isSendedEndLongAction) {
await Asc.Editor.callMethod("EndAction", ["Block", "AI (" + requestEngine.modelUI.name + ")"]);
isSendedEndLongAction = true
}
}

let result = await requestEngine.chatRequest(argPromt, false, async function(data) {
if (!data)
return;
await checkEndAction();

if (text && "sentence" === params.type)
{
Asc.scope.data = data;
await Asc.Editor.callCommand(function(){
let doc = Api.GetDocument();
doc.ReplaceCurrentSentence("");
});
text = null;
}

await Asc.Library.PasteText(data);
});

await checkEndAction();

if (turnOffTrackChanges)
await Asc.Editor.callCommand(function(){return Api.GetDocument().SetTrackRevisions(false);});

await Asc.Editor.callMethod("EndAction", ["GroupActions"]);
};

return func;

Methods used: GetDocument, GetCurrentParagraph, GetElement, GetText, Select, GetCurrentSentence, IsTrackRevisions, SetTrackRevisions, ReplaceCurrentSentence, EndAction, StartAction, Asc.scope object

Result

rewriteText rewriteText