AIChat
TsgcHTMLComponent_AIChat — 一个 AI 助手聊天,带有提供商/模型选择器、实时令牌流式传输和 RAG 来源引用,适用于 Delphi、C++ Builder 和 .NET。
TsgcHTMLComponent_AIChat — 一个 AI 助手聊天,带有提供商/模型选择器、实时令牌流式传输和 RAG 来源引用,适用于 Delphi、C++ Builder 和 .NET。
一个助手界面,扩展了 TsgcHTMLComponent_ChatBox,带有 OpenAI / Anthropic / Gemini 提供商页眉、流式回复和可折叠的 RAG 来源。选择一个提供商,处理 OnChatSend,然后读取 HTML 属性。
选择 AIProvider 和 ModelName,处理 OnChatSend 以生成回复,然后读取 HTML。该事件是浏览器消息到达您的 Delphi、C++ Builder 或 .NET 代码的方式。
uses
sgcHTML_Enums, sgcHTML_Component_AIChat;
var
oAI: TsgcHTMLComponent_AIChat;
begin
oAI := TsgcHTMLComponent_AIChat.Create(nil);
try
oAI.AIProvider := apOpenAI;
oAI.ModelName := 'gpt-4o';
oAI.AIName := 'Support Bot';
oAI.SystemPrompt := 'You are a helpful assistant.';
oAI.WelcomeMessage := 'Hi! Ask me anything.';
oAI.StreamingEnabled := True;
oAI.OnChatSend := DoChatSend; // browser message -> your code
WebModule.Response := oAI.HTML; // Bootstrap card + AI header
finally
oAI.Free;
end;
end;
// OnChatSend hands you the user message + the JSON history,
// you call your LLM and stream the answer back:
procedure TForm1.DoChatSend(Sender: TObject; const aUserMessage,
aConversationHistory: string);
begin
oAI.BeginStreaming;
oAI.PushStreamChunk('Sure, ');
oAI.PushStreamChunk('here is the answer...');
oAI.EndStreaming;
WebSocket.WriteData(oAI.GetStreamFragmentHTML);
end;
// includes: sgcHTML_Enums.hpp, sgcHTML_Component_AIChat.hpp
TsgcHTMLComponent_AIChat *oAI = new TsgcHTMLComponent_AIChat(NULL);
try
{
oAI->AIProvider = apOpenAI;
oAI->ModelName = "gpt-4o";
oAI->AIName = "Support Bot";
oAI->SystemPrompt = "You are a helpful assistant.";
oAI->WelcomeMessage = "Hi! Ask me anything.";
oAI->StreamingEnabled = true;
oAI->OnChatSend = DoChatSend; // browser message -> your code
String html = oAI->HTML; // Bootstrap card + AI header
}
__finally
{
delete oAI;
}
// OnChatSend handler: call your LLM, then stream the reply:
void __fastcall TForm1::DoChatSend(TObject *Sender,
const String aUserMessage, const String aConversationHistory)
{
oAI->BeginStreaming();
oAI->PushStreamChunk("Sure, ");
oAI->PushStreamChunk("here is the answer...");
oAI->EndStreaming();
}
using esegece.sgcWebSockets;
var ai = new TsgcHTMLComponent_AIChat();
ai.AIProvider = TsgcHTMLAIProvider.apOpenAI;
ai.ModelName = "gpt-4o";
ai.AIName = "Support Bot";
ai.SystemPrompt = "You are a helpful assistant.";
ai.WelcomeMessage = "Hi! Ask me anything.";
ai.StreamingEnabled = true;
// OnChatSend: browser message -> your code -> stream the reply
ai.OnChatSend += (sender, userMessage, conversationHistory) =>
{
ai.BeginStreaming();
ai.PushStreamChunk("Sure, ");
ai.PushStreamChunk("here is the answer...");
ai.EndStreaming();
};
string html = ai.HTML; // Bootstrap card + AI header
您最常使用的成员。
AIProvider 选择 apOpenAI、apAnthropic、apGemini 或 apCustom;ModelName 为页眉添加标签;ShowModelSelector 切换它;AIName 为助手命名。
SystemPrompt 和 WelcomeMessage 为聊天预设内容;UserColor 和 AIColor(均为 TsgcHTMLColor)为气泡着色;GetConversationHistoryJSON 返回供您 LLM 调用使用的角色/内容数组。
当访客提交时,OnChatSend 会随用户消息和 JSON 历史一起触发 — 这是您调用模型的钩子。ProcessUserMessage(aMessage) 可从代码中驱动该流程。
StreamingEnabled 开启它;BeginStreaming、PushStreamChunk(aChunk) 和 EndStreaming 逐令牌扩充回复;GetStreamFragmentHTML 返回要推送的 htmx 片段。
RAGEnabled 配合 OnRAGContext 注入检索到的上下文;RAGDisplayMode(rdInline/rdCollapsible/rdFootnotes)和 AddAIMessageWithSources(aText, aSourcesHTML) 渲染引用。
来自 ChatBox:Messages、Title、Height、InputPlaceholder 和 ShowTypingIndicator。HTML 返回整个卡片;GetLastMessageHTML 仅返回最新的气泡。