From 21d7c719f1dd3724af65c40293d546a79c5f7be2 Mon Sep 17 00:00:00 2001 From: zyxucp <286513187@qq.com> Date: Mon, 5 Aug 2024 22:20:18 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A4=84=E7=90=86=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AntSK.Domain/Utils/ConvertUtils.cs | 49 ++++++++++++++++++- .../Utils/OpenAIHttpClientHandler.cs | 25 +++++++--- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/AntSK.Domain/Utils/ConvertUtils.cs b/src/AntSK.Domain/Utils/ConvertUtils.cs index 850052d6..e50d7ae8 100644 --- a/src/AntSK.Domain/Utils/ConvertUtils.cs +++ b/src/AntSK.Domain/Utils/ConvertUtils.cs @@ -1,4 +1,7 @@ -using System.Security.Cryptography; +using Newtonsoft.Json; +using Serilog; +using System.Security.Cryptography; +using System.Text.RegularExpressions; using System.Web; namespace AntSK.Domain.Utils @@ -263,6 +266,50 @@ public static bool ComparisonIgnoreCase(this string s, string value) return s.Equals(value, StringComparison.OrdinalIgnoreCase); } + + /// + /// \uxxxx转中文,保留换行符号 + /// + /// + /// + public static string Unescape(this string value) + { + if (value.IsNull()) + { + return ""; + } + + try + { + Formatting formatting = Formatting.None; + + object jsonObj = JsonConvert.DeserializeObject(value); + string unescapeValue = JsonConvert.SerializeObject(jsonObj, formatting); + return unescapeValue; + } + catch (Exception ex) + { + Log.Error(ex.ToString()); + return ""; + } + } + + + /// + /// 是否为流式请求 + /// + /// + /// + public static bool IsStream(this string value) + { + // 正则表达式忽略空格的情况 + string pattern = @"\s*""stream""\s*:\s*true\s*"; + + // 使用正则表达式匹配 + bool contains = Regex.IsMatch(value, pattern); + return contains; + } + public static string AntSKCalculateSHA256(this BinaryData binaryData) { byte[] byteArray = SHA256.HashData(binaryData.ToMemory().Span); diff --git a/src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs b/src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs index c99839da..69215081 100644 --- a/src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs +++ b/src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs @@ -1,5 +1,5 @@ - -using Serilog; +using Serilog; +using System.Text; using System.Text.RegularExpressions; namespace AntSK.Domain.Utils @@ -17,12 +17,19 @@ protected override async Task SendAsync(HttpRequestMessage UriBuilder uriBuilder; Regex regex = new Regex(@"(https?)://([^/:]+)(:\d+)?/(.*)"); Match match = regex.Match(_endPoint); - if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" && request.Content != null) + string guid = Guid.NewGuid().ToString(); + var mediaType = request.Content.Headers.ContentType.MediaType; + string requestBody = (await request.Content.ReadAsStringAsync()).Unescape(); + var uncaseBody = new StringContent(requestBody, Encoding.UTF8, mediaType); + request.Content = uncaseBody; + + if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").ConvertToString() != "Production") { - string requestBody = await request.Content.ReadAsStringAsync(); + //生产环境根据环境变量可去关闭日志 //便于调试查看请求prompt - Log.Information(requestBody); + Log.Information("{Message}", $"【模型服务接口调用-{guid},host:{_endPoint}】:{Environment.NewLine}{requestBody}"); } + if (match.Success) { string xieyi = match.Groups[1].Value; @@ -72,7 +79,11 @@ protected override async Task SendAsync(HttpRequestMessage // 接着,调用基类的 SendAsync 方法将你的修改后的请求发出去 HttpResponseMessage response = await base.SendAsync(request, cancellationToken); - + if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").ConvertToString() != "Production") + { + string responseContent = requestBody.IsStream() ? response.Content.ReadAsStringAsync().Result : response.Content.ReadAsStringAsync().Result.Unescape(); + Log.Information("{Message}", $"【模型服务接口返回-{guid},host:{_endPoint}】:{Environment.NewLine}{responseContent}"); + } return response; } } @@ -84,7 +95,7 @@ public static HttpClient GetHttpClient(string endPoint) { var handler = new OpenAIHttpClientHandler(endPoint.ConvertToString()); var httpClient = new HttpClient(handler); - httpClient.Timeout = TimeSpan.FromMinutes(5); + httpClient.Timeout = TimeSpan.FromMinutes(10); return httpClient; } }