翻譯|使用教程|編輯:龔雪|2025-08-28 11:15:32.987|閱讀 33 次
概述:本文主要介紹了Tool Call Confirmation API層和DevExpress Blazor AI Chat組件的相關可自定義接口,歡迎下載最新版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress Blazor UI組件使用了C#為Blazor Server和Blazor WebAssembly創建高影響力的用戶體驗,這個UI自建庫提供了一套全面的原生Blazor UI組件(包括Pivot Grid、調度程序、圖表、數據編輯器和報表等)。
現代AI驅動的應用程序通常會自動執行工具來響應用戶查詢,雖然這種自動化包含了llm的潛力并改善了用戶體驗,但在未經用戶明確同意的情況下調用敏感操作(例如,修改數據庫、發送電子郵件或對外部服務進行API調用)時,它可能會引入安全風險。
本文主要介紹了Tool Call Confirmation API(工具調用)層和DevExpress Blazor AI Chat組件的相關可自定義接口的目的,DevExpress的解決方案攔截AI發起的函數調用,生成詳細的確認對話框,并在執行前需要用戶批準,這種UI模式在GitHub Copilot Chat、Cursor、Claude和其他具有MCP支持的AI驅動應用程序中很常見。
在本文中,我們將帶大家了解DevExpress的AI聊天確認系統、它的關鍵組件和自定義選項(當然還會向您展示如何使用DevExpress Blazor AI Chat控件將這個安全層添加到AI驅動的Blazor應用程序中)。
DevExpress技術交流群11:749942875 歡迎一起進群討論
AI函數調用是一種強大的資源,它允許模型與應用程序功能無縫交互。然而必須負責任地使用這種能力,考慮以下使用場景:
本文中描述的技術在便利性和控制性之間取得了平衡:它保留了自動工具調用的好處,并為用戶提供了對敏感或不可逆操作的明確權限。
在您的Blazor應用程序中創建和配置DevExpress Blazor Chat (DxAIChat)組件。
這個例子的目標是Azure OpenAI,但是這個解決方案與任何實現了來自 "Microsoft.Extensions.AI"庫的IChatClient接口的AI服務兼容。
Program.cs中的以下代碼定義了基本配置:
using Azure; using Azure.AI.OpenAI; using Microsoft.Extensions.AI; var builder = WebApplication.CreateBuilder(args); // Replace with your endpoint, API key, and model's deployment name string azureOpenAIEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); string azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY"); string deploymentName = "your-model-deployment-name"; var azureChatClient = new AzureOpenAIClient( new Uri(azureOpenAIEndpoint), new AzureKeyCredential(azureOpenAIKey)) .GetChatClient(deploymentName) .AsIChatClient(); builder.Services.AddDevExpressBlazor(); builder.Services.AddDevExpressAI();
DevExpress確認系統的核心是IToolCallFilter接口——這個接口定義了聊天器攔截和管理函數調用的方式。
public interface IToolCallFilter { public event Action<FunctionInvocationContext, TaskCompletionSource<bool>> ToolCalled; Task<bool> InvokeFunctionFilter(FunctionInvocationContext context); }
MyToolCallFilter管理AI觸發的操作,并僅在獲得批準時執行敏感操作。API設計允許過濾器暫停函數執行,等待用戶輸入,并根據用戶的決定繼續或取消,TaskCompletionSource實例支持UI和過濾器邏輯之間的異步通信。
public class MyToolCallFilter : IToolCallFilter { public event Action<FunctionInvocationContext, TaskCompletionSource<bool>> ToolCalled; public Task<bool> InvokeFunctionFilter(FunctionInvocationContext context) { if (ToolCalled is null) return Task.FromResult(true); var tcs = new TaskCompletionSource<bool>(); ToolCalled.Invoke(context, tcs); return tcs.Task; } }
CustomFunctionInvokingChatClient類擴展了DevExpress Blazor AI Chat控件的默認操作:
public class CustomFunctionInvokingChatClient : FunctionInvokingChatClient { public CustomFunctionInvokingChatClient(IChatClient innerClient, ILoggerFactory? factory = null, IServiceProvider? services = null) : base(innerClient, factory, services) { if(services == null) { throw new ArgumentNullException(nameof(services), "Service provider cannot be null."); } FunctionInvoker = CustomFunctionInvoker; } private async ValueTask<object?> CustomFunctionInvoker(FunctionInvocationContext context, CancellationToken cancellationToken) { IToolCallFilter? filter = FunctionInvocationServices!.GetService<IToolCallFilter>(); if(await (filter?.InvokeFunctionFilter(context) ?? Task.FromResult(true))) { return await context.Function.InvokeAsync(context.Arguments, cancellationToken); } return "The tool call was cancelled by the user. Do not attempt to invoke this tool again. Return a message indicating that the call was cancelled and that the weather information is unavailable at this time."; } }
未完待續,下期繼續......
更多產品資訊及授權,歡迎來電咨詢:023-68661681
慧都是?家?業數字化解決?案公司,專注于軟件、?油與?業領域,以深?的業務理解和?業經驗,幫助企業實現智能化轉型與持續競爭優勢。
慧都是DevExpress的中國區的合作伙伴,DevExpress作為用戶界面領域的優秀產品,幫助企業高效構建權限管理、數據可視化(如網格/圖表/儀表盤)、跨平臺系統(WinForms/ASP.NET/.NET MAUI)及行業定制解決方案,加速開發并強化交互體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網