跳到主要内容

生成话题标签

此函数根据选中的文本或当前光标所在的单词生成相关话题标签。

提示词

  • 生成话题标签
  • 生成社交媒体话题标签

函数注册

let func = new RegisteredFunction({
name: "generateHashtags",
description:
"Use this function if you need to generate relevant hashtags for the selected text or current word.",
parameters: {
type: "object",
properties: {
prompt: {
type: "string",
description:
"Instruction for the AI, for example: 'Generate hashtags for this text.'",
},
count: {
type: "number",
description: "How many hashtags to generate (default is 5)",
},
},
required: ["prompt"],
},
examples: [
{
prompt: "Generate hashtags for this text.",
arguments: { prompt: "Generate hashtags for this text." },
},
{
prompt: "Generate 10 hashtags for the selected text.",
arguments: { prompt: "Generate hashtags for this text.", count: 10 },
},
{
prompt: "Create 3 hashtags for this paragraph.",
arguments: { prompt: "Create hashtags for this paragraph.", count: 3 },
},
],
});

参数

NameTypeExampleDescription
promptstring"Generate hashtags"向 AI 发出的话题标签生成指令。
countnumber5要生成的话题标签数量。
categorystring"LinkedIn"要生成的话题标签类型。

函数执行

func.call = async function (params) {
let count = params.count || 5;

let text = await Asc.Editor.callCommand(function () {
let doc = Api.GetDocument();
let range = doc.GetRangeBySelect();
let txt = range ? range.GetText() : "";

if (!txt) {
txt = doc.GetCurrentWord();
doc.SelectCurrentWord();
}

return txt;
});

if (!text || text.trim().length === 0) return;

let argPrompt =
params.prompt +
":\n" +
"Text:\n" +
text +
"\n" +
"Generate " +
count +
" short and relevant hashtags. " +
"Output hashtags only, separated by spaces.";

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

await Asc.Editor.callMethod("StartAction", ["GroupActions"]);
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 resultText = "";

await requestEngine.chatRequest(argPrompt, false, async function (data) {
if (!data) return;
resultText += data;
});

await checkEndAction();

resultText = resultText.replace(/\s+/g, " ").trim();

if (resultText) {
Asc.scope.text = resultText;
await Asc.Editor.callCommand(function () {
let doc = Api.GetDocument();
doc.MoveCursorToEnd();
let par = Api.CreateParagraph();
par.AddText(Asc.scope.text);
doc.Push(par);
});
}

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

return func;

使用的方法:GetDocument, GetRangeBySelect, GetText, GetCurrentWord, SelectCurrentWord, CreateParagraph, Push, MoveCursorToEnd, StartAction, EndAction, Asc.scope object

结果