feat(openai): 导入对话开放平台项目

This commit is contained in:
Fu Diwei
2021-10-07 21:19:24 +08:00
parent 9aa017fdfe
commit 038d1d1293
16 changed files with 586 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461; netstandard2.0; net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>
</PropertyGroup>
<PropertyGroup>
<PackageId>SKIT.FlurlHttpClient.Wechat.OpenAI</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat</PackageProjectUrl>
<PackageTags>Flurl.Http Wechat Weixin MicroMessage WxOpenAI 微信 微信智能对话 微信对话开放平台 微信智能对话开放平台 智能对话平台</PackageTags>
<Version>0.1.0-alpha</Version>
<Description>基于 Flurl.Http 的微信对话开放平台(微信智能对话) API 客户端。</Description>
<Authors>Fu Diwei</Authors>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git</RepositoryUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Web" Condition="'$(TargetFramework)' == 'net461'" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SKIT.FlurlHttpClient.Wechat\SKIT.FlurlHttpClient.Wechat.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Settings
{
public class Credentials
{
/// <summary>
/// 初始化客户端时 <see cref="WechatOpenAIClientOptions.ClientId"/> 的副本。
/// </summary>
public string ClientId { get; }
/// <summary>
/// 初始化客户端时 <see cref="WechatOpenAIClientOptions.ClientKey"/> 的副本。
/// </summary>
public string ClientKey { get; }
internal Credentials(WechatOpenAIClientOptions options)
{
if (options == null) throw new ArgumentNullException(nameof(options));
ClientId = options.ClientId;
ClientKey = options.ClientKey;
}
}
}

View File

@@ -0,0 +1,147 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// 一个微信智能对话 API HTTP 客户端。
/// </summary>
public class WechatOpenAIClient : CommonClientBase, IWechatClient
{
/// <summary>
/// 获取当前客户端使用的微信公众平台凭证。
/// </summary>
public Settings.Credentials Credentials { get; }
/// <summary>
/// 用指定的配置项初始化 <see cref="WechatOpenAIClient"/> 类的新实例。
/// </summary>
/// <param name="options">配置项。</param>
public WechatOpenAIClient(WechatOpenAIClientOptions options)
: base()
{
if (options == null) throw new ArgumentNullException(nameof(options));
Credentials = new Settings.Credentials(options);
FlurlClient.BaseUrl = options.Endpoints ?? WechatOpenAIEndpoints.DEFAULT;
FlurlClient.WithTimeout(TimeSpan.FromMilliseconds(options.Timeout));
}
/// <summary>
/// 用指定的微信智能对话 ClientId 和微信智能对话 ClientKey 初始化 <see cref="WechatOpenAIClient"/> 类的新实例。
/// </summary>
/// <param name="clientId">微信智能对话 ClientId。</param>
/// <param name="clientKey">微信智能对话 ClientKey。</param>
public WechatOpenAIClient(string clientId, string clientKey)
: this(new WechatOpenAIClientOptions() { ClientId = clientId, ClientKey = clientKey })
{
}
/// <summary>
/// 使用当前客户端生成一个新的 <see cref="IFlurlRequest"/> 对象。
/// </summary>
/// <param name="request"></param>
/// <param name="method"></param>
/// <param name="urlSegments"></param>
/// <returns></returns>
public IFlurlRequest CreateRequest(WechatOpenAIRequest request, HttpMethod method, params object[] urlSegments)
{
IFlurlRequest flurlRequest = FlurlClient.Request(urlSegments).WithVerb(method);
if (request.Timeout.HasValue)
{
flurlRequest.WithTimeout(TimeSpan.FromMilliseconds(request.Timeout.Value));
}
if (request.RequestId != null)
{
flurlRequest.WithHeader("request_id", request.RequestId);
}
if (request.BotId != null)
{
flurlRequest.WithHeader("wxbot_bid", request.BotId);
}
if (request.AccessToken != null)
{
flurlRequest.WithHeader("X-OPENAI-TOKEN", request.AccessToken);
}
return flurlRequest;
}
/// <summary>
/// 异步发起请求。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="flurlRequest"></param>
/// <param name="httpContent"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<T> SendRequestAsync<T>(IFlurlRequest flurlRequest, HttpContent? httpContent = null, CancellationToken cancellationToken = default)
where T : WechatOpenAIResponse, new()
{
try
{
using IFlurlResponse flurlResponse = await base.SendRequestAsync(flurlRequest, httpContent, cancellationToken).ConfigureAwait(false);
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
}
catch (FlurlHttpException ex)
{
throw new WechatOpenAIException(ex.Message, ex);
}
}
/// <summary>
/// 异步发起请求。
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="flurlRequest"></param>
/// <param name="data"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<T> SendRequestWithJsonAsync<T>(IFlurlRequest flurlRequest, object? data = null, CancellationToken cancellationToken = default)
where T : WechatOpenAIResponse, new()
{
try
{
using IFlurlResponse flurlResponse = await base.SendRequestWithJsonAsync(flurlRequest, data, cancellationToken).ConfigureAwait(false);
return await GetResposneAsync<T>(flurlResponse).ConfigureAwait(false);
}
catch (FlurlHttpException ex)
{
throw new WechatOpenAIException(ex.Message, ex);
}
}
private async Task<T> GetResposneAsync<T>(IFlurlResponse flurlResponse)
where T : WechatOpenAIResponse, new()
{
string contentType = flurlResponse.Headers.GetAll("Content-Type").FirstOrDefault() ?? string.Empty;
bool contentTypeIsNotJson =
(flurlResponse.StatusCode != (int)HttpStatusCode.OK) ||
(!contentType.StartsWith("application/json") && !contentType.StartsWith("text/json") && !contentType.StartsWith("text/plain"));
T result = contentTypeIsNotJson ? new T() : await flurlResponse.GetJsonAsync<T>().ConfigureAwait(false);
result.RawStatus = flurlResponse.StatusCode;
result.RawHeaders = new ReadOnlyDictionary<string, string>(
flurlResponse.Headers
.GroupBy(e => e.Name)
.ToDictionary(
k => k.Key,
v => string.Join(", ", v.Select(e => e.Value))
)
);
result.RawBytes = await flurlResponse.ResponseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
return result;
}
}
}

View File

@@ -0,0 +1,30 @@
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// 一个用于构造 <see cref="WechatOpenAIClient"/> 时使用的配置项。
/// </summary>
public class WechatOpenAIClientOptions
{
/// <summary>
/// 获取或设置请求超时时间(单位:毫秒)。
/// <para>默认值30000</para>
/// </summary>
public int Timeout { get; set; } = 30 * 1000;
/// <summary>
/// 获取或设置微微信智能对话 API 域名。
/// <para>默认值:<see cref="WechatOpenAIEndpoints.DEFAULT"/></para>
/// </summary>
public string Endpoints { get; set; } = WechatOpenAIEndpoints.DEFAULT;
/// <summary>
/// 获取或设置微信智能对话 ClientId。
/// </summary>
public string ClientId { get; set; } = default!;
/// <summary>
/// 获取或设置微信智能对话 ClientKey。
/// </summary>
public string ClientKey { get; set; } = default!;
}
}

View File

@@ -0,0 +1,15 @@
using System;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// <para>微信智能对话 API 接口域名。</para>
/// </summary>
public static class WechatOpenAIEndpoints
{
/// <summary>
/// 主域名(默认)。
/// </summary>
public const string DEFAULT = "https://openaiapi.weixin.qq.com";
}
}

View File

@@ -0,0 +1,27 @@
using System;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// 当调用微信智能对话 API 出错时引发的异常。
/// </summary>
public class WechatOpenAIException : CommonExceptionBase
{
/// <inheritdoc/>
public WechatOpenAIException()
{
}
/// <inheritdoc/>
public WechatOpenAIException(string message)
: base(message)
{
}
/// <inheritdoc/>
public WechatOpenAIException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// 表示微信智能对话 API 请求的基类。
/// </summary>
public abstract class WechatOpenAIRequest : IWechatRequest
{
/// <summary>
/// 获取或设置请求超时时间(单位:毫秒)。如果不指定将使用构造 <see cref="WechatOpenAIClient"/> 时的 <see cref="WechatOpenAIClientOptions.Timeout"/> 参数,这在需要指定特定耗时请求(比如上传或下载文件)的超时时间时很有用。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual int? Timeout { get; set; }
/// <summary>
/// 获取或设置请求唯一标识。如果不指定将有系统自动生成。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual string? RequestId { get; set; }
/// <summary>
/// 获取或设置 Bot ID。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual string? BotId { get; set; }
/// <summary>
/// 获取或设置请求令牌。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public virtual string? AccessToken { get; set; }
}
}

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
/// <summary>
/// 表示微信智能对话 API 响应的基类。
/// </summary>
public abstract class WechatOpenAIResponse : IWechatResponse
{
/// <summary>
/// 获取原始的 HTTP 响应状态码。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public int RawStatus { get; internal set; }
/// <summary>
/// 获取原始的 HTTP 响应表头集合。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public IDictionary<string, string> RawHeaders { get; internal set; } = default!;
/// <summary>
/// 获取原始的 HTTP 响应正文。
/// </summary>
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
public byte[] RawBytes { get; internal set; } = default!;
/// <summary>
/// 获取微信智能对话 API 返回的错误码。
/// </summary>
[Newtonsoft.Json.JsonProperty("code")]
[System.Text.Json.Serialization.JsonPropertyName("code")]
public virtual int ErrorCode { get; set; }
/// <summary>
/// 获取微信智能对话 API 返回的错误描述。
/// </summary>
[Newtonsoft.Json.JsonProperty("msg")]
[System.Text.Json.Serialization.JsonPropertyName("msg")]
public virtual string? ErrorMessage { get; set; }
/// <summary>
/// 获取微信智能对话 API 返回的请求唯一标识。
/// </summary>
[Newtonsoft.Json.JsonProperty("request_id")]
[System.Text.Json.Serialization.JsonPropertyName("request_id")]
public virtual string? RequestId { get; set; }
/// <summary>
/// 获取一个值,该值指示调用微信 API 是否成功(即 HTTP 状态码为 200、且 errcode 值为 0
/// </summary>
/// <returns></returns>
public virtual bool IsSuccessful()
{
return RawStatus == 200 && ErrorCode == 0;
}
}
/// <summary>
/// 表示微信智能对话 API 响应的泛型基类。
/// </summary>
public abstract class WechatOpenAIResponse<TData>
where TData : class, new()
{
/// <summary>
/// 获取微信智能对话 API 返回的数据。
/// </summary>
[Newtonsoft.Json.JsonProperty("data")]
[System.Text.Json.Serialization.JsonPropertyName("data")]
public virtual TData? Data { get; set; }
}
}