Skip to content

Commit

Permalink
fix: refactor code. change project structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthuridea committed Dec 22, 2023
1 parent fbd4da6 commit fc1b27d
Show file tree
Hide file tree
Showing 24 changed files with 241 additions and 275 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# 百度文心千帆大模型(文心一言、智能作画)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、百度文心大模型和百度智能绘图高级版功能.

## 已知问题

## 安装

通过`Nuget`安装.

### OpenAI ChatGPT



### 文心大模型

[Nuget地址](https://www.nuget.org/packages/Aeex.LLMService.Baidu.Wenxin/)
Expand Down Expand Up @@ -53,9 +57,13 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config
// inject image storage provider
services.AddTransient<IImageStorageProvider, LocalImageStorageProvider>();
// inject chat data provider
services.AddTransient<IChatDataProvider<BaiduWenxinMessage>, ChatDataProvider<BaiduWenxinMessage>>();
// for baidu chat caching
services.AddTransient<IChatDataProvider<ChatMessageBase, string>, ChatDataProvider<ChatMessageBase, string>>();
// for chatgpt
services.AddTransient<IChatDataProvider<OpenAIChatMessage, List<OpenAIMessageContent>>, ChatDataProvider<OpenAIChatMessage, List<OpenAIMessageContent>>>();

// inject baidu api service
// inject service
services.AddChatGPT(config);
services.AddWenxinworkshop(config);
services.AddErnieVilg(config);

Expand Down Expand Up @@ -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`可以完美调用,实现了基本的接口 |
3 changes: 2 additions & 1 deletion src/LLMService.Baidu.ErnieVilg/BaiduErnieVilgApiService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace LLMService.Shared.Models
namespace LLMService.Baidu.ErnieVilg.Models
{
#nullable enable
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace LLMService.Shared.Models
namespace LLMService.Baidu.ErnieVilg.Models
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel;
using System.Text.Json.Serialization;

namespace LLMService.Shared.Models
namespace LLMService.Baidu.ErnieVilg.Models
{
/// <summary>
///
Expand Down
14 changes: 5 additions & 9 deletions src/LLMService.Baidu.Wenxinworkshop/BaiduWenxinApiService.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -33,10 +29,10 @@ public interface IBaiduErniebotLLMService
/// <seealso cref="ChatServiceBase{TRequestDto, TResponseDto, TBackendRequestDto, TBackendResponseDto, TChatMessage, TMessageContent, TChatServiceOption}" />
/// <seealso cref="IBaiduErniebotLLMService" />
public class BaiduErniebotLLMService :
ChatServiceBase<ChatRequest, ChatApiResponse,
ChatServiceBase<ChatRequest, BaiduChatApiResponse,
BaiduApiChatRequest, BaiduWenxinChatResponse,
ChatMessageBase, string,
OAuth2BackendServiceConfig>, IBaiduErniebotLLMService, IAIChatApiService<ChatRequest, ChatApiResponse>
OAuth2BackendServiceConfig>, IBaiduErniebotLLMService, IAIChatApiService<ChatRequest, BaiduChatApiResponse>
{
/// <summary>
/// Initializes a new instance of the <see cref="BaiduErniebotLLMService"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@
<ProjectReference Include="..\LLMService.Shared\LLMService.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Model\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 内部调用百度api的接口
Expand Down
54 changes: 54 additions & 0 deletions src/LLMService.Baidu.Wenxinworkshop/Models/BaiduChatApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using LLMService.Shared.Models;
using System.Text.Json.Serialization;
using static LLMService.Shared.Models.LLMApiDefaults;

namespace LLMService.Baidu.Wenxinworkshop.Models
{
/// <summary>
/// baidu API response wrapper
/// </summary>
/// <seealso cref="BaiduWenxinChatResponse" />
public class BaiduChatApiResponse : IChatResponse<BaiduWenxinChatResponse>
{
/// <summary>
/// Gets or sets the conversation identifier.
/// </summary>
/// <value>
/// The conversation identifier.
/// </value>
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; } = "";
/// <summary>
/// 大模型
/// <para>可选项:ERNIE-Bot-turbo(default)|ERNIE-Bot-4|ERNIE-Bot</para>
/// </summary>
/// <example>2</example>
[JsonPropertyName("model")]
public LLM_ModelType ModelSchema { get; set; } = LLM_ModelType.ERNIE_BOT_TURBO;
/// <summary>
/// Gets or sets the result.
/// </summary>
/// <value>
/// The result.
/// </value>
[JsonPropertyName("llm_response_data")]
public BaiduWenxinChatResponse LLMResponseData { get; set; }
/// <summary>
/// Gets a value indicating whether [need clear history].
/// </summary>
/// <value>
/// <c>true</c> if [need clear history]; otherwise, <c>false</c>.
/// </value>
[JsonPropertyName("need_clear_history")]
public bool NeedClearHistory => LLMResponseData?.NeedClearHistory ?? false;

/// <summary>
/// latest aigc message.
/// </summary>
/// <value>
/// The aigc message.
/// </value>
[JsonPropertyName("aigc_message")]
public string AIGCMessage => LLMResponseData?.Result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using LLMService.Shared.Models;
using System.Text.Json.Serialization;

namespace LLMService.Baidu.Wenxinworkshop.Models
{
/// <summary>
/// baidu API response model
/// </summary>
public class BaiduWenxinChatResponse /*: IServerSentEventData*/
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
[JsonPropertyName("id")]
public string Id { get; set; }
/// <summary>
/// Gets or sets the type of the object.
/// </summary>
/// <value>
/// The type of the object.
/// </value>
[JsonPropertyName("object")]
public string ObjectType { get; set; }
/// <summary>
/// Gets or sets the created.
/// </summary>
/// <value>
/// The created.
/// </value>
[JsonPropertyName("created")]
public long Created { get; set; }
/// <summary>
/// Gets or sets the sentence identifier.
/// </summary>
/// <value>
/// The sentence identifier.
/// </value>
[JsonPropertyName("sentence_id")]
public int SentenceId { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is end.
/// </summary>
/// <value>
/// <c>true</c> if this instance is end; otherwise, <c>false</c>.
/// </value>
[JsonPropertyName("is_end")]
public bool IsEnd { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is truncated.
/// </summary>
/// <value>
/// <c>true</c> if this instance is truncated; otherwise, <c>false</c>.
/// </value>
[JsonPropertyName("is_truncated")]
public bool IsTruncated { get; set; }
/// <summary>
/// Gets or sets the result.
/// </summary>
/// <value>
/// The result.
/// </value>
[JsonPropertyName("result")]
public string Result { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [need clear history].
/// </summary>
/// <value>
/// <c>true</c> if [need clear history]; otherwise, <c>false</c>.
/// </value>
[JsonPropertyName("need_clear_history")]
public bool NeedClearHistory { get; set; }
/// <summary>
/// Gets or sets the ban round.
/// </summary>
/// <value>
/// The ban round.
/// </value>
[JsonPropertyName("ban_round")]
public int BanRound { get; set; }
/// <summary>
/// Gets or sets the usage.
/// </summary>
/// <value>
/// The usage.
/// </value>
[JsonPropertyName("usage")]
public ChatApiTokenUsage Usage { get; set; }
}

}
8 changes: 0 additions & 8 deletions src/LLMService.OpenAI.ChatGPT/ChatGPTApiService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand Down
29 changes: 26 additions & 3 deletions src/LLMService.OpenAI.ChatGPT/Model/OpenAIChatResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace LLMService.OpenAI.ChatGPT.Model
///
/// </summary>
/// <seealso cref="IChatResponse{TChatApiResponse}" />
public class OpenAIChatResponse: IChatResponse<OpenAIBackendResponseModel>
public class OpenAIChatResponse : IChatResponse<OpenAIBackendResponseModel>
{
/// <summary>
/// Gets or sets the result.
/// </summary>
/// <value>
/// The result.
/// </value>
[JsonPropertyName("result")]
public OpenAIBackendResponseModel Result { get; set; }
[JsonPropertyName("llm_response_data")]
public OpenAIBackendResponseModel LLMResponseData { get; set; }
/// <summary>
/// Gets or sets the conversation identifier.
/// </summary>
Expand All @@ -42,6 +42,29 @@ public class OpenAIChatResponse: IChatResponse<OpenAIBackendResponseModel>
[JsonPropertyName("need_clear_history")]
public bool NeedClearHistory { get; set; }

/// <summary>
/// latest aigc message.
/// </summary>
/// <value>
/// The aigc message.
/// </value>
[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;
}
}

}


}
}
Loading

0 comments on commit fc1b27d

Please sign in to comment.