InlineAIPrompt
TsgcHTMLComponent_InlineAIPrompt:一个直接在用户工作的地方向助手提问的弹出框,位于多行文本区、富文本编辑器旁边,或网格列的表头中,适用于 Delphi、C++ Builder 和 .NET。保留什么由人来决定。
TsgcHTMLComponent_InlineAIPrompt:一个直接在用户工作的地方向助手提问的弹出框,位于多行文本区、富文本编辑器旁边,或网格列的表头中,适用于 Delphi、C++ Builder 和 .NET。保留什么由人来决定。
一个有人参与的控件。提示词在弹出框中输入,答案也显示在其中,只有点击 Accept 才会通过 OnAIAccept 把答案报告给您的应用。组件从不写入数据集、字段或页面。它通过 AIAssistant 属性作答,该属性是一个 TsgcHTMLAIAssistant。
TsgcHTMLComponent_InlineAIPrompt(单元 sgcHTML_Component_InlineAIPrompt)
一个触发按钮和一张弹出卡片,包含提示框、答案、Accept 和 Discard
Delphi, C++ Builder, .NET
给弹出框设置 PromptID 和 TargetName,指定 AIAssistant,用助手可以看到的记录填充 Context,并把它回传的四个操作分别路由到 Open、ProcessPrompt、Accept 和 Discard。被接受的内容会在 OnAIAccept 中送达您。
uses
sgcHTML_AI, sgcHTML_Component_InlineAIPrompt;
FAI := TsgcHTMLAIAssistant.Create(Self);
FAI.Provider := aiprOpenAI;
FAI.APIKey := 'API_KEY';
FAI.AllowRowDataToProvider := True; // the answer is drafted over Context
FPrompt := TsgcHTMLComponent_InlineAIPrompt.Create(Self);
FPrompt.PromptID := 'notePrompt';
FPrompt.TargetName := 'Note';
FPrompt.Title := 'Draft the note';
FPrompt.Placeholder := 'e.g. write a short delivery note';
FPrompt.AIAssistant := FAI;
FPrompt.Context.Add('Customer=Northwind'); // the rows the assistant may see
FPrompt.OnAIAccept := PromptAccept;
Response := FPrompt.HTML; // the trigger, and the popover while IsOpen
// only when a human presses Accept, aValue is the answer that was on screen
procedure TMain.PromptAccept(Sender: TObject; const aValue: string);
begin
SaveNote(aValue); // your application writes it where it belongs
end;
// in the message handler, one call for each action the popover posts
if vAction = 'inlineAIOpen' then
FPrompt.Open
else if vAction = 'inlineAIAsk' then
FPrompt.ProcessPrompt(vText)
else if vAction = 'inlineAIAccept' then
FPrompt.Accept
else if vAction = 'inlineAIDiscard' then
FPrompt.Discard;
oHTMX.PushFragment(vGuid, FPrompt.HTML);
// includes: sgcHTML_AI.hpp, sgcHTML_Component_InlineAIPrompt.hpp
FAI = new TsgcHTMLAIAssistant(this);
FAI->Provider = aiprOpenAI;
FAI->APIKey = "API_KEY";
FAI->AllowRowDataToProvider = true; // the answer is drafted over Context
FPrompt = new TsgcHTMLComponent_InlineAIPrompt(this);
FPrompt->PromptID = "notePrompt";
FPrompt->TargetName = "Note";
FPrompt->Title = "Draft the note";
FPrompt->Placeholder = "e.g. write a short delivery note";
FPrompt->AIAssistant = FAI;
FPrompt->Context->Add("Customer=Northwind"); // the rows the assistant may see
FPrompt->OnAIAccept = PromptAccept;
String html = FPrompt->HTML; // the trigger, and the popover while IsOpen
// only when a human presses Accept, aValue is the answer that was on screen
void __fastcall TMain::PromptAccept(TObject *Sender, const String aValue)
{
SaveNote(aValue); // your application writes it where it belongs
}
// in the message handler, one call for each action the popover posts
if (vAction == "inlineAIOpen")
FPrompt->Open();
else if (vAction == "inlineAIAsk")
FPrompt->ProcessPrompt(vText);
else if (vAction == "inlineAIAccept")
FPrompt->Accept();
else if (vAction == "inlineAIDiscard")
FPrompt->Discard();
oHTMX->PushFragment(vGuid, FPrompt->HTML);
using esegece.sgcWebSockets;
var ai = new TsgcHTMLAIAssistant();
ai.Provider = TsgcHTMLAIAssistant.TsgcHTMLAIProvider.aiprOpenAI;
ai.APIKey = "API_KEY";
ai.AllowRowDataToProvider = true; // the answer is drafted over Context
var prompt = new TsgcHTMLComponent_InlineAIPrompt();
prompt.PromptID = "notePrompt";
prompt.TargetName = "Note";
prompt.Title = "Draft the note";
prompt.Placeholder = "e.g. write a short delivery note";
prompt.AIAssistant = ai;
prompt.Context.Add("Customer=Northwind"); // the rows the assistant may see
prompt.OnAIAccept += PromptAccept;
string html = prompt.HTML; // the trigger, and the popover while IsOpen
// only when a human presses Accept, value is the answer that was on screen
void PromptAccept(object sender, string value)
{
SaveNote(value); // your application writes it where it belongs
}
// in the message handler, one call for each action the popover posts
if (action == "inlineAIOpen")
prompt.Open();
else if (action == "inlineAIAsk")
prompt.ProcessPrompt(text);
else if (action == "inlineAIAccept")
prompt.Accept();
else if (action == "inlineAIDiscard")
prompt.Discard();
htmx.PushFragment(guid, prompt.HTML);
您最常使用的成员。
提示词在弹出框中输入,答案也显示在其中,只有 Accept 才会通过 OnAIAccept 把答案报告给您的应用。Discard 关闭弹出框并丢弃答案。组件从不写入数据集、字段或页面:如何处理返回的内容由您的应用决定。模型文本通过节点层写入,因此会被转义,永远不会变成标记。
Open 打开弹出框,此时尚未输入提示词。ProcessPrompt(aPrompt) 先询问 OnAIPrompt,如果它没有处理,再让助手基于 Context 作答,并让弹出框保持打开,答案放在 ResultText 中。Accept 会把 ResultText 通过 OnAIAccept 报告出去并关闭弹出框,没有答案时不报告任何内容。Discard 将其关闭。
OnAIPrompt(Sender, aPrompt, aResult, aHandled) 让您的应用自行作答:设置 aResult 和 aHandled。如果让 aHandled 保持 False,则改为询问助手。当用户点击 Accept 时会触发 OnAIAccept(Sender, aValue),aValue 就是屏幕上显示的答案。
把一个 TsgcHTMLAIAssistant 指定给 AIAssistant,参见 AIAssistant。弹出框会调用它的 SuggestColumnValues,作用于 Context,这是一个 TStringList,每条记录一行。这些是行数据:当助手的 AllowRowDataToProvider 为 False 时不会发送任何内容,而当 Context 为空且 OnAIPrompt 也没有作答时,则根本不会发出任何询问。
PromptID(默认为 sgcInlineAI)是弹出框的 id,也是在 prompt 字段中回传的值。TargetName 指明被接受的答案所属的控件,并在 target 字段中回传,因此您可以对其进行路由。Title、Placeholder 和 ButtonText 设置文本,IsOpen 显示面板,PromptText 和 ResultText 保存所问和所答的内容。
没有 HTTP 路由。每个控件都是一个 data-sgc-ws-send 表单:inlineAIOpen(字段 prompt、target)、inlineAIAsk(另加 text)、inlineAIAccept 和 inlineAIDiscard。根据 action 分派,检查 prompt 以了解是哪个弹出框发送的,并把重新渲染的组件推回去。以流式返回答案的宿主,会在 ResultText 增加内容后重新渲染它。
触发按钮始终会被渲染。当 IsOpen 开启时,它旁边会渲染一张卡片,包含标题、提示框、提问按钮,以及在有答案之后,带有 Accept 和 Discard 控件的答案。文本来自本地化字符串 AIAsk、AIAccept 和 AIDiscard。
组件在两条消息之间保存问题和待处理的答案,因此一个实例对应一个用户:在多用户应用中,请为每个会话使用各自的实例。与任何其他 htmx WebSocket 消息一样,这些消息不做 CSRF 检查,因此请在服务器上保留 OnAIAccept 所执行操作的授权。
该单元在定义了 SGC_HTML 时编译,sgcVer.inc 不会为 Android 和 iOS 定义它。使用提供商需要 Enterprise 或 All-Access 版本的 AI 单元,或者 sgcAI 包,并且它们仅限 Windows。此组件没有基于规则的回退方案,因此在其他平台上,或者没有提供商时,请自行在 OnAIPrompt 中作答。