feat(openai): 新增 v2 版机器人配置相关接口

This commit is contained in:
Fu Diwei
2024-06-05 17:49:28 +08:00
parent a92a602a71
commit ea25c0bf24
25 changed files with 546 additions and 19 deletions

View File

@@ -0,0 +1,33 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
public static class WechatOpenAIClientExecuteAsyncExtensions
{
/// <summary>
/// <para>异步调用 [POST] /v2/async/fetch 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/fetch.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.AsyncFetchV2Response> ExecuteAsyncFetchV2Async(this WechatOpenAIClient client, Models.AsyncFetchV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));
IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "async", "fetch");
return await client.SendFlurlRequestAsJsonAsync<Models.AsyncFetchV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}

View File

@@ -0,0 +1,77 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
public static class WechatOpenAIClientExecuteBotExtensions
{
/// <summary>
/// <para>异步调用 [POST] /v2/bot/import/json 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/import.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotImportJsonV2Response> ExecuteBotImportJsonV2Async(this WechatOpenAIClient client, Models.BotImportJsonV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));
IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "import", "json");
return await client.SendFlurlRequestAsJsonAsync<Models.BotImportJsonV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// <para>异步调用 [POST] /v2/bot/publish 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/publish.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotPublishV2Response> ExecuteBotPublishV2Async(this WechatOpenAIClient client, Models.BotPublishV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));
IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "publish");
return await client.SendFlurlRequestAsJsonAsync<Models.BotPublishV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// <para>异步调用 [POST] /v2/bot/effective_progress 接口。</para>
/// <para>
/// REF: <br/>
/// <![CDATA[ https://developers.weixin.qq.com/doc/aispeech/confapi/dialog/bot/progress.html ]]>
/// </para>
/// </summary>
/// <param name="client"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<Models.BotEffectiveProgressV2Response> ExecuteBotEffectiveProgressV2Async(this WechatOpenAIClient client, Models.BotEffectiveProgressV2Request request, CancellationToken cancellationToken = default)
{
if (client is null) throw new ArgumentNullException(nameof(client));
if (request is null) throw new ArgumentNullException(nameof(request));
IFlurlRequest flurlReq = client
.CreateFlurlRequest(request, HttpMethod.Post, "v2", "bot", "effective_progress");
return await client.SendFlurlRequestAsJsonAsync<Models.BotEffectiveProgressV2Response>(flurlReq, data: request, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}

View File

@@ -30,15 +30,6 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Interceptors
_baseUrl = baseUrl;
_encodingAESKey = encodingAESKey;
_customEncryptedRequestPathMatcher = customEncryptedRequestPathMatcher;
// AES 密钥的长度不是 4 的倍数需要补齐,确保其始终为有效的 Base64 字符串
const int MULTI = 4;
int tLen = _encodingAESKey.Length;
int tRem = tLen % MULTI;
if (tRem > 0)
{
_encodingAESKey = _encodingAESKey.PadRight(tLen - tRem + MULTI, '=');
}
}
public override async Task BeforeCallAsync(HttpInterceptorContext context, CancellationToken cancellationToken = default)
@@ -83,7 +74,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Interceptors
}
context.FlurlCall.HttpRequestMessage!.Content?.Dispose();
context.FlurlCall.HttpRequestMessage!.Content = new ByteArrayContent(reqBytesEncrypted);
context.FlurlCall.HttpRequestMessage!.Content = new StringContent(Convert.ToBase64String(reqBytesEncrypted));
context.FlurlCall.Request.WithHeader(HttpHeaders.ContentType, MimeTypes.Text);
}
@@ -150,9 +141,10 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Interceptors
{
if (_customEncryptedRequestPathMatcher is not null)
return _customEncryptedRequestPathMatcher(relativeUrl);
return false;
}
return true;
return false;
}
}
}

View File

@@ -0,0 +1,15 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/async/fetch 接口的请求。</para>
/// </summary>
public class AsyncFetchV2Request : WechatOpenAIRequest
{
/// <summary>
/// 获取或设置任务 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("task_id")]
[System.Text.Json.Serialization.JsonPropertyName("task_id")]
public string TaskId { get; set; } = string.Empty;
}
}

View File

@@ -0,0 +1,133 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/async/fetch 接口的响应。</para>
/// </summary>
public class AsyncFetchV2Response : WechatOpenAIResponse<AsyncFetchV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
public static class Types
{
public class Intent
{
/// <summary>
/// 获取或设置意图 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("id")]
[System.Text.Json.Serialization.JsonPropertyName("id")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.TextualNumberReadOnlyConverter))]
public long IntentId { get; set; }
/// <summary>
/// 获取或设置意图名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")]
public string Name { get; set; } = default!;
}
public class FailedSkill
{
/// <summary>
/// 获取或设置技能名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("name")]
[System.Text.Json.Serialization.JsonPropertyName("name")]
public string Name { get; set; } = default!;
}
public class SucceededSkill : FailedSkill
{
/// <summary>
/// 获取或设置技能 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("id")]
[System.Text.Json.Serialization.JsonPropertyName("id")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.TextualNumberReadOnlyConverter))]
public long SkillId { get; set; }
/// <summary>
/// 获取或设置意图列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("intents")]
[System.Text.Json.Serialization.JsonPropertyName("intents")]
public Types.Intent[] IntentList { get; set; } = default!;
}
}
/// <summary>
/// 获取或设置任务状态。
/// </summary>
[Newtonsoft.Json.JsonProperty("state")]
[System.Text.Json.Serialization.JsonPropertyName("state")]
public int State { get; set; }
/// <summary>
/// 获取或设置任务进度(单位:百分数)。
/// </summary>
[Newtonsoft.Json.JsonProperty("progress")]
[System.Text.Json.Serialization.JsonPropertyName("progress")]
public int Progress { get; set; }
/// <summary>
/// 获取或设置任务开始时间戳。
/// </summary>
[Newtonsoft.Json.JsonProperty("start")]
[System.Text.Json.Serialization.JsonPropertyName("start")]
public long StartTimestamp { get; set; }
/// <summary>
/// 获取或设置任务结束时间戳。
/// </summary>
[Newtonsoft.Json.JsonProperty("end")]
[System.Text.Json.Serialization.JsonPropertyName("end")]
public long EndTimestamp { get; set; }
/// <summary>
/// 获取或设置导出任务的 URL。
/// </summary>
[Newtonsoft.Json.JsonProperty("url")]
[System.Text.Json.Serialization.JsonPropertyName("url")]
public string? Url { get; set; }
/// <summary>
/// 获取或设置导入的总数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("total_count")]
[System.Text.Json.Serialization.JsonPropertyName("total_count")]
public int TotalCount { get; set; }
/// <summary>
/// 获取或设置导入失败的数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("fail_count")]
[System.Text.Json.Serialization.JsonPropertyName("fail_count")]
public int FailedCount { get; set; }
/// <summary>
/// 获取或设置导入失败的技能列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("fail_skill_info")]
[System.Text.Json.Serialization.JsonPropertyName("fail_skill_info")]
public Types.FailedSkill[]? FailedSkillList { get; set; }
/// <summary>
/// 获取或设置导入成功的数量。
/// </summary>
[Newtonsoft.Json.JsonProperty("success_count")]
[System.Text.Json.Serialization.JsonPropertyName("success_count")]
public int SucceededCount { get; set; }
/// <summary>
/// 获取或设置导入成功的技能列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("success_skill_info")]
[System.Text.Json.Serialization.JsonPropertyName("success_skill_info")]
public Types.SucceededSkill[]? SucceededSkillList { get; set; }
}
}
}
}

View File

@@ -0,0 +1,16 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/effective_progress 接口的请求。</para>
/// </summary>
public class BotEffectiveProgressV2Request : WechatOpenAIRequest
{
/// <summary>
/// 获取或设置查询环境。
/// <para>默认值:"online"</para>
/// </summary>
[Newtonsoft.Json.JsonProperty("env")]
[System.Text.Json.Serialization.JsonPropertyName("env")]
public string Environment { get; set; } = "online";
}
}

View File

@@ -0,0 +1,35 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/effective_progress 接口的响应。</para>
/// </summary>
public class BotEffectiveProgressV2Response : WechatOpenAIResponse<BotEffectiveProgressV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
/// <summary>
/// 获取或设置任务状态。
/// </summary>
[Newtonsoft.Json.JsonProperty("status")]
[System.Text.Json.Serialization.JsonPropertyName("status")]
public int Status { get; set; }
/// <summary>
/// 获取或设置任务进度(单位:百分数)。
/// </summary>
[Newtonsoft.Json.JsonProperty("progress")]
[System.Text.Json.Serialization.JsonPropertyName("progress")]
public int Progress { get; set; }
/// <summary>
/// 获取或设置任务结束时间字符串。
/// </summary>
[Newtonsoft.Json.JsonProperty("end_time")]
[System.Text.Json.Serialization.JsonPropertyName("end_time")]
public string? EndTimeString { get; set; }
}
}
}
}

View File

@@ -0,0 +1,81 @@
using System.Collections.Generic;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/import/json 接口的请求。</para>
/// </summary>
public class BotImportJsonV2Request : WechatOpenAIRequest
{
public static class Types
{
public class Skill
{
/// <summary>
/// 获取或设置技能名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("skill")]
[System.Text.Json.Serialization.JsonPropertyName("skill")]
public string SkillName { get; set; } = string.Empty;
/// <summary>
/// 获取或设置原意图名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("old_intent")]
[System.Text.Json.Serialization.JsonPropertyName("old_intent")]
public string? OldIntentName { get; set; }
/// <summary>
/// 获取或设置意图名称。
/// </summary>
[Newtonsoft.Json.JsonProperty("intent")]
[System.Text.Json.Serialization.JsonPropertyName("intent")]
public string IntentName { get; set; } = string.Empty;
/// <summary>
/// 获取或设置相似度。
/// </summary>
[Newtonsoft.Json.JsonProperty("threshold")]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.Common.TextualNumberConverter))]
[System.Text.Json.Serialization.JsonPropertyName("threshold")]
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.Common.TextualNumberConverter))]
public decimal Threshold { get; set; }
/// <summary>
/// 获取或设置是否关闭。
/// </summary>
[Newtonsoft.Json.JsonProperty("disable")]
[System.Text.Json.Serialization.JsonPropertyName("disable")]
public bool IsDisabled { get; set; }
/// <summary>
/// 获取或设置问题列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("questions")]
[System.Text.Json.Serialization.JsonPropertyName("questions")]
public IList<string> QuestionList { get; set; } = new List<string>();
/// <summary>
/// 获取或设置回答列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("answers")]
[System.Text.Json.Serialization.JsonPropertyName("answers")]
public IList<string> AnswerList { get; set; } = new List<string>();
}
}
/// <summary>
/// 获取或设置导入模式。
/// </summary>
[Newtonsoft.Json.JsonProperty("mode")]
[System.Text.Json.Serialization.JsonPropertyName("mode")]
public int Mode { get; set; }
/// <summary>
/// 获取或设置技能列表。
/// </summary>
[Newtonsoft.Json.JsonProperty("data")]
[System.Text.Json.Serialization.JsonPropertyName("data")]
public IList<Types.Skill> SkillList { get; set; } = new List<Types.Skill>();
}
}

View File

@@ -0,0 +1,21 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/import/json 接口的响应。</para>
/// </summary>
public class BotImportJsonV2Response : WechatOpenAIResponse<BotImportJsonV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
/// <summary>
/// 获取或设置任务 ID。
/// </summary>
[Newtonsoft.Json.JsonProperty("task_id")]
[System.Text.Json.Serialization.JsonPropertyName("task_id")]
public string TaskId { get; set; } = default!;
}
}
}
}

View File

@@ -0,0 +1,9 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/publish 接口的请求。</para>
/// </summary>
public class BotPublishV2Request : WechatOpenAIRequest
{
}
}

View File

@@ -0,0 +1,15 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
/// <summary>
/// <para>表示 [POST] /v2/bot/publish 接口的响应。</para>
/// </summary>
public class BotPublishV2Response : WechatOpenAIResponse<BotPublishV2Response.Types.Data>
{
public static class Types
{
public class Data : WechatOpenAIResponseData
{
}
}
}
}

View File

@@ -10,6 +10,6 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
/// </summary>
[Newtonsoft.Json.JsonProperty("account")]
[System.Text.Json.Serialization.JsonPropertyName("account")]
public string Account { get; set; } = string.Empty;
public string AccountId { get; set; } = string.Empty;
}
}

View File

@@ -7,7 +7,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Models
{
public static class Types
{
public class Data
public class Data : WechatOpenAIResponseData
{
/// <summary>
/// 获取或设置接口访问令牌。

View File

@@ -25,7 +25,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Settings
AppId = options.AppId;
Token = options.Token;
EncodingAESKey = options.EncodingAESKey;
EncodingAESKey = PadEncodingAESKey(options.EncodingAESKey);
}
internal Credentials(WechatChatbotClientOptions options)
@@ -34,7 +34,21 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Settings
AppId = options.AppId;
Token = options.Token;
EncodingAESKey = options.EncodingAESKey;
EncodingAESKey = PadEncodingAESKey(options.EncodingAESKey);
}
private string PadEncodingAESKey(string encodingAESKey)
{
// AES 密钥的长度不是 4 的倍数需要补齐,确保其始终为有效的 Base64 字符串
const int MULTI = 4;
int tLen = encodingAESKey.Length;
int tRem = tLen % MULTI;
if (tRem > 0)
{
encodingAESKey = encodingAESKey.PadRight(tLen - tRem + MULTI, '=');
}
return encodingAESKey;
}
}
}

View File

@@ -43,11 +43,11 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
Interceptors.Add(new Interceptors.WechatOpenAIRequestEncryptionInterceptor(
baseUrl: FlurlClient.BaseUrl,
encodingAESKey: options.EncodingAESKey,
encodingAESKey: Credentials.EncodingAESKey,
customEncryptedRequestPathMatcher: options.CustomEncryptedRequestPathMatcher
));
Interceptors.Add(new Interceptors.WechatOpenAIRequestSigningInterceptor(
token: options.Token
token: Credentials.Token
));
}

View File

@@ -43,7 +43,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
/// 表示微信智能对话 API 响应的泛型基类。
/// </summary>
public abstract class WechatOpenAIResponse<TData> : WechatOpenAIResponse
where TData : class
where TData : WechatOpenAIResponseData
{
/// <summary>
/// 获取微信智能对话 API 返回的数据。
@@ -52,4 +52,17 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI
[System.Text.Json.Serialization.JsonPropertyName("data")]
public virtual TData? Data { get; set; }
}
/// <summary>
/// 表示微信智能对话 API 响应的返回数据基类。
/// </summary>
public abstract class WechatOpenAIResponseData
{
/// <summary>
/// 获取或设置错误详情。
/// </summary>
[Newtonsoft.Json.JsonProperty("err_detail")]
[System.Text.Json.Serialization.JsonPropertyName("err_detail")]
public virtual string? ErrorDetail { get; set; }
}
}