AIChat

TsgcHTMLComponent_AIChat — 一个 AI 助手聊天,带有提供商/模型选择器、实时令牌流式传输和 RAG 来源引用,适用于 Delphi、C++ Builder 和 .NET。

TsgcHTMLComponent_AIChat

一个助手界面,扩展了 TsgcHTMLComponent_ChatBox,带有 OpenAI / Anthropic / Gemini 提供商页眉、流式回复和可折叠的 RAG 来源。选择一个提供商,处理 OnChatSend,然后读取 HTML 属性。

组件类

TsgcHTMLComponent_AIChat

渲染为

Bootstrap 5 card + 作用域 CSS

家族

聊天与 AI

语言

Delphi, C++ Builder, .NET

创建它、接入 OnChatSend、渲染它

选择 AIProviderModelName,处理 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 选择 apOpenAIapAnthropicapGeminiapCustomModelName 为页眉添加标签;ShowModelSelector 切换它;AIName 为助手命名。

会话

SystemPromptWelcomeMessage 为聊天预设内容;UserColorAIColor(均为 TsgcHTMLColor)为气泡着色;GetConversationHistoryJSON 返回供您 LLM 调用使用的角色/内容数组。

发送事件

当访客提交时,OnChatSend 会随用户消息和 JSON 历史一起触发 — 这是您调用模型的钩子。ProcessUserMessage(aMessage) 可从代码中驱动该流程。

流式传输

StreamingEnabled 开启它;BeginStreamingPushStreamChunk(aChunk)EndStreaming 逐令牌扩充回复;GetStreamFragmentHTML 返回要推送的 htmx 片段。

RAG 来源

RAGEnabled 配合 OnRAGContext 注入检索到的上下文;RAGDisplayModerdInline/rdCollapsible/rdFootnotes)和 AddAIMessageWithSources(aText, aSourcesHTML) 渲染引用。

继承与输出

来自 ChatBoxMessagesTitleHeightInputPlaceholderShowTypingIndicatorHTML 返回整个卡片;GetLastMessageHTML 仅返回最新的气泡。

继续探索

所有 sgcHTML 组件浏览 60 多个组件的完整功能矩阵。
下载免费试用版30 天试用版附带 60.HTML 演示项目。
价格Single、Team 和 Site 授权,均含完整源代码。

准备好开始了吗?

下载免费试用版,开始在 Delphi、C++ Builder 和 .NET 中构建 Web 界面。