Skip to content

Commit

Permalink
add 处理请求编码
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzeyu91 committed Aug 5, 2024
1 parent 4ef398b commit 21d7c71
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
49 changes: 48 additions & 1 deletion src/AntSK.Domain/Utils/ConvertUtils.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -263,6 +266,50 @@ public static bool ComparisonIgnoreCase(this string s, string value)
return s.Equals(value, StringComparison.OrdinalIgnoreCase);
}


/// <summary>
/// \uxxxx转中文,保留换行符号
/// </summary>
/// <param name="unicodeString"></param>
/// <returns></returns>
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 "";
}
}


/// <summary>
/// 是否为流式请求
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
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);
Expand Down
25 changes: 18 additions & 7 deletions src/AntSK.Domain/Utils/OpenAIHttpClientHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

using Serilog;
using Serilog;
using System.Text;
using System.Text.RegularExpressions;

namespace AntSK.Domain.Utils
Expand All @@ -17,12 +17,19 @@ protected override async Task<HttpResponseMessage> 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;
Expand Down Expand Up @@ -72,7 +79,11 @@ protected override async Task<HttpResponseMessage> 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;
}
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 21d7c71

Please sign in to comment.