原創|使用教程|編輯:龔雪|2024-09-30 11:05:47.173|閱讀 100 次
概述:本文主要介紹界面控件DevExpress v24.2如何完成具有AI功能文本編輯器的擴展,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
本文重點介紹了DevExpress在近年來最熱門領域——人工智能(AI)和自然語言處理(NLP)的改進!
NLP是人工智能的一個分支,它允許計算機與人類語言進行交互,這包括以有意義/有用的方式理解、解釋、生成和回應文本(和語音)的能力。基于NLP的功能允許更好的數據分析、個性化體驗、高效的溝通,并導致更明智的決策和提高效率。例如:
P.S:DevExpress擁有.NET開發需要的所有平臺控件,包含600多個UI控件、報表平臺、DevExpress Dashboard eXpressApp 框架、適用于 Visual Studio的CodeRush等一系列輔助工具。
DevExpress技術交流群10:532598169 歡迎一起進群討論
DevExpress AI APIs允許您在WinForms、WPF或Blazor應用程序中注冊和使用多個AI服務,DevExpress的搶先體驗預覽版(v24.2.1)支持以下AI服務:
本文中描述的AI服務和DevExpress AI擴展遵循"bring your own key" 原則,DevExpress不提供REST API,也不提供任何內置的LLMs/SLMs。您需要有一個激活的Azure/Open AI訂閱來獲取REST API端點、密鑰和模型部署名稱,這些變量必須在應用程序啟動時指定,來注冊AI客戶端并在應用程序中啟用DevExpress AI-Powered擴展。
在開始之前,您需要滿足以下先決條件:
或 .NET Framework v4.7.2
下表列出了DevExpress AI相關的NuGet包:
要在控制臺應用程序中使用DevExpress AI-Powered擴展,您需要安裝以下DevExpress包中的一個并注冊AI客戶端:
DevExpress AI-Powered擴展在AIExtensionsContainerDefault容器中運行,該容器管理已注冊的AI客戶端,以便DevExpress UI組件可以自動利用AI服務。
下面的代碼片段注冊了一個Azure OpenAI客戶端:
using Azure.AI.OpenAI; using DevExpress.AIIntegration; ... //Modify the following lines to obtain and pass your personal Azure OpenAI credentails to the Register* method. string AzureOpenAIEndpoint { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"); } } string AzureOpenAIKey { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_APIKEY"); } } string DeploymentName { get { return Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENTNAME"); } } AIExtensionsContainerDefault defaultAIContainer; ... static void Main() { defaultAIContainer = new AIExtensionsContainerDefault(); defaultAIContainer.RegisterChatClientOpenAIService( new AzureOpenAIClient(new Uri(AzureOpenAIEndpoint), new System.ClientModel.ApiKeyCredential(AzureOpenAIKey)), DeploymentName ); }
要使用離線的模型,請調用RegisterChatClientOllamaAIService方法。這個方法注冊一個Ollama客戶端,在這個方法中,第一個參數是默認的,第二個參數指定模型名稱:
defaultAIContainer.RegisterChatClientOllamaAIService("http://localhost:11434/api/chat", "llama3.1");
一旦您注冊了一個AI客戶端,可以使用DevExpress AI驅動的擴展如下(DevExpress AI驅動的擴展如下所示):
var result = await defaultAIContainer.TranslateAsync(new TranslateRequest(textToTranslate, language)); //OR var translateExtension = defaultAIContainer.CreateTranslateExtension(); TranslateRequest request = new TranslateRequest(textToTranslate, language); var result = await translateExtension.ExecuteAsync(request, CancellationToken.None);
處理大型文本塊可能具有挑戰性,但我們已經覆蓋了。DevExpress AI-Powered擴展自動將大型內容拆分為可管理的塊(包括段落、句子、單詞、標點符號和其他文本元素),以確保準確處理,以下是如何管理大型文本塊和處理AI服務響應的方法:
public async Task<string> GetTranslation(string textToTranslate, string language) { TextResponse result = await defaultAIContainer.TranslateAsync(new TranslateRequest(textToTranslate, language)); if(result.IsCompleted) return result.Response; if(!result.IsRestrictedOrFailed) { string translatedText = result.Response; while(result.IsContinuationRequired){ await result.ContinueAsync(); translatedText += result.Response } return translatedText; } //Something unexpected happened switch (result.Status) { case ResponseStatus.MaxTokenLimitExceeded: case ResponseStatus.InputSizeLimitExceeded: return "The text you're trying to send within a request is too long and exceeds the allowed limit."; case ResponseStatus.ContentFiltered: return "Potentially harmful content was detected in your request."; case ResponseStatus.Error: return "An error occurred while processing the request."; } throw new NotSupportedException(); }
重要的響應包括:
使用以下設置來配置文本內容和圖像的大小限制:
public static partial class AIIntegration { //Specifies the maximum number of tokens that can be processed by a model request. Applies to all DevExpress AI-Powered extensions. public static int? ChatMaxTokensDefault { get; set; } = null; //Specifies the default temperature for chat-based AI-Powered extensions. //To learn more please visit //ai.stackexchange.com/questions/32477/what-is-the-temperature-in-the-gpt-models public static float? ChatTemperatureDefault { get; set; } = null; //Specifies the maximum allowed size of the text buffer per request, in bytes. public static int TextBufferMaxSize { get; set; } = 64 * 1024;//64kb //Specifies the maximum allowed size of the image buffer per request, in bytes. public static int ImageBufferMaxSize { get; set; } = 2 * 1024 * 1024;//2mb //Specifies the maximum allowed chunk length, in characters. public static int ChunkMaxLength { get; set; } = 6000; }
DevExpress EAP v24.2附帶以下AI支持的擴展:
這些擴展以類似聊天的方式工作 - 其中請求通過內置提示發送到OpenAI/Ollama模型。
除了提供對托管在Azure和Ollama中的AI模型的訪問外,我們還實現了與Azure AI語言服務交互的API,包括Azure AI Translator和Azure AI Language(文本摘要)。服務的選擇取決于您的預算,但是請注意,在調用以下方法時,文本翻譯和摘要請求將專門發送到Azure AI語言服務:
string azureTranslatorEndpoint = GetEnvironmentVariable("AZURE_TRANSLATOR_ENDPOINT"); string azureTranslatorKey = GetEnvironmentVariable("AZURE_TRANSLATOR_API_KEY"); //Register an Azure.TextAnalytics client. defaultAIContainer.RegisterTranslatorAzureAIService(new TextTranslationClient(new AzureKeyCredential(azureTranslatorKey), new Uri(azureTranslatorEndpoint))); //Register an Azure.TextAnalytics client. string azureTextAnalyticsEndpoint = GetEnvironmentVariable("AZURE_TEXT_ANALYTICS_ENDPOINT"); string azureTextAnalyticsKey = GetEnvironmentVariable("AZURE_TEXT_ANALYTICS_API_KEY"); defaultAIContainer.RegisterTextAnalyticsAzureAIService(new TextAnalyticsClient(new Uri(azureTextAnalyticsEndpoint), new AzureKeyCredential(azureTextAnalyticsKey)));
DevExpress AI驅動的擴展具有預先構建的提示,可以根據需要進行定制:
1. AI驅動的擴展中派生并覆蓋GetSystemPrompt方法:
public class WilliamShakespeareStyleExtension : RewriteStyleExtension { public WilliamShakespeareStyleExtension(IServiceProvider serviceProvider) : base(serviceProvider) { } protected override string GetSystemPrompt(RewriteStyleRequest request) { return "Rewrite this text in the William Shakespeare style."; } }
2. 在默認容器中注冊您的擴展(這將覆蓋內置擴展):
defaultAIContainer.Register<RewriteStyleRequest, WilliamShakespeareStyleExtension>();
當您調用RewriteStyleAsync方法時,自定義提示將與用戶輸入一起發送到AI服務。
在這個階段,DevExpress AI相關的API的另一個重要元素開始發揮作用:AIExtensionsContainerLocal類。與默認擴展容器類似,AIExtensionsContainerLocal類存儲了您手動實現和注冊的ai支持的擴展,而無需替換DevExpress內置擴展。
關鍵的區別在于這個容器中包含的擴展不會被DevExpress UI組件自動使用,這允許您為應用程序的不同部分實現不同的行為。下面的代碼片段實現了一個自定義的AI驅動的擴展(注意如何使用基類):
public class AuthoredStyleExtension : ChangeTextExtension<AuthoredStyleRequest> { public AuthoredStyleExtension(IServiceProvider serviceProvider) : base(serviceProvider) { } protected override string GetSystemPrompt(AuthoredStyleRequest request) { return $"Rewrite this text in the {request.Author} style"; } } //A custom request that processes text. public class AuthoredStyleRequest : TextRequest { public AuthoredStyleRequest(string Author, string Text) : base(Text) { this.Author = Author; } public string Author { get; init; } } public static class CustomAIIntegration { public static AuthoredStyleExtension CreateAuthoredStyleExtension(this IAIExtensionsContainer container){ return (AuthoredStyleExtension)container.GetExtension(typeof(AuthoredStyleRequest));
您可以實現自己的TextRequest后代,以便在構造提示符時向AI模型提供額外的信息。示例包括內置的ChangeTone和RewriteStyle擴展,它們獲得用于指定語氣(例如,Casual, Friendly, Confident)和風格(例如,Formal, Conversational)的枚舉值。要在運行時調用自定義擴展,請使用以下代碼片段:
var localContainer = new AIExtensionsContainerLocal(defaultAIContainer); localContainer.Register<AuthoredStyleRequest, AuthoredStyleExtension>(); AuthoredStyleExtension extension = localContainer.CreateAuthoredStyleExtension(); var request = new AuthoredStyleRequest("Mark Twain", textToModify); //Here you can also set up the Temperature variable that will be used to execute this particular model request request.Options.Temperature = 0.9f; string result = await extension.ExecuteAsync(request, CancellationToken.None);
DevExpress AI-Powered擴展支持本地化,這有助于確保AI驅動的功能對于不同地區的用戶都是可訪問和相關的。通過本地化人工智能擴展,您可以調整用戶界面和任何預定義的內容,以滿足最終用戶的語言和文化偏好,您也可以根據區域偏好定制提示(例如,當使用單語言AI模型時)。
更多產品需求,歡迎咨詢“”~
更多DevExpress線上公開課、中文教程資訊請上中文網獲取
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網