From fc1b27daa3dcf4e2e13c29b5170ac3e1c65075bb Mon Sep 17 00:00:00 2001 From: arthuridea w <2337583+arthuridea@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:16:52 +0800 Subject: [PATCH] fix: refactor code. change project structure. --- README.md | 25 ++- .../BaiduErnieVilgApiService.cs | 3 +- .../ErnieVilgDependencyInjection.cs | 2 +- .../Models/PaintApplyRequest.cs | 2 +- .../Models/PaintApplyResponse.cs | 2 +- .../Models/PaintResultResponse.cs | 2 +- .../BaiduWenxinApiService.cs | 14 +- .../LLMService.Baidu.Wenxinworkshop.csproj | 4 - .../Models/BaiduApiChatRequest.cs | 5 +- .../Models/BaiduChatApiResponse.cs | 54 +++++ .../Models/BaiduWenxinChatResponse.cs | 93 +++++++++ .../ChatGPTApiService.cs | 8 - .../Model/OpenAIChatResponse.cs | 29 ++- .../ChatService/ChatServiceBase.cs | 12 +- .../Models/BaiduWenxinChatResponse.cs | 187 ------------------ .../Models/ChatApiTokenUsage.cs | 7 +- src/LLMService.Shared/Models/IChatResponse.cs | 13 +- .../Configuration/AppSwaggerGenOption.cs | 4 +- .../Controller/v1_0/BaiduApiController.cs | 23 +-- .../Controller/v1_0/ChatGPTApiController.cs | 5 +- src/LLMServiceHub/Pages/Index.cshtml | 9 +- src/LLMServiceHub/Startup.cs | 9 +- src/LLMServiceHub/wwwroot/js/index.js | 2 +- src/LLMServiceHub/wwwroot/js/index.min.js | 2 +- 24 files changed, 241 insertions(+), 275 deletions(-) rename src/{LLMService.Shared => LLMService.Baidu.ErnieVilg}/Models/PaintApplyRequest.cs (99%) rename src/{LLMService.Shared => LLMService.Baidu.ErnieVilg}/Models/PaintApplyResponse.cs (96%) rename src/{LLMService.Shared => LLMService.Baidu.ErnieVilg}/Models/PaintResultResponse.cs (99%) rename src/{LLMService.Shared => LLMService.Baidu.Wenxinworkshop}/Models/BaiduApiChatRequest.cs (85%) create mode 100644 src/LLMService.Baidu.Wenxinworkshop/Models/BaiduChatApiResponse.cs create mode 100644 src/LLMService.Baidu.Wenxinworkshop/Models/BaiduWenxinChatResponse.cs delete mode 100644 src/LLMService.Shared/Models/BaiduWenxinChatResponse.cs diff --git a/README.md b/README.md index 5e576c8..bf185e2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# 百度文心千帆大模型(文心一言、智能作画)SDK (.net6+) +# OpenAI ChatGPT、文心一言、百度智能作画 SDK (.net6.0+) ![.NET6.0](https://badgen.net/badge/.NET/6.0/green) ![.NET8.0](https://badgen.net/badge/.NET/8.0/green) ![license](https://badgen.net/badge/license/mit) -`LLMService` 是一个非百度官方的开源项目.提供了接口方法能够在`.NET6.0` `.NET8.0`项目中使用接口调用百度文心大模型和百度智能绘图高级版功能. +`Aeex.LLMService` 是一个非官方的开源项目.提供了在`.NET6.0` `.NET8.0`项目中使用接口调用OpenAI ChatGPT、百度文心大模型和百度智能绘图高级版功能. ## 已知问题 @@ -12,6 +12,10 @@ 通过`Nuget`安装. +### OpenAI ChatGPT + + + ### 文心大模型 [Nuget地址](https://www.nuget.org/packages/Aeex.LLMService.Baidu.Wenxin/) @@ -53,9 +57,13 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config // inject image storage provider services.AddTransient(); // inject chat data provider - services.AddTransient, ChatDataProvider>(); + // for baidu chat caching + services.AddTransient, ChatDataProvider>(); + // for chatgpt + services.AddTransient>, ChatDataProvider>>(); - // inject baidu api service + // inject service + services.AddChatGPT(config); services.AddWenxinworkshop(config); services.AddErnieVilg(config); @@ -116,19 +124,24 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config ## 支持的模型 -`LLMService` 支持的语言模型列表如下: +`Aeex.LLMService` 支持的语言模型列表如下: | 模型 | 描述 | | --- | --- | | ERNIEBot | 百度自行研发的大语言模型,覆盖海量中文数据,具有更强的对话问答、内容创作生成等能力。 | | ERNIEBotTurbo | 百度自行研发的大语言模型,覆盖海量中文数据,具有更强的对话问答、内容创作生成等能力,响应速度更快。 | | ERNIE-Bot-4 | ERNIE-Bot-4是百度自行研发的大语言模型,覆盖海量中文数据,具有更强的对话问答、内容创作生成等能力。 | +| GPT_3_5_TURBO| | +| GPT_3_5_TURBO_1106| | +| GPT_4| | +| GPT_4_32K| | ## 项目说明 | 项目 | 说明 | | --- | --- | -| `LLMService.Shared` | 公共模型和接口及扩展方法 | +| `LLMService.Shared` | 公共模型和接口及扩展方法 | +| `LLMService.OpenAI.ChatGPT` | ChatGPT大模型项目,目前支持文字对话| | `LLMService.Baidu.WenxinWorkshop` | 百度千帆大模型项目,目前提供ErnieBot系列API调用支持 | | `LLMService.Baidu.ErnieVilg` | 百度智能创作模块,目前支持AI作画高级版2 | | `LLMServiceHub` | `.NET8 MVC WebApi` 项目示例,填入自己的 `application id`可以完美调用,实现了基本的接口 | \ No newline at end of file diff --git a/src/LLMService.Baidu.ErnieVilg/BaiduErnieVilgApiService.cs b/src/LLMService.Baidu.ErnieVilg/BaiduErnieVilgApiService.cs index cabdde5..83b9a3c 100644 --- a/src/LLMService.Baidu.ErnieVilg/BaiduErnieVilgApiService.cs +++ b/src/LLMService.Baidu.ErnieVilg/BaiduErnieVilgApiService.cs @@ -1,4 +1,5 @@ -using LLMService.Shared; +using LLMService.Baidu.ErnieVilg.Models; +using LLMService.Shared; using LLMService.Shared.Extensions; using LLMService.Shared.Models; using LLMService.Shared.ServiceInterfaces; diff --git a/src/LLMService.Baidu.ErnieVilg/Extensions/ErnieVilgDependencyInjection.cs b/src/LLMService.Baidu.ErnieVilg/Extensions/ErnieVilgDependencyInjection.cs index 9cd71dc..b5fd466 100644 --- a/src/LLMService.Baidu.ErnieVilg/Extensions/ErnieVilgDependencyInjection.cs +++ b/src/LLMService.Baidu.ErnieVilg/Extensions/ErnieVilgDependencyInjection.cs @@ -1,8 +1,8 @@ using LLMService.Baidu.ErnieVilg; +using LLMService.Baidu.ErnieVilg.Models; using LLMService.Shared; using LLMService.Shared.Authentication.Handlers; using LLMService.Shared.Authentication.Models; -using LLMService.Shared.Models; using LLMService.Shared.ServiceInterfaces; using Microsoft.Extensions.Configuration; diff --git a/src/LLMService.Shared/Models/PaintApplyRequest.cs b/src/LLMService.Baidu.ErnieVilg/Models/PaintApplyRequest.cs similarity index 99% rename from src/LLMService.Shared/Models/PaintApplyRequest.cs rename to src/LLMService.Baidu.ErnieVilg/Models/PaintApplyRequest.cs index 43536fd..bdf995f 100644 --- a/src/LLMService.Shared/Models/PaintApplyRequest.cs +++ b/src/LLMService.Baidu.ErnieVilg/Models/PaintApplyRequest.cs @@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; -namespace LLMService.Shared.Models +namespace LLMService.Baidu.ErnieVilg.Models { #nullable enable /// diff --git a/src/LLMService.Shared/Models/PaintApplyResponse.cs b/src/LLMService.Baidu.ErnieVilg/Models/PaintApplyResponse.cs similarity index 96% rename from src/LLMService.Shared/Models/PaintApplyResponse.cs rename to src/LLMService.Baidu.ErnieVilg/Models/PaintApplyResponse.cs index 9a01430..f6b7273 100644 --- a/src/LLMService.Shared/Models/PaintApplyResponse.cs +++ b/src/LLMService.Baidu.ErnieVilg/Models/PaintApplyResponse.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace LLMService.Shared.Models +namespace LLMService.Baidu.ErnieVilg.Models { /// /// diff --git a/src/LLMService.Shared/Models/PaintResultResponse.cs b/src/LLMService.Baidu.ErnieVilg/Models/PaintResultResponse.cs similarity index 99% rename from src/LLMService.Shared/Models/PaintResultResponse.cs rename to src/LLMService.Baidu.ErnieVilg/Models/PaintResultResponse.cs index a9ae8ed..dddb5a9 100644 --- a/src/LLMService.Shared/Models/PaintResultResponse.cs +++ b/src/LLMService.Baidu.ErnieVilg/Models/PaintResultResponse.cs @@ -1,7 +1,7 @@ using System.ComponentModel; using System.Text.Json.Serialization; -namespace LLMService.Shared.Models +namespace LLMService.Baidu.ErnieVilg.Models { /// /// diff --git a/src/LLMService.Baidu.Wenxinworkshop/BaiduWenxinApiService.cs b/src/LLMService.Baidu.Wenxinworkshop/BaiduWenxinApiService.cs index 16301c3..d229d74 100644 --- a/src/LLMService.Baidu.Wenxinworkshop/BaiduWenxinApiService.cs +++ b/src/LLMService.Baidu.Wenxinworkshop/BaiduWenxinApiService.cs @@ -1,15 +1,11 @@ -using LLMService.Shared; +using LLMService.Baidu.Wenxinworkshop.Models; +using LLMService.Shared.Authentication.Models; +using LLMService.Shared.ChatService; using LLMService.Shared.Models; -using LLMService.Shared.Extensions; using LLMService.Shared.ServiceInterfaces; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; -using System.Net.Http.Json; -using System.Text.Json; -using LLMService.Shared.ChatService; -using LLMService.Shared.Authentication.Models; using Microsoft.Extensions.Options; -using Microsoft.VisualBasic; namespace LLMService.Baidu.Wenxinworkshop { @@ -33,10 +29,10 @@ public interface IBaiduErniebotLLMService /// /// public class BaiduErniebotLLMService : - ChatServiceBase, IBaiduErniebotLLMService, IAIChatApiService + OAuth2BackendServiceConfig>, IBaiduErniebotLLMService, IAIChatApiService { /// /// Initializes a new instance of the class. diff --git a/src/LLMService.Baidu.Wenxinworkshop/LLMService.Baidu.Wenxinworkshop.csproj b/src/LLMService.Baidu.Wenxinworkshop/LLMService.Baidu.Wenxinworkshop.csproj index 33b5caf..2477b5f 100644 --- a/src/LLMService.Baidu.Wenxinworkshop/LLMService.Baidu.Wenxinworkshop.csproj +++ b/src/LLMService.Baidu.Wenxinworkshop/LLMService.Baidu.Wenxinworkshop.csproj @@ -23,8 +23,4 @@ - - - - diff --git a/src/LLMService.Shared/Models/BaiduApiChatRequest.cs b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduApiChatRequest.cs similarity index 85% rename from src/LLMService.Shared/Models/BaiduApiChatRequest.cs rename to src/LLMService.Baidu.Wenxinworkshop/Models/BaiduApiChatRequest.cs index 5878ba7..49b06c8 100644 --- a/src/LLMService.Shared/Models/BaiduApiChatRequest.cs +++ b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduApiChatRequest.cs @@ -1,7 +1,8 @@ -using System.ComponentModel.DataAnnotations; +using LLMService.Shared.Models; +using System.ComponentModel.DataAnnotations; using System.Text.Json.Serialization; -namespace LLMService.Shared.Models +namespace LLMService.Baidu.Wenxinworkshop.Models { /// /// 内部调用百度api的接口 diff --git a/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduChatApiResponse.cs b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduChatApiResponse.cs new file mode 100644 index 0000000..5d5b038 --- /dev/null +++ b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduChatApiResponse.cs @@ -0,0 +1,54 @@ +using LLMService.Shared.Models; +using System.Text.Json.Serialization; +using static LLMService.Shared.Models.LLMApiDefaults; + +namespace LLMService.Baidu.Wenxinworkshop.Models +{ + /// + /// baidu API response wrapper + /// + /// + public class BaiduChatApiResponse : IChatResponse + { + /// + /// Gets or sets the conversation identifier. + /// + /// + /// The conversation identifier. + /// + [JsonPropertyName("conversation_id")] + public string ConversationId { get; set; } = ""; + /// + /// 大模型 + /// 可选项:ERNIE-Bot-turbo(default)|ERNIE-Bot-4|ERNIE-Bot + /// + /// 2 + [JsonPropertyName("model")] + public LLM_ModelType ModelSchema { get; set; } = LLM_ModelType.ERNIE_BOT_TURBO; + /// + /// Gets or sets the result. + /// + /// + /// The result. + /// + [JsonPropertyName("llm_response_data")] + public BaiduWenxinChatResponse LLMResponseData { get; set; } + /// + /// Gets a value indicating whether [need clear history]. + /// + /// + /// true if [need clear history]; otherwise, false. + /// + [JsonPropertyName("need_clear_history")] + public bool NeedClearHistory => LLMResponseData?.NeedClearHistory ?? false; + + /// + /// latest aigc message. + /// + /// + /// The aigc message. + /// + [JsonPropertyName("aigc_message")] + public string AIGCMessage => LLMResponseData?.Result; + } +} diff --git a/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduWenxinChatResponse.cs b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduWenxinChatResponse.cs new file mode 100644 index 0000000..2c9627a --- /dev/null +++ b/src/LLMService.Baidu.Wenxinworkshop/Models/BaiduWenxinChatResponse.cs @@ -0,0 +1,93 @@ +using LLMService.Shared.Models; +using System.Text.Json.Serialization; + +namespace LLMService.Baidu.Wenxinworkshop.Models +{ + /// + /// baidu API response model + /// + public class BaiduWenxinChatResponse /*: IServerSentEventData*/ + { + /// + /// Gets or sets the identifier. + /// + /// + /// The identifier. + /// + [JsonPropertyName("id")] + public string Id { get; set; } + /// + /// Gets or sets the type of the object. + /// + /// + /// The type of the object. + /// + [JsonPropertyName("object")] + public string ObjectType { get; set; } + /// + /// Gets or sets the created. + /// + /// + /// The created. + /// + [JsonPropertyName("created")] + public long Created { get; set; } + /// + /// Gets or sets the sentence identifier. + /// + /// + /// The sentence identifier. + /// + [JsonPropertyName("sentence_id")] + public int SentenceId { get; set; } + /// + /// Gets or sets a value indicating whether this instance is end. + /// + /// + /// true if this instance is end; otherwise, false. + /// + [JsonPropertyName("is_end")] + public bool IsEnd { get; set; } + /// + /// Gets or sets a value indicating whether this instance is truncated. + /// + /// + /// true if this instance is truncated; otherwise, false. + /// + [JsonPropertyName("is_truncated")] + public bool IsTruncated { get; set; } + /// + /// Gets or sets the result. + /// + /// + /// The result. + /// + [JsonPropertyName("result")] + public string Result { get; set; } + /// + /// Gets or sets a value indicating whether [need clear history]. + /// + /// + /// true if [need clear history]; otherwise, false. + /// + [JsonPropertyName("need_clear_history")] + public bool NeedClearHistory { get; set; } + /// + /// Gets or sets the ban round. + /// + /// + /// The ban round. + /// + [JsonPropertyName("ban_round")] + public int BanRound { get; set; } + /// + /// Gets or sets the usage. + /// + /// + /// The usage. + /// + [JsonPropertyName("usage")] + public ChatApiTokenUsage Usage { get; set; } + } + +} diff --git a/src/LLMService.OpenAI.ChatGPT/ChatGPTApiService.cs b/src/LLMService.OpenAI.ChatGPT/ChatGPTApiService.cs index ddc0f3b..1293b58 100644 --- a/src/LLMService.OpenAI.ChatGPT/ChatGPTApiService.cs +++ b/src/LLMService.OpenAI.ChatGPT/ChatGPTApiService.cs @@ -1,5 +1,4 @@ using LLMService.OpenAI.ChatGPT.Model; -using LLMService.Shared; using LLMService.Shared.Authentication.Models; using LLMService.Shared.ChatService; using LLMService.Shared.Extensions; @@ -8,14 +7,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Net.Http.Json; -using System.Text; -using System.Text.Json; -using System.Threading.Tasks; namespace LLMService.OpenAI.ChatGPT { diff --git a/src/LLMService.OpenAI.ChatGPT/Model/OpenAIChatResponse.cs b/src/LLMService.OpenAI.ChatGPT/Model/OpenAIChatResponse.cs index f29c460..a16adbc 100644 --- a/src/LLMService.OpenAI.ChatGPT/Model/OpenAIChatResponse.cs +++ b/src/LLMService.OpenAI.ChatGPT/Model/OpenAIChatResponse.cs @@ -7,7 +7,7 @@ namespace LLMService.OpenAI.ChatGPT.Model /// /// /// - public class OpenAIChatResponse: IChatResponse + public class OpenAIChatResponse : IChatResponse { /// /// Gets or sets the result. @@ -15,8 +15,8 @@ public class OpenAIChatResponse: IChatResponse /// /// The result. /// - [JsonPropertyName("result")] - public OpenAIBackendResponseModel Result { get; set; } + [JsonPropertyName("llm_response_data")] + public OpenAIBackendResponseModel LLMResponseData { get; set; } /// /// Gets or sets the conversation identifier. /// @@ -42,6 +42,29 @@ public class OpenAIChatResponse: IChatResponse [JsonPropertyName("need_clear_history")] public bool NeedClearHistory { get; set; } + /// + /// latest aigc message. + /// + /// + /// The aigc message. + /// + [JsonPropertyName("aigc_message")] + public string AIGCMessage { + get + { + var ret = LLMResponseData?.Choices.FirstOrDefault(); + if(ret.Message == null) + { + return ret.Delta?.Content; + } + else + { + return ret.Message?.Content; + } + } + + } + } } diff --git a/src/LLMService.Shared/ChatService/ChatServiceBase.cs b/src/LLMService.Shared/ChatService/ChatServiceBase.cs index 1322a71..c222c4d 100644 --- a/src/LLMService.Shared/ChatService/ChatServiceBase.cs +++ b/src/LLMService.Shared/ChatService/ChatServiceBase.cs @@ -4,17 +4,9 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Net.Http.Json; -using System.Net.Mail; using System.Text; using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using System.Xml.Linq; namespace LLMService.Shared.ChatService { @@ -134,7 +126,7 @@ public async Task Chat(TRequestDto request, CancellationToken cancellationToken apiResponseData = await apiResponse.DeserializeAsync(logger: _logger); result.ConversationId = request.ConversationId; result.ModelSchema = request.ModelSchema; - result.Result = apiResponseData; + result.LLMResponseData = apiResponseData; //只有流模式会返回是否结束标识,在非流式请求中直接设置为true. //result.IsEnd = !request.Stream || result.IsEnd; @@ -312,7 +304,7 @@ private TResponseDto WrapData(string sseSection, TRequestDto request, StringBuil TResponseDto result = new(); result.ConversationId = request.ConversationId; result.ModelSchema = request.ModelSchema; - result.Result = ldata; + result.LLMResponseData = ldata; return result; //await GenerateSSEResponse(response, result, cancellationToken); diff --git a/src/LLMService.Shared/Models/BaiduWenxinChatResponse.cs b/src/LLMService.Shared/Models/BaiduWenxinChatResponse.cs deleted file mode 100644 index 9b9f780..0000000 --- a/src/LLMService.Shared/Models/BaiduWenxinChatResponse.cs +++ /dev/null @@ -1,187 +0,0 @@ -using System.Text.Json.Serialization; -using static LLMService.Shared.Models.LLMApiDefaults; - -namespace LLMService.Shared.Models -{ - /// - /// baidu API response model - /// - public class BaiduWenxinChatResponse /*: IServerSentEventData*/ - { - /// - /// Gets or sets the identifier. - /// - /// - /// The identifier. - /// - [JsonPropertyName("id")] - public string Id { get; set; } - /// - /// Gets or sets the type of the object. - /// - /// - /// The type of the object. - /// - [JsonPropertyName("object")] - public string ObjectType { get; set; } - /// - /// Gets or sets the created. - /// - /// - /// The created. - /// - [JsonPropertyName("created")] - public long Created { get; set; } - /// - /// Gets or sets the sentence identifier. - /// - /// - /// The sentence identifier. - /// - [JsonPropertyName("sentence_id")] - public int SentenceId { get; set; } - /// - /// Gets or sets a value indicating whether this instance is end. - /// - /// - /// true if this instance is end; otherwise, false. - /// - [JsonPropertyName("is_end")] - public bool IsEnd { get; set; } - /// - /// Gets or sets a value indicating whether this instance is truncated. - /// - /// - /// true if this instance is truncated; otherwise, false. - /// - [JsonPropertyName("is_truncated")] - public bool IsTruncated { get; set; } - /// - /// Gets or sets the result. - /// - /// - /// The result. - /// - [JsonPropertyName("result")] - public string Result { get; set; } - /// - /// Gets or sets a value indicating whether [need clear history]. - /// - /// - /// true if [need clear history]; otherwise, false. - /// - [JsonPropertyName("need_clear_history")] - public bool NeedClearHistory { get; set; } - /// - /// Gets or sets the ban round. - /// - /// - /// The ban round. - /// - [JsonPropertyName("ban_round")] - public int BanRound { get; set; } - /// - /// Gets or sets the usage. - /// - /// - /// The usage. - /// - [JsonPropertyName("usage")] - public ChatApiTokenUsage Usage { get; set; } - } - - /// - /// baidu API response wrapper - /// - /// - public class ChatApiResponse: IChatResponse - { - /// - /// Gets or sets the conversation identifier. - /// - /// - /// The conversation identifier. - /// - [JsonPropertyName("conversation_id")] - public string ConversationId { get; set; } = ""; - /// - /// 大模型 - /// 可选项:ERNIE-Bot-turbo(default)|ERNIE-Bot-4|ERNIE-Bot - /// - /// 2 - [JsonPropertyName("model")] - public LLM_ModelType ModelSchema { get; set; } = LLM_ModelType.ERNIE_BOT_TURBO; - /// - /// Gets or sets the result. - /// - /// - /// The result. - /// - [JsonPropertyName("result")] - public BaiduWenxinChatResponse Result { get; set; } - /// - /// Gets a value indicating whether [need clear history]. - /// - /// - /// true if [need clear history]; otherwise, false. - /// - [JsonPropertyName("need_clear_history")] - public bool NeedClearHistory => Result?.NeedClearHistory ?? false; - } - - - ///// - ///// baidu API token usage per request. - ///// - //public class BaiduWenxinUsage - //{ - // /// - // /// Gets or sets the prompt tokens. - // /// - // /// - // /// The prompt tokens. - // /// - // [JsonPropertyName("prompt_tokens")] - // public int PromptTokens { get; set; } - // /// - // /// Gets or sets the completion tokens. - // /// - // /// - // /// The completion tokens. - // /// - // [JsonPropertyName("completion_tokens")] - // public int CompletionTokens { get; set; } - // /// - // /// Gets or sets the total tokens. - // /// - // /// - // /// The total tokens. - // /// - // [JsonPropertyName("total_tokens")] - // public int TotalTokens { get; set; } - //} - - /// - /// - /// - public class ResponseStream - { - /// - /// Gets or sets the data. - /// - /// - /// The data. - /// - [JsonPropertyName("data")] - public T Data { get; set; } - - /// - /// Unfolds this instance. - /// - /// - public T Unfold() - { - return Data; - } - } -} diff --git a/src/LLMService.Shared/Models/ChatApiTokenUsage.cs b/src/LLMService.Shared/Models/ChatApiTokenUsage.cs index b42ee44..2db17e1 100644 --- a/src/LLMService.Shared/Models/ChatApiTokenUsage.cs +++ b/src/LLMService.Shared/Models/ChatApiTokenUsage.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; +using System.Text.Json.Serialization; namespace LLMService.Shared.Models { diff --git a/src/LLMService.Shared/Models/IChatResponse.cs b/src/LLMService.Shared/Models/IChatResponse.cs index 5e628ed..adda042 100644 --- a/src/LLMService.Shared/Models/IChatResponse.cs +++ b/src/LLMService.Shared/Models/IChatResponse.cs @@ -34,12 +34,19 @@ public interface IChatResponse /// bool NeedClearHistory { get; } /// - /// Gets or sets the result. + /// Gets or sets the LLM response data. /// /// - /// The result. + /// The LLM response data. /// - TChatApiResponse Result { get; set; } + TChatApiResponse LLMResponseData { get; set; } + /// + /// Gets the aigc message. + /// + /// + /// The aigc message. + /// + string AIGCMessage { get; } } } diff --git a/src/LLMServiceHub/Configuration/AppSwaggerGenOption.cs b/src/LLMServiceHub/Configuration/AppSwaggerGenOption.cs index 8c86234..3736ffb 100644 --- a/src/LLMServiceHub/Configuration/AppSwaggerGenOption.cs +++ b/src/LLMServiceHub/Configuration/AppSwaggerGenOption.cs @@ -64,9 +64,9 @@ private OpenApiInfo CreateVersionInfo( { var info = new OpenApiInfo() { - Title = "百度千帆大模型 API", + Title = "Baidu Erniebot & OpenAI ChatGPT LLM API for .NET", Version = $"v{apiDescription.ApiVersion.ToString()}", - Description = " API for LLMServiceHub ", + Description = " API for Erniebot|ErnieVilg|ChatGPT. ", Contact = new OpenApiContact() { Name = "arthuridea", Email = "arthuridea@gmail.com" } }; diff --git a/src/LLMServiceHub/Controller/v1_0/BaiduApiController.cs b/src/LLMServiceHub/Controller/v1_0/BaiduApiController.cs index dd7dc1c..2685554 100644 --- a/src/LLMServiceHub/Controller/v1_0/BaiduApiController.cs +++ b/src/LLMServiceHub/Controller/v1_0/BaiduApiController.cs @@ -1,17 +1,12 @@ using Asp.Versioning; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Formatters; -using Polly; -using Swashbuckle.AspNetCore.Annotations; -using LLMServiceHub.Common; +using LLMService.Baidu.ErnieVilg.Models; +using LLMService.Baidu.Wenxinworkshop; +using LLMService.Baidu.Wenxinworkshop.Models; using LLMService.Shared.Extensions; -using LLMService.Shared.ServiceInterfaces; using LLMService.Shared.Models; -using Microsoft.AspNetCore.Identity; -using LLMService.Baidu.Wenxinworkshop; +using LLMService.Shared.ServiceInterfaces; +using LLMServiceHub.Common; +using Microsoft.AspNetCore.Mvc; namespace LLMServiceHub.Controller.v1_0 { @@ -25,8 +20,8 @@ namespace LLMServiceHub.Controller.v1_0 /// The baidu API service. /// The ernie vilg API service. -// use oidc jwt authentication or local identity -//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] + // use oidc jwt authentication or local identity + //[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] #if !DEBUG [Authorize] @@ -56,7 +51,7 @@ public class BaiduApiController( /// [HttpPost("chat")] [ProducesResponseType(typeof(string), 200)] - [ProducesResponseType(typeof(ChatApiResponse), 200)] + [ProducesResponseType(typeof(BaiduChatApiResponse), 200)] [AppExceptionInterceptor(ReturnCode = -100001, ApiVersion = "1.0")] public async Task Chat(ChatRequest request) { diff --git a/src/LLMServiceHub/Controller/v1_0/ChatGPTApiController.cs b/src/LLMServiceHub/Controller/v1_0/ChatGPTApiController.cs index ab895a0..832c342 100644 --- a/src/LLMServiceHub/Controller/v1_0/ChatGPTApiController.cs +++ b/src/LLMServiceHub/Controller/v1_0/ChatGPTApiController.cs @@ -1,9 +1,8 @@ using Asp.Versioning; using LLMService.OpenAI.ChatGPT; +using LLMService.OpenAI.ChatGPT.Model; using LLMService.Shared.Models; using LLMServiceHub.Common; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace LLMServiceHub.Controller.v1_0 @@ -35,7 +34,7 @@ public class ChatGPTApiController( /// [HttpPost("chat")] [ProducesResponseType(typeof(string), 200)] - [ProducesResponseType(typeof(ChatApiResponse), 200)] + [ProducesResponseType(typeof(OpenAIChatResponse), 200)] [AppExceptionInterceptor(ReturnCode = -100001, ApiVersion = "1.0")] public async Task Chat(ChatRequest request) { diff --git a/src/LLMServiceHub/Pages/Index.cshtml b/src/LLMServiceHub/Pages/Index.cshtml index 1b3729b..9c080b8 100644 --- a/src/LLMServiceHub/Pages/Index.cshtml +++ b/src/LLMServiceHub/Pages/Index.cshtml @@ -58,8 +58,9 @@
- 文心3.5 +
@@ -129,8 +130,9 @@
- 文心turbo +
@@ -158,8 +160,9 @@
- 文心4.0 +
diff --git a/src/LLMServiceHub/Startup.cs b/src/LLMServiceHub/Startup.cs index a7db274..48a9123 100644 --- a/src/LLMServiceHub/Startup.cs +++ b/src/LLMServiceHub/Startup.cs @@ -3,20 +3,13 @@ using Asp.Versioning.Conventions; using IdentityModel; using LLMService.Baidu.ErnieVilg; -using LLMService.Baidu.Wenxinworkshop; using LLMService.OpenAI.ChatGPT.Model; -using LLMService.Shared; -using LLMService.Shared.Authentication.Handlers; -using LLMService.Shared.Authentication.Models; using LLMService.Shared.DataProvider; using LLMService.Shared.Models; using LLMService.Shared.ServiceInterfaces; using LLMServiceHub.Common; using LLMServiceHub.Configuration; -using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; @@ -77,7 +70,7 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient, ChatDataProvider>(); services.AddTransient>, ChatDataProvider>>(); - // inject baidu api service + // inject service services.AddWenxinworkshop(Configuration); services.AddChatGPT(Configuration); services.AddErnieVilg(Configuration); diff --git a/src/LLMServiceHub/wwwroot/js/index.js b/src/LLMServiceHub/wwwroot/js/index.js index ccf0306..f92ee3b 100644 --- a/src/LLMServiceHub/wwwroot/js/index.js +++ b/src/LLMServiceHub/wwwroot/js/index.js @@ -212,7 +212,7 @@ let sendBtnClickEventHandler = function (e) { console.log(data); if (data) { $(`#reply_time_${replyId}`).html(dateFormat('HH:MM:SS', new Date())); - var markdeownResult = data.result.result; + var markdeownResult = data.aigc_message; const typing = new EasyTyper({ output: '', isEnd: false, diff --git a/src/LLMServiceHub/wwwroot/js/index.min.js b/src/LLMServiceHub/wwwroot/js/index.min.js index 2696140..734c8e8 100644 --- a/src/LLMServiceHub/wwwroot/js/index.min.js +++ b/src/LLMServiceHub/wwwroot/js/index.min.js @@ -39,7 +39,7 @@ function dateFormat(n,t){let i;const r={"Y+":t.getFullYear().toString(),"m+":(t.
- `;$(h).appendTo($(u));$(c).appendTo($(u));let o=$(u).closest(".scrollable"),l=$(o).height();$(o).scrollTop(l);console.log("sending request with:");console.log(r);$.ajax(apiEndpoint,{method:"POST",dataType:"json",contentType:"application/json",data:JSON.stringify(r)}).done(function(t){if(console.log(t),t){$(`#reply_time_${n}`).html(dateFormat("HH:MM:SS",new Date));var i=t.result.result;const r=new EasyTyper({output:"",isEnd:!1,speed:20,singleBack:!1,sleep:0,type:"normal",backSpeed:40,sentencePause:!0},marked.parse(i),()=>{},t=>{$("#reply_"+n).html(`${t}`);let i=$(u).closest(".scrollable"),r=$(i).prop("scrollTop")+$("#reply_"+n).height();$(i).scrollTop(r)})}}).fail(function(t){console.log(t);$("#reply_"+n).html("发生错误")})}});n.preventDefault()};$(document).on("keyup",function(n){var t=n||window.event,i=t.which||t.keyCode||t.charCode;i==13&&sendBtnClickEventHandler(n)});$(".btn-send-message").on("click",sendBtnClickEventHandler);$(".btn-export").on("click",function(){let t=dateFormat("YYYYmmddHHMMSS",new Date),r=$(this).closest(".card"),u=$(this).data("key")+"-"+t+".txt",f=$(r).find(".chat-bubble"),n=[];n.push(`--------------${t}------------- + `;$(h).appendTo($(u));$(c).appendTo($(u));let o=$(u).closest(".scrollable"),l=$(o).height();$(o).scrollTop(l);console.log("sending request with:");console.log(r);$.ajax(apiEndpoint,{method:"POST",dataType:"json",contentType:"application/json",data:JSON.stringify(r)}).done(function(t){if(console.log(t),t){$(`#reply_time_${n}`).html(dateFormat("HH:MM:SS",new Date));var i=t.aigc_message;const r=new EasyTyper({output:"",isEnd:!1,speed:20,singleBack:!1,sleep:0,type:"normal",backSpeed:40,sentencePause:!0},marked.parse(i),()=>{},t=>{$("#reply_"+n).html(`${t}`);let i=$(u).closest(".scrollable"),r=$(i).prop("scrollTop")+$("#reply_"+n).height();$(i).scrollTop(r)})}}).fail(function(t){console.log(t);$("#reply_"+n).html("发生错误")})}});n.preventDefault()};$(document).on("keyup",function(n){var t=n||window.event,i=t.which||t.keyCode||t.charCode;i==13&&sendBtnClickEventHandler(n)});$(".btn-send-message").on("click",sendBtnClickEventHandler);$(".btn-export").on("click",function(){let t=dateFormat("YYYYmmddHHMMSS",new Date),r=$(this).closest(".card"),u=$(this).data("key")+"-"+t+".txt",f=$(r).find(".chat-bubble"),n=[];n.push(`--------------${t}------------- `);$(f).each(function(t,i){let r=$(i).find(".chat-bubble-author").text(),u=$(i).find(".chat-bubble-date").text(),f=$(i).find(".chat-bubble-body").text();n.push(`[${r}] ${u} `);n.push(f.trim());n.push("\r\n-------------------------------\r\n")});let i=n.join("");console.log(i);exportBlob(i,u)}); \ No newline at end of file