From a6d1c966d855db80d0be931ff2db51c44dec57b3 Mon Sep 17 00:00:00 2001 From: lihanbo Date: Fri, 28 Feb 2025 08:41:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5AI=E6=94=AF=E6=8C=81=EF=BC=88?= =?UTF-8?q?=E7=81=AB=E5=B1=B1=E5=BC=95=E6=93=8E=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AI/AiHelper.cs | 74 ++++++ AI/PromptUtil.cs | 31 +++ App.xaml | 7 +- Converter/Converters.cs | 68 +++++ ExcelHelper.csproj | 9 +- Views/Components/AiMessageControll.xaml | 131 ++++++++++ Views/Components/AiMessageControll.xaml.cs | 184 ++++++++++++++ Views/Pages/ImportExcelPage.xaml | 20 +- Views/Pages/ImportExcelPage.xaml.cs | 4 +- Views/ViewModels/ImportViewModel.cs | 275 ++++++++++++++++++++- 10 files changed, 797 insertions(+), 6 deletions(-) create mode 100644 AI/AiHelper.cs create mode 100644 AI/PromptUtil.cs create mode 100644 Converter/Converters.cs create mode 100644 Views/Components/AiMessageControll.xaml create mode 100644 Views/Components/AiMessageControll.xaml.cs diff --git a/AI/AiHelper.cs b/AI/AiHelper.cs new file mode 100644 index 0000000..01bd126 --- /dev/null +++ b/AI/AiHelper.cs @@ -0,0 +1,74 @@ +using System; +using System.ClientModel; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.AI; +using OpenAI; + +namespace ExcelHelper.AI; + +class AiHelper +{ + /// + /// 火山引擎 + /// + private const string API_KEY = ""; + private const string API_ENDPOINT = "https://ark.cn-beijing.volces.com/api/v3/"; + + private readonly IChatClient _client; + + private bool UseTool = false; + public AiHelper(string modelName, bool useTools = false) + { + UseTool = useTools; + if (useTools) + { + //TODO: add function call + } + else + { + + var client = new OpenAIClient(new ApiKeyCredential(API_KEY), new OpenAIClientOptions + { + Endpoint = new Uri(API_ENDPOINT), + }); + _client = client.AsChatClient(modelName); + } + } + + public IChatClient Client => _client; + + public async Task GetChatResponseAsync(string question) + { + var response = await _client.GetResponseAsync(question); + return response.Message.ToString(); + } + + public async IAsyncEnumerable GetStreamingResponseAsync(string question) + { + // 获取所有的工具函数 + //var toolsList = AgentExecutor.GetDefaultTools(); + ChatOptions chatOptions = new ChatOptions + { + // TODO: Models config + TopK= 10, + TopP = 0.85f, + Temperature = 0.7f, + + }; + + if (UseTool) + { + //TODO: add function call + } + await foreach (var response in _client.GetStreamingResponseAsync(question, chatOptions)) + { + yield return response.Text; + } + } + +} diff --git a/AI/PromptUtil.cs b/AI/PromptUtil.cs new file mode 100644 index 0000000..f97a4f5 --- /dev/null +++ b/AI/PromptUtil.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExcelHelper.AI +{ + class PromptUtil + { + public static string UsePrompt(string msg,string excelPrompt) + { + return $@" +你是一个Excel公式大师,你需要帮助用户解答Excel公式相关的问题。 + +你在回复用户的问题时,需要遵守以下规则: + 1. 优先保证答案的准确性,其次是简洁明了。 + 2. 需要保证你的回答是有逻辑性的,不要让用户感到困惑。 + 3. 可以例举2~3个例子来帮助用户更好地理解你的回答。 + 4. 一定要保证文档格式的正确性,避免出现无法显示的内容。 + 5. 尽量使用比较简短与易懂的公式来回答用户的问题,避免用户无法理解。 + +现在的时间是{DateTime.Now}。 + +当前用户提问的excel文件列信息为:{excelPrompt} + +用户的问题是:{msg} +"; + } + } +} diff --git a/App.xaml b/App.xaml index 74a86bf..dd7bf0f 100644 --- a/App.xaml +++ b/App.xaml @@ -2,13 +2,18 @@ x:Class="ExcelHelper.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="clr-namespace:ExcelHelper"> + xmlns:local="clr-namespace:ExcelHelper" + xmlns:local1="clr-namespace:ExcelHelper.Converter"> + + + + diff --git a/Converter/Converters.cs b/Converter/Converters.cs new file mode 100644 index 0000000..e2f7d2b --- /dev/null +++ b/Converter/Converters.cs @@ -0,0 +1,68 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; +using System.Windows.Media; + +namespace ExcelHelper.Converter; + +public class BoolToColorConverter : IValueConverter +{ + public Brush UserMessageColor { get; set; } = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DCF8C6")); + public Brush AIMessageColor { get; set; } = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ECECEC")); + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool isUser) + { + return isUser ? UserMessageColor : AIMessageColor; + } + return AIMessageColor; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } +} + +public class BoolToAlignmentConverter : IValueConverter +{ + public HorizontalAlignment TrueValue { get; set; } = HorizontalAlignment.Right; + public HorizontalAlignment FalseValue { get; set; } = HorizontalAlignment.Left; + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + return boolValue ? TrueValue : FalseValue; + } + return FalseValue; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } +} + +public class InverseBoolConverter : IValueConverter +{ + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + return !boolValue; + } + return true; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool boolValue) + { + return !boolValue; + } + return true; + } +} \ No newline at end of file diff --git a/ExcelHelper.csproj b/ExcelHelper.csproj index 3175fbc..3ad210b 100644 --- a/ExcelHelper.csproj +++ b/ExcelHelper.csproj @@ -46,14 +46,18 @@ - 8.3.2 + 8.4.0 3.5.1 + + + + 1.34.2 @@ -69,4 +73,7 @@ + + + \ No newline at end of file diff --git a/Views/Components/AiMessageControll.xaml b/Views/Components/AiMessageControll.xaml new file mode 100644 index 0000000..f3284a3 --- /dev/null +++ b/Views/Components/AiMessageControll.xaml @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +