没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|使用教程|编辑:龚雪|2025-06-23 10:32:52.837|阅读 113 次
概述:本文将探索如何在DevExpress Blazor DxAiChat组件中启用函数调用,欢迎下载最新版组件体验!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
DevExpress Blazor UI组件使用了C#为Blazor Server和Blazor WebAssembly创建高影响力的用户体验,这个UI自建库提供了一套全面的原生Blazor UI组件(包括Pivot Grid、调度程序、图表、数据编辑器和报表等)。
现代AI驱动的应用程序需要与外部系统或内部应用程序组件无缝交互,许多AI服务提供商现在支持函数调用(也称为工具调用),这允许AI模型在运行时触发函数。这种功能对于AI需要执行诸如获取数据、调用API或在应用程序中启动任务(从安排约会和修改数据库信息到更新应用程序的外观)等操作的代理工作流/应用程序特别有价值。
DevExpress技术交流群11:749942875 欢迎一起进群讨论
本文实例中的整个流程是这样的:模型不是回复用户消息,而是请求一个带有指定参数的函数调用,然后聊天客户端调用该函数并将结果返回给LLM。此时,LLM根据函数返回的值构造一个响应。
在本指南中,我们将探索如何在DevExpress Blazor DxAiChat组件中启用函数调用:
要开始,您必须首先将DxAiChat组件集成到应用程序中(请参阅我们的官方指南以获取更多信息):。
接下来注册您的AI服务,在这个例子中我们将使用Azure OpenAI。下面是一个示例Program.cs设置:
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
...
var builder = WebApplication.CreateBuilder(args);
...
// Replace with your endpoint, API key, and deployed AI model name
string azureOpenAIEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
string azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
string deploymentName = string.Empty;
...
var azureChatClient = new AzureOpenAIClient(
new Uri(azureOpenAIEndpoint),
new AzureKeyCredential(azureOpenAIKey));
IChatClient chatClient = azureChatClient.AsChatClient(deploymentName);
builder.Services.AddDevExpressBlazor();
builder.Services.AddChatClient(chatClient);
builder.Services.AddDevExpressAI();
运行项目来确认您可以发送消息和接收AI响应。
首先,定义一个简单的函数来检索指定城市的天气信息。在本例中,这是GetWeatherTool。为了帮助AI理解如何调用GetWeatherTool函数,请使用方法及其参数的System.ComponentModel.Description属性。LLM使用参数找出最合适的方法调用,并规划调用顺序:
using System.ComponentModel;
using Microsoft.Extensions.AI;
public class CustomAIFunctions
{
public static AIFunction GetWeatherTool => AIFunctionFactory.Create(GetWeather);
[Description("Gets the current weather in the city")]
public static string GetWeather([Description("The name of the city")] string city)
{
switch (city)
{
case "Los Angeles":
case "LA":
return GetTemperatureValue(20);
case "London":
return GetTemperatureValue(15);
default:
return $"The information about the weather in {city} is not available.";
}
}
static string GetTemperatureValue(int value)
{
var valueInFahrenheits = value * 9 / 5 + 32;
return $"{valueInFahrenheits}\u00b0F ({value}\u00b0C)";
}
}
修改聊天客户端注册,如下所示,来提供可用函数列表,并允许客户端在回答用户问题时调用函数。确保首先配置聊天客户端选项,因为这里的方法调用顺序至关重要:
using Azure;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
...
IChatClient chatClient = new ChatClientBuilder(azureChatClient)
.ConfigureOptions(opt =>
{
opt.Tools = [CustomAIFunctions.GetWeatherTool];
})
.UseFunctionInvocation()
.Build();
builder.Services.AddChatClient(chatClient);
此时当用户向AI服务询问天气时,该服务将自动触发GetWeatherTool函数并将结果添加到其响应中。
Microsoft语义内核允许开发人员将高级AI功能整合到应用程序中(包括推理、工作流编排和动态提示工程),Microsoft的框架通过允许应用程序与插件交互和更有效地管理内存来增强AI解决方案。
首先,将以下NuGet包添加到项目中:
如果您已经在应用程序中使用语义内核,并且熟悉插件的概念,可以很容易地将它连接到DevExpress Blazor DxAiChat控件。
由于DevExpress AI驱动的API使用IChatClient接口与llm一起操作,您需要手动实现接口并从语义内核调用IChatCompletionService方法:
using Microsoft.Extensions.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
...
public class SemanticKernelPluginCallingChatClient : IChatClient {
private IChatCompletionService _chatCompletionService;
private Kernel _kernel;
private OpenAIPromptExecutionSettings _executionSettings;
public SemanticKernelPluginCallingChatClient(Kernel kernel)
{
_kernel = kernel;
_chatCompletionService = _kernel.GetRequiredService();
_executionSettings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
}
public async Task GetResponseAsync(IEnumerable chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
var history = GetChatHistory(chatMessages);
ChatMessageContent message = await _chatCompletionService.GetChatMessageContentAsync(history, _executionSettings, _kernel, cancellationToken);
return new ChatResponse(new ChatMessage(ChatRole.Assistant, message.Content));
}
public async IAsyncEnumerable GetStreamingResponseAsync(IEnumerable chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
var history = GetChatHistory(chatMessages);
await foreach(var item in _chatCompletionService.GetStreamingChatMessageContentsAsync(history, _executionSettings, _kernel, cancellationToken)) {
yield return new ChatResponseUpdate(ChatRole.Assistant, item.Content);
}
}
AuthorRole GetRole(ChatRole chatRole) {
if(chatRole == ChatRole.User) return AuthorRole.User;
if(chatRole == ChatRole.System) return AuthorRole.System;
if(chatRole == ChatRole.Assistant) return AuthorRole.Assistant;
if(chatRole == ChatRole.Tool) return AuthorRole.Tool;
throw new Exception();
}
private ChatHistory GetChatHistory(IEnumerable chatMessages)
{
var history = new ChatHistory(chatMessages.Select(x => new ChatMessageContent(GetRole(x.Role), x.Text)));
return history;
}
...
}
实现一个类似于前面函数的语义内核插件,但是用Microsoft.SemanticKernel.KernelFunction属性修饰main函数方法:
using Microsoft.SemanticKernel;
using System.ComponentModel;
...
public class WeatherPlugin {
[KernelFunction]
[Description("Gets the current weather in the city")]
public static string GetWeather([Description("The name of the city")] string city) {
switch(city) {
case "Los Angeles":
case "LA":
return GetTemperatureValue(20);
case "London":
return GetTemperatureValue(15);
default:
return $"The information about the weather in {city} is not available.";
}
}
static string GetTemperatureValue(int value)
{
var valueInFahrenheits = value * 9 / 5 + 32;
return $"{valueInFahrenheits}\u00b0F ({value}\u00b0C)";
}
}
最后,在应用程序启动时注册语义内核和聊天客户端:
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Plugins.Core; ... var semanticKernelBuilder = Kernel.CreateBuilder(); semanticKernelBuilder.AddAzureOpenAIChatCompletion( deploymentName, azureOpenAIEndpoint, azureOpenAIKey); // Add plugins from Microsoft.SemanticKernel.Plugins.Core #pragma warning disable SKEXP0050 semanticKernelBuilder.Plugins.AddFromType<TimePlugin>(); // this is a built-in plugin semanticKernelBuilder.Plugins.AddFromType<WeatherPlugin>(); // this is our custom plugin #pragma warning restore SKEXP0050 var globalKernel = semanticKernelBuilder.Build(); builder.Services.AddChatClient(new SemanticKernelPluginCallingChatClient(globalKernel)); builder.Services.AddDevExpressAI();
一旦配置好,您的应用程序将使用Semantic Kernel插件来智能地处理请求:
更多产品资讯及授权,欢迎来电咨询:023-68661681
慧都是⼀家⾏业数字化解决⽅案公司,专注于软件、⽯油与⼯业领域,以深⼊的业务理解和⾏业经验,帮助企业实现智能化转型与持续竞争优势。
慧都是DevExpress的中国区的合作伙伴,DevExpress作为用户界面领域的优秀产品,帮助企业高效构建权限管理、数据可视化(如网格/图表/仪表盘)、跨平台系统(WinForms/ASP.NET/.NET MAUI)及行业定制解决方案,加速开发并强化交互体验。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn
文章转载自:慧都网



在使用Parasoft C/C++test执行BugDetective数据流分析时,可能会遇到用户自定义的资源API,那在这种情况下,若要判断是否存在资源问题,如资源泄露等,则需要手动配置测试配置。
大型SaaS系统的自动化测试常常受制于界面变化快、结构复杂、加载机制多变等因素。从元素识别到脚本管理,SmartBear TestComplete帮助Salesforce建了可靠的自动化测试体系。
BarTender 标签管理系统,正是帮助企业轻松实现 GS1 标准化标签设计、编码生成与信息联动的强大工具。
Parasoft C/C++test 是一款功能强大的 C/C++ 软件测试工具,集成了静态代码分析、单元测试、集成测试和覆盖率分析等功能,单元测试作为其关键功能之一,为了适配多样化的目标部署环境,C/C++test 设计了灵活的测试结果收集机制。通过Socket通讯方式来收集单元测试结果,从而扩展其测试覆盖范围与应用场景。
相关产品
优秀的界面控件开发包,帮助企业构建卓越应用!
DevExpress DXperience Subscription高性价比的企业级.NET用户界面套包,助力企业创建卓越应用!
最新文章 MORE
半岛外围网上直营相关的文章 MORE
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@ke049m.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
半岛外围网上直营