Compare commits

...

52 Commits
0.1.5 ... 0.1.7

Author SHA1 Message Date
zyxucp
3b5997fce6 Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-09 23:43:30 +08:00
zyxucp
7b0f6c3e75 add 增加消息结束后滚动条滚到底部 2024-03-09 23:40:00 +08:00
zyxucp
099b85619c add 增加消息时间的气泡 2024-03-09 23:12:28 +08:00
zyxucp
0129cd3f39 fix 修改头像样式为固定大小 2024-03-09 23:04:09 +08:00
zyxucp
84f3cbf9a9 add 增加消息复制按钮 2024-03-09 22:55:45 +08:00
zyxucp
a7af462a44 add 增加知识库外部使用流式接口 2024-03-09 22:20:05 +08:00
zyxucp
fdca08eb3d fix 抽象KMS结构 2024-03-09 22:09:31 +08:00
zyxucp
c3eeefe9fe fix 抽象sendkms 2024-03-09 21:40:36 +08:00
zyxucp
2f6990320c fix 封装Chat方法 2024-03-09 21:21:32 +08:00
zyxucp
ef83450425 fix 调整名称 2024-03-09 21:12:22 +08:00
zyxucp
f2f10ec9f4 fix 调整项目结构,抽象发送消息接口 2024-03-09 21:10:50 +08:00
zyxucp
7346ff2e78 add 更新图片 2024-03-09 19:35:38 +08:00
zyxucp
1dc274ce82 fix 执行code clear 2024-03-09 19:19:05 +08:00
zyxucp
2be438f9c3 fix 调整项目结构 2024-03-09 19:12:40 +08:00
zyxucp
1f7f51ff1e fix 优化代码 2024-03-09 18:24:17 +08:00
zyxucp
338a7ae083 Update docker-compose.simple.yml 2024-03-09 17:47:30 +08:00
zyxucp
04d7896a92 Update docker-compose.yml 2024-03-09 17:47:17 +08:00
zyxucp
4b8c8c0f96 fix 修改openai azue 和llama 流式不一致的问题 2024-03-09 17:30:11 +08:00
zyxucp
8296476d94 Update docker-compose.simple.yml 2024-03-09 12:40:04 +08:00
zyxucp
38b3fc26ed Update docker-compose.yml 2024-03-09 12:39:48 +08:00
zyxucp
6852b458fa fix 字典修改 2024-03-09 12:15:32 +08:00
zyxucp
cdb41023d4 Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-09 12:10:05 +08:00
zyxucp
8e00e681f0 fix 修改StreamingTextContent 2024-03-09 12:09:53 +08:00
zyxucp
577d6dd3a6 add 增加异常日志 2024-03-09 12:02:47 +08:00
zyxucp
eeecd15d9e fix 修改StreamingTextContent 2024-03-09 11:59:36 +08:00
zyxucp
e39b26bcc3 更新 README.md 2024-03-09 00:05:20 +08:00
zyxucp
6b5a77f8c1 Update appsettings.json 2024-03-08 23:38:36 +08:00
zyxucp
0dc9736c35 Update docker-compose.simple.yml 2024-03-08 23:18:15 +08:00
zyxucp
bbe2471815 Update docker-compose.yml 2024-03-08 23:17:49 +08:00
zyxucp
bee2c56382 Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-08 22:29:55 +08:00
zyxucp
b05a4f51b7 add 增加清理聊天记录 2024-03-08 22:22:18 +08:00
zyxucp
608794b600 add 增加描述 2024-03-08 22:08:11 +08:00
zyxucp
ce8829ae69 add 新增知识库修改 2024-03-08 22:04:52 +08:00
zyxucp
1fb27f8d5a add 增加应用配置校验 2024-03-08 21:54:53 +08:00
zyxucp
caf8777290 add 增加模型管理配置 2024-03-08 21:47:51 +08:00
zyxucp
7dacdab2b5 fix 修改LLamaSharp本地调用 2024-03-08 21:12:42 +08:00
zyxucp
f6a8660144 fix 修改KMS删除 2024-03-08 20:25:25 +08:00
zyxucp
1e51631eba add 增加LLamaSharp本地执行 2024-03-08 19:53:13 +08:00
zyxucp
91048dc9c1 add 增加应用和知识库使用配置模型
fix 删除openai配置文件
2024-03-08 18:46:26 +08:00
zyxucp
9f8dc39e2f add 增加会话的模型和知识库模型配置 2024-03-08 17:06:35 +08:00
zyxucp
07aa8f4829 Update README.md 2024-03-08 16:55:13 +08:00
zyxucp
b504615b6d add 增加模型配置 2024-03-08 15:20:57 +08:00
zyxucp
3b1811a5ff add 增加模型配置 2024-03-08 14:50:46 +08:00
zyxucp
71be6d4a5a Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-08 10:04:01 +08:00
zyxucp
ae04d20a82 add 增加session 登陆记录 2024-03-08 00:48:41 +08:00
zyxucp
2ad3953d3f add session 2024-03-08 00:28:27 +08:00
zyxucp
c536f1d74b Update README.md 2024-03-07 21:46:06 +08:00
zyxucp
d8c1695ac9 Update README.en.md 2024-03-07 21:26:17 +08:00
zyxucp
b39912d08b Merge branch 'main' of https://github.com/xuzeyu91/AntSK 2024-03-07 18:27:26 +08:00
zyxucp
8f5dd08836 Update README.md 2024-03-07 17:52:59 +08:00
zyxucp
6a27e61321 fix 删除appsetting注释 2024-03-07 17:32:59 +08:00
zyxucp
34fa19cf1e fix 修改docker file 2024-03-07 16:46:14 +08:00
141 changed files with 1730 additions and 1586 deletions

View File

@@ -7,7 +7,7 @@ COPY ["src/AntSK/AntSK.csproj", "AntSK/"]
RUN dotnet restore "AntSK/AntSK.csproj"
# Copy everything else and build
COPY . .
COPY src/ .
WORKDIR "/src/AntSK"
RUN dotnet build "AntSK.csproj" -c Release -o /app/build
RUN dotnet publish "AntSK.csproj" -c Release -o /app/publish

View File

@@ -123,58 +123,76 @@ The model supports openai by default. If you need to use azure openai and need t
The following configuration files need to be configured
## Using Docker Compose
Provided pg version appsettings. json and simplified version (Sqlite+disk) Docker Compose. simple. yml
Download Docker Compose.yml from the project root directory, and then place the configuration file appsettings.json and it in a unified directory,
The image of PG has been prepared here. You can modify the default account password in Docker Compose.yml, and your appsettings. json database connection needs to be consistent.
Then you can enter the directory and execute it
```
docker compose up - d
```
To start AntSK
Some meanings of configuration files
```
"ConnectionStrings":{
"Postgres": "Host=; Port=; Database=antsk; Username=; Password="
},
"OpenAIOption":{
"EndPoint": "",
"Key": "",
"Model": "",
"Embedding Model": """""
},
Postgres:{
"ConnectionString": "Host=; Port=; Database=antsk; Username=; Password=",
"TableNamePrefix": "km -"
},
"Login": {
"User": "admin",
"Password": "xuzeyu"
{
"DBConnection": {
"DbType": "Sqlite",
"ConnectionStrings": "Data Source=AntSK.db;"
},
"OpenAIOption": {
"EndPoint": "http://localhost:5000/llama/",
"Key": "NotNull",
"Model": "gpt4-turbo",
"EmbeddingModel": "text-embedding-ada-002"
},
"KernelMemory": {
"VectorDb": "Disk",
"ConnectionString": "Host=;Port=;Database=antsk;Username=;Password=",
"TableNamePrefix": "km-"
},
"LLamaSharp": {
"RunType": "GPU",
"Chat": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf",
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf"
},
"Login": {
"User": "admin",
"Password": "xuzeyu"
},
"BackgroundTaskBroker": {
"ImportKMSTask": {
"WorkerCount": 1
}
}
}
```
I use CodeFirst mode. As long as the database link is configured, the table structure is automatically created
```
//Supports multiple databases, including SqlSugar, MySql, SqlServer, Sqlite, Oracle, PostgreSQL, Dm, Kdbndp, Oscar, MySqlConnector, Access, OpenGaussian, QuestDB, HG, ClickHouse, GBase, Odbc, OceanBaseForOracle, TDengine, GaussDB, OceanBase, Tidb, Vastbase, PolarDB, Custom
DBConnection DbType
//Connection string, corresponding strings need to be used according to different DB types
DBConnection ConnectionStrings
//You can use an online API that conforms to the OpenAI format (domestic models use one API adapter), or you can use AntSK's built-in llama API, with the IP and port being the AntSK startup address
OpenAIOption EndPoint
//Model key, if using a local model, it can default to Notnull. Chinese cannot be used here
OpenAIOption Key
//The type of vector storage supports Postgres Disk Memory, where Postgres requires the configuration of ConnectionString
KernelMemory VectorDb
//The running mode used by the local model is GUP CPU. If using an online API, you can freely use one
LLamaSharp RunType
//The model path of the local session model should pay attention to distinguishing between Linux and Windows drive letters
LLamaSharp Chat
//The model path of the local vector model should pay attention to distinguishing between Linux and Windows drive letters
LLamaSharp Embedding
//Default administrator account password
Login
//The number of threads for importing asynchronous processing can be higher when using online APIs. Local models suggest 1, otherwise memory overflow and crash may occur
BackgroundTaskBroker ImportKMSTask WorkerCount
If you want to use LLamaSharp to run the local model, you also need to set the following configuration:
```
"LLamaSharp": {
"Chat": "D:\\Code\\AI\\AntBlazor\\model\\tinyllama-1.1b-chat.gguf",
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\tinyllama-1.1b-chat.gguf"
},
```
You need to configure the addresses of the Chat and Embedding models, and then modify EndPoint to local. When using the local model, parameters such as Key, Model, and Embedding Model are not used, so you can freely fill in these parameters:
```
"OpenAIOption": {
"EndPoint": "https://ip:port/llama/",
"Key": "",
"Model": "",
"EmbeddingModel": ""
},
```

107
README.md
View File

@@ -71,9 +71,11 @@ Login是默认的登陆账号和密码
## 使用docker-compose
从项目根目录下载docker-compose.yml,然后把配置文件appsettings.json和它放在统一目录
提供了pg版本 **appsettings.json** 和 简化版本Sqlite+disk **docker-compose.simple.yml**
这里已经把pg的镜像做好了。在docker-compose.yml中可以修改默认账号密码,然后你的appsettings.json的数据库连接需要保持一致。
从项目根目录下载**docker-compose.yml**,然后把配置文件**appsettings.json**和它放在统一目录,
这里已经把pg的镜像做好了。在docker-compose.yml中可以修改默认账号密码然后你的**appsettings.json**的数据库连接需要保持一致。
然后你可以进入到目录后执行
```
@@ -81,46 +83,100 @@ docker-compose up -d
```
来启动AntSK
## 如何在docker中挂载本地模型
```
# 非 host 版本, 不使用本机代理
version: '3.8'
services:
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.5
ports:
- 5000:5000
networks:
- antsk
depends_on:
- antskpg
restart: always
environment:
- ASPNETCORE_URLS=http://*:5000
volumes:
- ./appsettings.json:/app/appsettings.json # 本地配置文件 需要放在同级目录
- D://model:/app/model
networks:
antsk:
```
以这个为示例意思是把windows本地D://model的文件夹挂载进 容器内/app/model 如果是这样你的appsettings.json中的模型地址应该配置为
```
model/xxx.gguf
```
## 配置文件的一些含义
```
"ConnectionStrings": {
"Postgres": "Host=;Port=;Database=antsk;Username=;Password="//这个是业务数据的连接字符串
{
"DBConnection": {
"DbType": "Sqlite",
"ConnectionStrings": "Data Source=AntSK.db;"
},
"OpenAIOption": {
"EndPoint": "", //openai协议的接口写到v1之前
"Key": "",//接口秘钥,如果使用本地模型可以随意填写一个但不能为空
"Model": "",//会话模型,使用接口时需要,使用本地模型可以随意填写
"EmbeddingModel": ""//向量模型,使用接口时需要,使用本地模型可以随意填写
},
"Postgres": {
"KernelMemory": {
"VectorDb": "Disk",
"ConnectionString": "Host=;Port=;Database=antsk;Username=;Password=",
"TableNamePrefix": "km-"
},
"LLamaSharp": {
"RunType": "GPU",
"Chat": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf",
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\qwen1_5-1_8b-chat-q8_0.gguf"
},
"Login": {
"User": "admin",
"Password": "xuzeyu"
},
"BackgroundTaskBroker": {
"ImportKMSTask": {
"WorkerCount": 1
}
}
}
```
我使用的是CodeFirst模式只要配置好数据库链接表结构是自动创建的
如果想使用LLamaSharp运行本地模型还需要设置如下配置
```
"LLamaSharp": {
"Chat": "D:\\Code\\AI\\AntBlazor\\model\\tinyllama-1.1b-chat.gguf",//本地会话模型的磁盘路径
"Embedding": "D:\\Code\\AI\\AntBlazor\\model\\tinyllama-1.1b-chat.gguf"//本地向量模型的磁盘路径
},
//支持多种数据库具体可以查看SqlSugarMySqlSqlServerSqliteOraclePostgreSQLDmKdbndpOscarMySqlConnectorAccessOpenGaussQuestDBHGClickHouseGBaseOdbcOceanBaseForOracleTDengineGaussDBOceanBaseTidbVastbasePolarDBCustom
DBConnection.DbType
//连接字符串需要根据不同DB类型用对应的字符串
DBConnection.ConnectionStrings
//可以使用符合openai格式的在线API国产模型使用one-api转接 也可以使用AntSK自带的llama apiip和端口是AntSK启动地址
OpenAIOption.EndPoint
//模型秘钥如果使用本地模型可以默认NotNull 这里不能用中文
OpenAIOption.Key
//向量存储的类型,支持 Postgres Disk Memory 其中Postgres需要配置 ConnectionString
KernelMemory.VectorDb
//本地模型使用的运行方式 GUP CPU ,如果用在线API 这个随意使用一个即可
LLamaSharp.RunType
//本地会话模型的模型路径 注意区分linux和windows盘符不同
LLamaSharp.Chat
//本地向量模型的模型路径 注意区分linux和windows盘符不同
LLamaSharp.Embedding
//默认管理员账号密码
Login
//导入异步处理的线程数使用在线API可以高一点本地模型建议1 否则容易内存溢出崩掉
BackgroundTaskBroker.ImportKMSTask.WorkerCount
```
需要配置Chat和Embedding模型的地址然后修改EndPoint为本地使用本地模型时并没有用到Key、Model、EmbeddingModel这些参数所以这几个你可以随意填写
## 找不到样式问题解决
AntSK/src/AntSK下执行:
```
"OpenAIOption": {
"EndPoint": "http://ip:port/llama/",//如果使用本地模型这个ip端口是AntSK服务启动的ip和端口
"Key": "",//接口秘钥,如果使用本地模型可以随意填写一个但不能为空
"Model": "",//会话模型,使用接口时需要,使用本地模型可以随意填写
"EmbeddingModel": ""//向量模型,使用接口时需要,使用本地模型可以随意填写
},
dotnet clean
dotnet build
dotnet publish "AntSK.csproj"
```
再去AntSK/src/AntSK/bin/Release/net8.0/publish下
```
dotnet AntSK.dll
```
然后启动就有样式了
DB我使用的是CodeFirst模式只要配置好数据库链接表结构是自动创建的
想了解更多信息或开始使用 **AntSK**,可以关注我的公众号以及加入交流群。
@@ -132,3 +188,4 @@ docker-compose up -d
---
我们对您在**AntSK**的兴趣表示感谢,并期待与您携手共创智能化的未来!

View File

@@ -3,7 +3,7 @@ version: '3.8'
services:
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.5
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.6.2
ports:
- 5000:5000
networks:

View File

@@ -18,7 +18,7 @@ services:
- ./pg/data:/var/lib/postgresql/data
antsk:
container_name: antsk
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.5
image: registry.cn-hangzhou.aliyuncs.com/xuzeyu91/antsk:v0.1.6.2
ports:
- 5000:5000
networks:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 97 KiB

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 174 KiB

After

Width:  |  Height:  |  Size: 170 KiB

View File

@@ -8,6 +8,9 @@
<NoWarn>CA1050,CA1707,CA2007,VSTHRD111,CS1591,RCS1110,CA5394,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0010,SKEXP0011,,SKEXP0012,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0040,SKEXP0041,SKEXP0042,SKEXP0050,SKEXP0051,SKEXP0052,SKEXP0053,SKEXP0054,SKEXP0055,SKEXP0060,SKEXP0061,SKEXP0101,SKEXP0102</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AntDesign.Charts" Version="0.5.1" />
<PackageReference Include="AntDesign.ProLayout" Version="0.17.3" />
<PackageReference Include="AutoMapper" Version="8.1.0" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="MarkdownSharp" Version="2.0.5" />

View File

@@ -48,7 +48,21 @@
<param name="value"></param>
<returns></returns>
</member>
<member name="M:AntSK.Domain.Domain.Service.KernelService.GetKernel(System.String,System.String)">
<member name="F:AntSK.Domain.Domain.Other.LLamaConfig.dicLLamaWeights">
<summary>
避免模型重复加载,本地缓存
</summary>
</member>
<member name="M:AntSK.Domain.Domain.Service.ChatService.SendChatByAppAsync(AntSK.Domain.Repositories.Apps,System.String,System.String)">
<summary>
发送消息
</summary>
<param name="app"></param>
<param name="questions"></param>
<param name="history"></param>
<returns></returns>
</member>
<member name="M:AntSK.Domain.Domain.Service.KernelService.GetKernelByApp(AntSK.Domain.Repositories.Apps)">
<summary>
获取kernel实例依赖注入不好按每个用户去Import不同的插件所以每次new一个新的kernel
</summary>
@@ -103,6 +117,16 @@
<param name="result"></param>
<returns></returns>
</member>
<member name="T:AntSK.Domain.Model.Enum.AIType">
<summary>
AI类型
</summary>
</member>
<member name="T:AntSK.Domain.Model.Enum.AIModelType">
<summary>
模型类型
</summary>
</member>
<member name="P:AntSK.Domain.Model.MessageInfo.IsSend">
<summary>
发送是true 接收是false
@@ -208,6 +232,11 @@
类型
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Apps.ChatModelID">
<summary>
会话模型ID
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Apps.Temperature">
<summary>
温度
@@ -278,6 +307,16 @@
会话模型
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Kmss.ChatModelID">
<summary>
会话模型ID
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Kmss.EmbeddingModelID">
<summary>
向量模型ID
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Kmss.MaxTokensPerParagraph">
<summary>
每个段落的最大标记数。
@@ -550,6 +589,11 @@
sqlserver连接
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.AIModels.AIType">
<summary>
AI类型
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.AIModels.AIModelType">
<summary>
模型类型
@@ -570,6 +614,11 @@
模型秘钥
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.AIModels.ModelDescription">
<summary>
部署名azure需要使用
</summary>
</member>
<member name="P:AntSK.Domain.Repositories.Users.No">
<summary>
工号,用于登陆

View File

@@ -1,10 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Common.DependencyInjection
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Common.DependencyInjection
namespace AntSK.Domain.Common.DependencyInjection
{
public class ServiceDescriptionAttribute : Attribute
{

View File

@@ -1,7 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
{

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
namespace AntSK.Domain.Common.Map
{
public static class MapperExtend
{

View File

@@ -1,8 +1,5 @@
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
namespace AntSK.Domain.Common.Map
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Dto
namespace AntSK.Domain.Domain.Dto
{
public class KMFile
{

View File

@@ -1,11 +1,4 @@
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Models
namespace AntSK.Domain.Domain.Dto
{
public class OpenAIModel
{

View File

@@ -1,7 +1,6 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace AntSK.Models.OpenAPI
namespace AntSK.Domain.Domain.Dto
{
public class OpenAIResult
{
@@ -21,7 +20,7 @@ namespace AntSK.Models.OpenAPI
public OpenAIMessage message { get; set; }
}
public class OpenAIEmbeddingResult
public class OpenAIEmbeddingResult
{
[JsonProperty("object")]
public string obj { get; set; } = "list";
@@ -29,17 +28,17 @@ namespace AntSK.Models.OpenAPI
public UsageModel usage { get; set; } = new UsageModel();
public List<DataModel> data { get; set; } = new List<DataModel>() { new DataModel() };
public List<DataModel> data { get; set; } = new List<DataModel>() { new DataModel() };
}
public class UsageModel
public class UsageModel
{
public long prompt_tokens { get; set; } = 0;
public long total_tokens { get; set; } = 0;
}
public class DataModel
public class DataModel
{
[JsonProperty("object")]
public string obj { get; set; } = "embedding";

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Dto
{
public class RelevantSource
{
public string SourceName { get; set; }
public string Text { get; set; }
public float Relevance { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{
public interface IChatService
{
IAsyncEnumerable<StreamingKernelContent> SendChatByAppAsync(Apps app, string questions, string history);
IAsyncEnumerable<StreamingKernelContent> SendKmsByAppAsync(Apps app, string questions, string history, List<RelevantSource> relevantSources = null);
}
}

View File

@@ -1,9 +1,4 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{

View File

@@ -1,17 +1,11 @@
using AntSK.Domain.Domain.Dto;
using Microsoft.KernelMemory.Configuration;
using Microsoft.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{
public interface IKMService
{
MemoryServerless GetMemory(SearchClientConfig searchClientConfig = null, TextPartitioningOptions textPartitioningOptions = null);
Task<List<KMFile>> GetDocumentByFileID(string fileid);
MemoryServerless GetMemoryByKMS(string kmsID, SearchClientConfig searchClientConfig = null);
Task<List<KMFile>> GetDocumentByFileID(string kmsid, string fileid);
}
}

View File

@@ -1,16 +1,11 @@
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Interface
{
public interface IKernelService
{
Kernel GetKernel(string modelId = null, string apiKey = null);
Kernel GetKernelByApp(Apps app);
void ImportFunctionsByApp(Apps app, Kernel _kernel);
Task<string> HistorySummarize(Kernel _kernel, string questions, string history);
}

View File

@@ -2,13 +2,8 @@
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
namespace AntSK.Domain.Domain.Other
{
public class BackGroundTaskHandler : IBackgroundTaskHandler<ImportKMSTaskReq>
{
@@ -23,7 +18,7 @@ namespace AntSK.Domain.Domain.Service
using (var scope = _scopeFactory.CreateScope())
{
Console.WriteLine("ExecuteAsync.开始执行后台任务");
var importKMSService = scope.ServiceProvider.GetRequiredService<IImportKMSService>();
var importKMSService = scope.ServiceProvider.GetRequiredService<IImportKMSService>();
//不能使用异步
importKMSService.ImportKMSTask(item);
Console.WriteLine("ExecuteAsync.后台任务执行完成");

View File

@@ -0,0 +1,44 @@
using LLama;
using LLama.Common;
using LLamaSharp.KernelMemory;
namespace AntSK.Domain.Domain.Other
{
public static class LLamaConfig
{
static object lockobj = new object();
/// <summary>
/// 避免模型重复加载,本地缓存
/// </summary>
static Dictionary<string, (LLamaWeights, ModelParams)> dicLLamaWeights = new Dictionary<string, (LLamaWeights, ModelParams)>();
public static (LLamaWeights, ModelParams) GetLLamaConfig(string modelPath, LLamaSharpConfig config = null)
{
lock (lockobj)
{
if (dicLLamaWeights.ContainsKey(modelPath))
{
return dicLLamaWeights.GetValueOrDefault(modelPath);
}
else
{
InferenceParams infParams = new() { AntiPrompts = ["\n\n"] };
LLamaSharpConfig lsConfig = new(modelPath) { DefaultInferenceParams = infParams };
if (config != null)
{
lsConfig = config;
}
var parameters = new ModelParams(lsConfig.ModelPath)
{
ContextSize = lsConfig?.ContextSize ?? 2048,
Seed = lsConfig?.Seed ?? 0,
GpuLayerCount = lsConfig?.GpuLayerCount ?? 20,
EmbeddingMode = true
};
var weights = LLamaWeights.LoadFromFile(parameters);
dicLLamaWeights.Add(modelPath, (weights, parameters));
return (weights, parameters);
}
}
}
}
}

View File

@@ -0,0 +1,105 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Repositories;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Utils;
using Microsoft.KernelMemory;
using AntSK.Domain.Model;
using MarkdownSharp;
using AntSK.Domain.Domain.Dto;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IChatService), ServiceLifetime.Scoped)]
public class ChatService(
IKernelService _kernelService,
IKMService _kMService ,
IKmsDetails_Repositories _kmsDetails_Repositories
) : IChatService
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="app"></param>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <returns></returns>
public async IAsyncEnumerable<StreamingKernelContent> SendChatByAppAsync(Apps app, string questions, string history)
{
if (string.IsNullOrEmpty(app.Prompt) || !app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString() + "{{$input}}";
}
var _kernel = _kernelService.GetKernelByApp(app);
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings.ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions;
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync(function: func, arguments: new KernelArguments() { ["input"] = $"{history}{Environment.NewLine} user:{questions}" });
await foreach (var content in chatResult)
{
yield return content;
}
}
public async IAsyncEnumerable<StreamingKernelContent> SendKmsByAppAsync(Apps app, string questions, string history, List<RelevantSource> relevantSources=null)
{
var _kernel = _kernelService.GetKernelByApp(app);
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
//只取第一个知识库的配置
var _memory = _kMService.GetMemoryByKMS(kmsidList.FirstOrDefault());
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance * 100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
if (relevantSources.IsNotNull())
{
var markdown = new Markdown();
string sourceName = item.SourceName;
var fileDetail = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName);
if (fileDetail.IsNotNull())
{
sourceName = fileDetail.FileName;
}
relevantSources.Add(new RelevantSource() { SourceName = sourceName, Text = markdown.Transform(part.Text), Relevance = part.Relevance });
}
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = history, ["questions"] = questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
yield return content;
}
}
}
}
}

View File

@@ -1,30 +1,21 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Utils;
using Microsoft.KernelMemory;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IHttpService), ServiceLifetime.Scoped)]
public class HttpService: IHttpService
public class HttpService : IHttpService
{
public async Task< RestResponse> PostAsync(string url ,Object jsonBody)
public async Task<RestResponse> PostAsync(string url, Object jsonBody)
{
RestClient client = new RestClient();
RestRequest request= new RestRequest(url, Method.Post);
RestRequest request = new RestRequest(url, Method.Post);
string josn = JsonConvert.SerializeObject(jsonBody);
request.AddJsonBody(jsonBody);
var result =await client.ExecuteAsync(request);
return result;
var result = await client.ExecuteAsync(request);
return result;
}
}
}

View File

@@ -3,13 +3,6 @@ using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using Microsoft.SemanticKernel.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Domain.Service
{
@@ -26,24 +19,20 @@ namespace AntSK.Domain.Domain.Service
try
{
var km = _kmss_Repositories.GetFirst(p => p.Id == req.KmsId);
var _memory = _kMService.GetMemory(textPartitioningOptions: new TextPartitioningOptions()
{
MaxTokensPerLine = km.MaxTokensPerLine,
MaxTokensPerParagraph = km.MaxTokensPerParagraph,
OverlappingTokens = km.OverlappingTokens
});
var _memory = _kMService.GetMemoryByKMS(km.Id);
string fileid = req.KmsDetail.Id;
switch (req.ImportType)
{
case ImportType.File:
//导入文件
{
var importResult= _memory.ImportDocumentAsync(new Document(fileid)
.AddFile(req.FilePath)
.AddTag("kmsid", req.KmsId)
, index: "kms").Result;
var importResult = _memory.ImportDocumentAsync(new Document(fileid)
.AddFile(req.FilePath)
.AddTag("kmsid", req.KmsId)
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
string fileGuidName = Path.GetFileName(req.FilePath);
req.KmsDetail.FileName = req.FileName;
req.KmsDetail.FileGuidName = fileGuidName;
@@ -57,7 +46,7 @@ namespace AntSK.Domain.Domain.Service
var importResult = _memory.ImportWebPageAsync(req.Url, fileid, new TagCollection() { { "kmsid", req.KmsId } }
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
req.KmsDetail.Url = req.Url;
req.KmsDetail.DataCount = docTextList.Count;
}
@@ -68,7 +57,7 @@ namespace AntSK.Domain.Domain.Service
var importResult = _memory.ImportTextAsync(req.Text, fileid, new TagCollection() { { "kmsid", req.KmsId } }
, index: "kms").Result;
//查询文档数量
var docTextList = _kMService.GetDocumentByFileID(fileid).Result;
var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result;
req.KmsDetail.Url = req.Url;
req.KmsDetail.DataCount = docTextList.Count;
@@ -76,14 +65,14 @@ namespace AntSK.Domain.Domain.Service
break;
}
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Success;
_kmsDetails_Repositories.Update(req.KmsDetail);
//_kmsDetails_Repositories.GetList(p => p.KmsId == req.KmsId);
_kmsDetails_Repositories.Update(req.KmsDetail);
//_kmsDetails_Repositories.GetList(p => p.KmsId == req.KmsId);
Console.WriteLine("后台导入任务成功:" + req.KmsDetail.DataCount);
}
catch (Exception ex)
{
req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Fail;
_kmsDetails_Repositories.Update(req.KmsDetail);
_kmsDetails_Repositories.Update(req.KmsDetail);
Console.WriteLine("后台导入任务异常:" + ex.Message);
}
}

View File

@@ -1,28 +1,39 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using Microsoft.KernelMemory;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Options;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Other;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using LLama;
using LLamaSharp.KernelMemory;
using Microsoft.Extensions.Configuration;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Configuration;
using Microsoft.KernelMemory.ContentStorage.DevTools;
using Microsoft.KernelMemory.FileSystem.DevTools;
using Microsoft.KernelMemory.Postgres;
using System.Net.Http;
using Microsoft.Extensions.Options;
using Microsoft.KernelMemory.Configuration;
using Microsoft.Extensions.Configuration;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IKMService), ServiceLifetime.Scoped)]
public class KMService(
IConfiguration _config
IConfiguration _config,
IKmss_Repositories _kmss_Repositories,
IAIModels_Repositories _aIModels_Repositories
) : IKMService
{
public MemoryServerless GetMemory(SearchClientConfig searchClientConfig = null, TextPartitioningOptions textPartitioningOptions = null)
public MemoryServerless GetMemoryByKMS(string kmsID, SearchClientConfig searchClientConfig = null)
{
var handler = new OpenAIHttpClientHandler();
var httpClient = new HttpClient(handler);
//获取KMS配置
var kms = _kmss_Repositories.GetFirst(p => p.Id == kmsID);
var chatModel = _aIModels_Repositories.GetFirst(p => p.Id == kms.ChatModelID);
var embedModel = _aIModels_Repositories.GetFirst(p => p.Id == kms.EmbeddingModelID);
//http代理
var chatHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(chatModel.EndPoint);
var embeddingHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(embedModel.EndPoint);
//搜索配置
if (searchClientConfig.IsNull())
{
searchClientConfig = new SearchClientConfig
@@ -34,33 +45,87 @@ namespace AntSK.Domain.Domain.Service
};
}
if (textPartitioningOptions.IsNull())
{
textPartitioningOptions = new TextPartitioningOptions
{
MaxTokensPerLine = 99,
MaxTokensPerParagraph = 299,
OverlappingTokens = 47
};
}
var memory = new KernelMemoryBuilder()
.WithSimpleFileStorage(new SimpleFileStorageConfig { StorageType = FileSystemTypes.Volatile, Directory = "_files" })
.WithSearchClientConfig(searchClientConfig)
.WithCustomTextPartitioningOptions(textPartitioningOptions)
.WithOpenAITextGeneration(new OpenAIConfig()
.WithCustomTextPartitioningOptions(new TextPartitioningOptions
{
APIKey = OpenAIOption.Key,
TextModel = OpenAIOption.Model
MaxTokensPerLine = kms.MaxTokensPerLine,
MaxTokensPerParagraph = kms.MaxTokensPerParagraph,
OverlappingTokens = kms.OverlappingTokens
});
//加载huihu 模型
WithTextGenerationByAIType(memory, chatModel, chatHttpClient);
//加载向量模型
WithTextEmbeddingGenerationByAIType(memory, embedModel, embeddingHttpClient);
//加载向量库
WithMemoryDbByVectorDB(memory, _config);
}, null, httpClient)
.WithOpenAITextEmbeddingGeneration(new OpenAIConfig()
var result = memory.Build<MemoryServerless>();
return result;
}
private void WithTextEmbeddingGenerationByAIType(IKernelMemoryBuilder memory, AIModels embedModel, HttpClient embeddingHttpClient)
{
switch (embedModel.AIType)
{
APIKey = OpenAIOption.Key,
EmbeddingModel = OpenAIOption.EmbeddingModel
case Model.Enum.AIType.OpenAI:
memory.WithOpenAITextEmbeddingGeneration(new OpenAIConfig()
{
APIKey = embedModel.ModelKey,
EmbeddingModel = embedModel.ModelName
}, null, false, embeddingHttpClient);
break;
case Model.Enum.AIType.AzureOpenAI:
memory.WithAzureOpenAITextEmbeddingGeneration(new AzureOpenAIConfig()
{
APIKey = embedModel.ModelKey,
Deployment = embedModel.ModelName.ConvertToString(),
Endpoint = embedModel.EndPoint.ConvertToString(),
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIType = AzureOpenAIConfig.APITypes.EmbeddingGeneration,
});
break;
case Model.Enum.AIType.LLamaSharp:
var (weights, parameters) = LLamaConfig.GetLLamaConfig(embedModel.ModelName);
var embedder = new LLamaEmbedder(weights, parameters);
memory.WithLLamaSharpTextEmbeddingGeneration(new LLamaSharpTextEmbeddingGenerator(embedder));
break;
}
}
}, null, false, httpClient);
private void WithTextGenerationByAIType(IKernelMemoryBuilder memory, AIModels chatModel, HttpClient chatHttpClient)
{
switch (chatModel.AIType)
{
case Model.Enum.AIType.OpenAI:
memory.WithOpenAITextGeneration(new OpenAIConfig()
{
APIKey = chatModel.ModelKey,
TextModel = chatModel.ModelName
}, null, chatHttpClient);
break;
case Model.Enum.AIType.AzureOpenAI:
memory.WithAzureOpenAITextGeneration(new AzureOpenAIConfig()
{
APIKey = chatModel.ModelKey,
Deployment = chatModel.ModelName.ConvertToString(),
Endpoint = chatModel.EndPoint.ConvertToString(),
Auth = AzureOpenAIConfig.AuthTypes.APIKey,
APIType = AzureOpenAIConfig.APITypes.TextCompletion,
});
break;
case Model.Enum.AIType.LLamaSharp:
var (weights, parameters) = LLamaConfig.GetLLamaConfig(chatModel.ModelName);
var context = weights.CreateContext(parameters);
var executor = new StatelessExecutor(weights, parameters);
memory.WithLLamaSharpTextGeneration(new LlamaSharpTextGenerator(weights, context, executor));
break;
}
}
private void WithMemoryDbByVectorDB(IKernelMemoryBuilder memory, IConfiguration _config)
{
string VectorDb = _config["KernelMemory:VectorDb"].ConvertToString();
string ConnectionString = _config["KernelMemory:ConnectionString"].ConvertToString();
string TableNamePrefix = _config["KernelMemory:TableNamePrefix"].ConvertToString();
@@ -86,14 +151,11 @@ namespace AntSK.Domain.Domain.Service
});
break;
}
var result = memory.Build<MemoryServerless>();
return result;
}
public async Task<List<KMFile>> GetDocumentByFileID(string fileid)
public async Task<List<KMFile>> GetDocumentByFileID(string kmsid, string fileid)
{
var _memory = GetMemory();
var _memory = GetMemoryByKMS(kmsid);
var memories = await _memory.ListIndexesAsync();
var memoryDbs = _memory.Orchestrator.GetMemoryDbs();
List<KMFile> docTextList = new List<KMFile>();
@@ -120,5 +182,7 @@ namespace AntSK.Domain.Domain.Service
}
return docTextList;
}
}
}

View File

@@ -1,47 +1,80 @@
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Other;
using AntSK.Domain.Model;
using AntSK.Domain.Options;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using LLama;
using LLamaSharp.SemanticKernel.TextCompletion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Core;
using Microsoft.SemanticKernel.TextGeneration;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ServiceLifetime = AntSK.Domain.Common.DependencyInjection.ServiceLifetime;
namespace AntSK.Domain.Domain.Service
{
[ServiceDescription(typeof(IKernelService), ServiceLifetime.Scoped)]
public class KernelService(
IApis_Repositories _apis_Repositories
) : IKernelService
public class KernelService : IKernelService
{
private readonly IApis_Repositories _apis_Repositories;
private readonly IAIModels_Repositories _aIModels_Repositories;
public KernelService(
IApis_Repositories apis_Repositories,
IAIModels_Repositories aIModels_Repositories
)
{
_apis_Repositories = apis_Repositories;
_aIModels_Repositories = aIModels_Repositories;
}
/// <summary>
/// 获取kernel实例依赖注入不好按每个用户去Import不同的插件所以每次new一个新的kernel
/// </summary>
/// <param name="modelId"></param>
/// <param name="apiKey"></param>
/// <returns></returns>
public Kernel GetKernel(string modelId=null,string apiKey=null)
public Kernel GetKernelByApp(Apps app)
{
var handler = new OpenAIHttpClientHandler();
var httpClient = new HttpClient(handler);
httpClient.Timeout = TimeSpan.FromMinutes(5);
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: modelId!=null? modelId : OpenAIOption.Model,
apiKey: apiKey!=null? apiKey: OpenAIOption.Key,
httpClient: httpClient)
.Build();
var chatModel = _aIModels_Repositories.GetFirst(p => p.Id == app.ChatModelID);
var chatHttpClient = OpenAIHttpClientHandlerUtil.GetHttpClient(chatModel.EndPoint);
var builder = Kernel.CreateBuilder();
WithTextGenerationByAIType(builder, chatModel, chatHttpClient);
var kernel = builder.Build();
RegisterPluginsWithKernel(kernel);
return kernel;
}
private void WithTextGenerationByAIType(IKernelBuilder builder, AIModels chatModel, HttpClient chatHttpClient)
{
switch (chatModel.AIType)
{
case Model.Enum.AIType.OpenAI:
builder.AddOpenAIChatCompletion(
modelId: chatModel.ModelName,
apiKey: chatModel.ModelKey,
httpClient: chatHttpClient);
break;
case Model.Enum.AIType.AzureOpenAI:
builder.AddAzureOpenAIChatCompletion(
deploymentName: chatModel.ModelName,
apiKey: chatModel.ModelKey,
endpoint: chatModel.EndPoint
);
break;
case Model.Enum.AIType.LLamaSharp:
var (weights, parameters) = LLamaConfig.GetLLamaConfig(chatModel.ModelName);
var ex = new StatelessExecutor(weights, parameters);
builder.Services.AddKeyedSingleton<ITextGenerationService>("local-llama", new LLamaSharpTextCompletion(ex));
break;
}
}
/// <summary>
/// 根据app配置的插件导入插件
/// </summary>
@@ -145,7 +178,7 @@ namespace AntSK.Domain.Domain.Service
/// <param name="questions"></param>
/// <param name="history"></param>
/// <returns></returns>
public async Task<string> HistorySummarize(Kernel _kernel,string questions, string history)
public async Task<string> HistorySummarize(Kernel _kernel, string questions, string history)
{
KernelFunction sunFun = _kernel.Plugins.GetFunction("ConversationSummaryPlugin", "SummarizeConversation");
var summary = await _kernel.InvokeAsync(sunFun, new() { ["input"] = $"内容是:{history.ToString()} {Environment.NewLine} 请注意用中文总结" });

View File

@@ -1,9 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,9 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,10 +1,5 @@
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Map
{

View File

@@ -1,11 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model.Enum
namespace AntSK.Domain.Model.Enum
{
/// <summary>
/// AI类型
/// </summary>
public enum AIType
{
OpenAI = 1,
AzureOpenAI = 2,
LLamaSharp = 3
}
/// <summary>
/// 模型类型
/// </summary>
public enum AIModelType
{
Chat = 1,

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public enum HttpMethodType
{

View File

@@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model.Enum
namespace AntSK.Domain.Model.Enum
{
public enum ImportKmsStatus
{
Loadding=0,
Success=1,
Fail=2
Loadding = 0,
Success = 1,
Fail = 2
}
}

View File

@@ -1,10 +1,4 @@
using AntSK.Domain.Repositories;
using Microsoft.KernelMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
{
@@ -26,14 +20,15 @@ namespace AntSK.Domain.Model
}
public class ImportKMSTaskReq: ImportKMSTaskDTO
public class ImportKMSTaskReq : ImportKMSTaskDTO
{
public KmsDetails KmsDetail { get; set; } = new KmsDetails();
}
public enum ImportType {
File=1,
Url=2,
Text=3
public enum ImportType
{
File = 1,
Url = 2,
Text = 3
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public class MessageInfo
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Model
namespace AntSK.Domain.Model
{
public class PageList<T>
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class DBConnectionOption
{

View File

@@ -1,16 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class KernelMemoryOption
{
/// <summary>
/// 向量库
/// </summary>
/// <summary>
/// 向量库
/// </summary>
public static string VectorDb { get; set; }
/// <summary>
/// 连接字符串

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class LLamaSharpOption
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
namespace AntSK.Domain.Options
{
public class LoginOption
{

View File

@@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Options
{
public class OpenAIOption
{
public static string EndPoint { get; set; }
public static string Key { get; set; }
public static string Model { get; set; }
public static string EmbeddingModel { get; set; }
}
}

View File

@@ -1,11 +1,6 @@
using AntSK.Domain.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -37,11 +32,17 @@ namespace AntSK.Domain.Repositories
[Required]
public string Type { get; set; }
/// <summary>
/// 会话模型ID
/// </summary>
[Required]
public string? ChatModelID { get; set; }
/// <summary>
/// 温度
/// </summary>
[SugarColumn(DefaultValue = "70")]
public double Temperature { get; set; }=70f;
public double Temperature { get; set; } = 70f;
/// <summary>
/// 提示词

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -28,6 +23,17 @@ namespace AntSK.Domain.Repositories
/// </summary>
[Required]
public string Describe { get; set; }
/// <summary>
/// 会话模型ID
/// </summary>
[Required]
public string? ChatModelID { get; set; }
/// <summary>
/// 向量模型ID
/// </summary>
[Required]
public string? EmbeddingModelID { get; set; }
/// <summary>
/// 每个段落的最大标记数。
@@ -48,5 +54,7 @@ namespace AntSK.Domain.Repositories
/// </summary>
[SugarColumn(DefaultValue = "49")]
public int OverlappingTokens { get; set; } = 49;
}
}

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using AntSK.Domain.Model.Enum;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using AntSK.Domain.Model;
using SqlSugar;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Model;
namespace AntSK.Domain.Repositories.Base
{

View File

@@ -1,13 +1,7 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Map;
using AntSK.Domain.Map;
using AntSK.Domain.Model;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System.Linq.Expressions;
namespace AntSK.Domain.Repositories.Base
@@ -15,7 +9,7 @@ namespace AntSK.Domain.Repositories.Base
public class Repository<T> : SimpleClient<T> where T : class, new()
{
public Repository( ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
public Repository(ISqlSugarClient context = null) : base(context)//注意这里要有默认值等于null
{
if (context == null)

View File

@@ -1,13 +1,6 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Options;
using AntSK.Domain.Utils;
using AntSK.Domain.Options;
using SqlSugar;
using System.Reflection;
using Microsoft.Extensions.Configuration;
namespace AntSK.Domain.Repositories.Base
{
@@ -17,7 +10,8 @@ namespace AntSK.Domain.Repositories.Base
/// <summary>
/// sqlserver连接
/// </summary>
public static SqlSugarScope SqlScope() {
public static SqlSugarScope SqlScope()
{
string DBType = DBConnectionOption.DbType;
string ConnectionString = DBConnectionOption.ConnectionStrings;
@@ -43,12 +37,12 @@ namespace AntSK.Domain.Repositories.Base
}
};
DbType dbType = (DbType)Enum.Parse(typeof(DbType), DBType);
config.DbType = dbType;
var scope= new SqlSugarScope(config, Db =>
config.DbType = dbType;
var scope = new SqlSugarScope(config, Db =>
{
});
return scope;
}
}
}
}

View File

@@ -1,11 +1,6 @@
using AntSK.Domain.Model.Enum;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{
@@ -15,25 +10,36 @@ namespace AntSK.Domain.Repositories
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; }
/// <summary>
/// AI类型
/// </summary>
[Required]
[SugarColumn(DefaultValue = "1")]
public AIType AIType { get; set; } = AIType.OpenAI;
/// <summary>
/// 模型类型
/// </summary>
[Required]
public AIModelType AIModelType { get; set; }
public AIModelType AIModelType { get; set; } = AIModelType.Chat;
/// <summary>
/// 模型地址
/// </summary>
[Required]
public string EndPoint { get; set; }
public string EndPoint { get; set; } = "";
/// <summary>
/// 模型名称
/// </summary>
[Required]
public string ModelName { get; set; }
public string ModelName { get; set; } = "";
/// <summary>
/// 模型秘钥
/// </summary>
[Required]
public string ModelKey { get; set; }
public string ModelKey { get; set; } = "";
/// <summary>
/// 部署名azure需要使用
/// </summary>
[Required]
public string ModelDescription { get; set; }
}

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,9 +1,4 @@
using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,11 +1,6 @@

using AntSK.Domain.Repositories.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntSK.Domain.Common.DependencyInjection;
using AntSK.Domain.Repositories.Base;
namespace AntSK.Domain.Repositories
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AntSK.Domain.Utils
namespace AntSK.Domain.Utils
{
public static class ConvertUtils
{

View File

@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace AntSK.Domain.Utils
{

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.JSInterop;
namespace AntSK.Domain.Utils
{
public static class JSUtils
{
public static async Task ScrollToBottomAsync(this IJSRuntime _JSRuntime, string elementId)
{
await _JSRuntime.InvokeVoidAsync("scrollToBottom", elementId);
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Buffers.Text;
using System.Buffers.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

View File

@@ -1,22 +1,22 @@
using AntSK.Domain.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace AntSK.Domain.Utils
{
public class OpenAIHttpClientHandler : HttpClientHandler
{
private string _endPoint { get; set; }
public OpenAIHttpClientHandler(string endPoint)
{
this._endPoint = endPoint;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
UriBuilder uriBuilder;
Regex regex = new Regex(@"(https?)://([^/:]+)(:\d+)?/(.*)");
Match match = regex.Match(OpenAIOption.EndPoint);
Match match = regex.Match(_endPoint);
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" && request.Content != null)
{
{
string requestBody = await request.Content.ReadAsStringAsync();
//便于调试查看请求prompt
Console.WriteLine(requestBody);
@@ -40,14 +40,14 @@ namespace AntSK.Domain.Utils
{
// 这里是你要修改的 URL
Scheme = $"{xieyi}://{hostnew}/",
Host = host,
Host = host,
Path = route + "v1/chat/completions",
};
if (port.ConvertToInt32() != 0)
{
uriBuilder.Port = port.ConvertToInt32();
}
request.RequestUri = uriBuilder.Uri;
break;
@@ -74,4 +74,16 @@ namespace AntSK.Domain.Utils
return response;
}
}
public class OpenAIHttpClientHandlerUtil
{
public static HttpClient GetHttpClient(string endPoint)
{
var handler = new OpenAIHttpClientHandler(endPoint.ConvertToString());
var httpClient = new HttpClient(handler);
httpClient.Timeout = TimeSpan.FromMinutes(5);
return httpClient;
}
}
}

View File

@@ -1,6 +1,4 @@
using BCrypt.Net;
namespace AntSK.Domain.Utils
namespace AntSK.Domain.Utils
{
public class PasswordUtil
{

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace AntSK.Domain.Utils
{

View File

@@ -8,8 +8,8 @@
<NoWarn>CA1050,CA1707,CA2007,VSTHRD111,CS1591,RCS1110,CA5394,SKEXP0001,SKEXP0002,SKEXP0003,SKEXP0004,SKEXP0010,SKEXP0011,,SKEXP0012,SKEXP0020,SKEXP0021,SKEXP0022,SKEXP0023,SKEXP0024,SKEXP0025,SKEXP0026,SKEXP0027,SKEXP0028,SKEXP0029,SKEXP0030,SKEXP0031,SKEXP0032,SKEXP0040,SKEXP0041,SKEXP0042,SKEXP0050,SKEXP0051,SKEXP0052,SKEXP0053,SKEXP0054,SKEXP0055,SKEXP0060,SKEXP0061,SKEXP0101,SKEXP0102</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AntDesign.Charts" Version="0.5.1" />
<PackageReference Include="AntDesign.ProLayout" Version="0.17.3" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.2" />

View File

@@ -22,13 +22,13 @@
</summary>
<param name="_taskBroker"></param>
</member>
<member name="M:AntSK.Controllers.LLamaSharpController.chat(AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Controllers.LLamaSharpController.chat(AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
本地会话接口
</summary>
<returns></returns>
</member>
<member name="M:AntSK.Controllers.LLamaSharpController.embedding(AntSK.Models.OpenAIEmbeddingModel)">
<member name="M:AntSK.Controllers.LLamaSharpController.embedding(AntSK.Domain.Domain.Dto.OpenAIEmbeddingModel)">
<summary>
本地嵌入接口
</summary>
@@ -45,7 +45,7 @@
对外接口
</summary>
</member>
<member name="M:AntSK.Controllers.OpenController.chat(AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Controllers.OpenController.chat(AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
对话接口
</summary>
@@ -65,11 +65,11 @@
发送普通对话
</summary>
<param name="questions"></param>
<param name="msg"></param>
<param name="history"></param>
<param name="app"></param>
<returns></returns>
</member>
<member name="M:AntSK.Pages.ChatPage.Chat.HistorySummarize(System.String)">
<member name="M:AntSK.Pages.ChatPage.Chat.HistorySummarize(AntSK.Domain.Repositories.Apps,System.String)">
<summary>
历史会话的会话总结
</summary>
@@ -90,11 +90,11 @@
发送普通对话
</summary>
<param name="questions"></param>
<param name="msg"></param>
<param name="history"></param>
<param name="app"></param>
<returns></returns>
</member>
<member name="M:AntSK.Pages.ChatPage.OpenChat.HistorySummarize(System.String)">
<member name="M:AntSK.Pages.ChatPage.OpenChat.HistorySummarize(AntSK.Domain.Repositories.Apps,System.String)">
<summary>
历史会话的会话总结
</summary>
@@ -136,7 +136,7 @@
<param name="app"></param>
<returns></returns>
</member>
<member name="M:AntSK.Services.OpenApi.OpenApiService.HistorySummarize(AntSK.Models.OpenAIModel)">
<member name="M:AntSK.Services.OpenApi.OpenApiService.HistorySummarize(AntSK.Domain.Repositories.Apps,AntSK.Domain.Domain.Dto.OpenAIModel)">
<summary>
历史会话的会话总结
</summary>

View File

@@ -1,15 +1,12 @@
using AntDesign;
using AntDesign.ProLayout;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AntSK.Domain.Options;
using AntSK.Models;
using AntSK.Services;
using AntSK.Services.Auth;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using AntSK.Services.Auth;
using AntSK.Domain.Options;
namespace AntSK.Components
{
@@ -56,7 +53,7 @@ namespace AntSK.Components
[Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject] protected MessageService? Message { get; set; }
private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();
private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();
protected override async Task OnInitializedAsync()
{
@@ -86,7 +83,7 @@ namespace AntSK.Components
{
NavigationManager.NavigateTo("/setting/user/info/" + context.Identity.Name);
}
else
else
{
_ = Message.Info("管理员无需设置", 2);
}

View File

@@ -1,7 +1,4 @@
using AntDesign;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -32,10 +29,10 @@ namespace AntSK.Controllers
}
string extension = Path.GetExtension(file.FileName);
string fileid=Guid.NewGuid().ToString();
string fileid = Guid.NewGuid().ToString();
// 组合目标路径
var uploads = Path.Combine(uploadsFolderPath, fileid+extension);
var uploads = Path.Combine(uploadsFolderPath, fileid + extension);
// 保存文件至目标路径
using var fileStream = System.IO.File.Create(uploads);
using var uploadStream = file.OpenReadStream();

View File

@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Http;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using AntSK.Domain.Repositories;
namespace AntSK.Controllers
{

View File

@@ -1,14 +1,10 @@
using AntSK.Domain.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static AntSK.Pages.KmsPage.KmsDetail;
using System;
using AntSK.Domain.Repositories;
using AntSK.BackgroundTask;
using AntSK.Domain.Domain.Interface;
using Microsoft.KernelMemory.Configuration;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Map;
using AntSK.BackgroundTask;
using AntSK.Domain.Model;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -27,14 +23,14 @@ namespace AntSK.Controllers
IKmsDetails_Repositories kmsDetails_Repositories,
IKMService iKMService,
BackgroundTaskBroker<ImportKMSTaskReq> taskBroker
)
)
{
_kmsDetails_Repositories = kmsDetails_Repositories;
_iKMService = iKMService;
_taskBroker = taskBroker;
}
[HttpPost]
public async Task<IActionResult> ImportKMSTask(ImportKMSTaskDTO model)
public async Task<IActionResult> ImportKMSTask(ImportKMSTaskDTO model)
{
Console.WriteLine("api/kms/ImportKMSTask 开始");
ImportKMSTaskReq req = model.ToDTO<ImportKMSTaskReq>();

View File

@@ -1,8 +1,6 @@
using AntSK.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Dto;
using AntSK.Services.LLamaSharp;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
{
@@ -39,7 +37,7 @@ namespace AntSK.Controllers
public async Task embedding(OpenAIEmbeddingModel model)
{
Console.WriteLine("开始llama/v1/embeddings");
await _lLamaSharpService.Embedding(model,HttpContext);
await _lLamaSharpService.Embedding(model, HttpContext);
Console.WriteLine("结束llama/v1/embeddings");
}

View File

@@ -1,10 +1,6 @@
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Utils;
using AntSK.Models;
using AntSK.Models.OpenAPI;
using AntSK.Services.OpenApi;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AntSK.Controllers
@@ -25,7 +21,7 @@ namespace AntSK.Controllers
public async Task chat(OpenAIModel model)
{
string sk = HttpContext.Request.Headers["Authorization"].ConvertToString();
await _openApiService.Chat(model,sk, HttpContext);
await _openApiService.Chat(model, sk, HttpContext);
}
}
}

View File

@@ -1,6 +1,4 @@
using System;
namespace AntSK
namespace AntSK
{
public static class DateTimeExtension
{

View File

@@ -4,6 +4,7 @@
@using Microsoft.AspNetCore.Components.Authorization
@using AntSK.Domain.Options
@using AntSK.Domain.Repositories
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage
@inherits LayoutComponentBase
<AntDesign.ProLayout.BasicLayout
@@ -28,6 +29,7 @@
[Inject] public HttpClient HttpClient { get; set; }
[Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject] protected IUsers_Repositories _users_Repositories { get; set; }
[Inject] protected ProtectedSessionStorage _protectedSessionStore { get; set; }
private ClaimsPrincipal context => ((AntSKAuthProvider)AuthenticationStateProvider).GetCurrentUser();
protected override async Task OnInitializedAsync()
@@ -35,15 +37,30 @@
await base.OnInitializedAsync();
//菜单权限控制
var menuList = await HttpClient.GetFromJsonAsync<MenuDataItem[]>("data/menu.json");
if ((bool)context?.Identity.IsAuthenticated)
var userSessionStorageResult = await _protectedSessionStore.GetAsync<UserSession>("UserSession");
var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null;
if (userSession != null || (bool)context?.Identity.IsAuthenticated)
{
if (context.Identity.Name == LoginOption.User)
string username = "";
if (userSession != null)
{
username = userSession.UserName;
}
else
{
username = context.Identity.Name;
}
if (username == LoginOption.User )
{
_menuData = menuList;
}
else
{
var userMenuList = _users_Repositories.GetFirst(p => p.No == context.Identity.Name).MenuRole.Split(",").ToList();
var userMenuList = _users_Repositories.GetFirst(p => p.No == username).MenuRole.Split(",").ToList();
//非管理员用户不允许操作系统设置
_menuData = menuList.Where(p => p.Key != "setting" && userMenuList.Contains(p.Key)).ToArray();
}

View File

@@ -1,5 +1,3 @@
using System;
namespace AntSK.Models
{
public class ActivitiesType

View File

@@ -1,6 +1,4 @@
using System;
namespace AntSK.Models
namespace AntSK.Models
{
public class StepFormModel
{

View File

@@ -1,6 +1,5 @@
using System;
using System.Text.Json.Serialization;
using AntSK.Domain.Utils;
using System.Text.Json.Serialization;
namespace AntSK.Models
{

View File

@@ -0,0 +1,8 @@
namespace AntSK.Models
{
public class UserSession
{
public string UserName { get; set; }
public string Role { get; set; }
}
}

View File

@@ -1,8 +1,6 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using System.IO;
using Microsoft.AspNetCore.Components;
using System.Text.RegularExpressions;
namespace AntSK.Pages.ApiPage
@@ -20,7 +18,7 @@ namespace AntSK.Pages.ApiPage
[Inject]
protected MessageService? Message { get; set; }
private Apis _apiModel = new Apis() ;
private Apis _apiModel = new Apis();
protected override async Task OnInitializedAsync()
@@ -38,7 +36,7 @@ namespace AntSK.Pages.ApiPage
{
//新增
_apiModel.Id = Guid.NewGuid().ToString();
if (_apis_Repositories.IsAny(p => p.Name == _apiModel.Name))
{
_ = Message.Error("名称已存在!", 2);
@@ -54,7 +52,8 @@ namespace AntSK.Pages.ApiPage
_apis_Repositories.Insert(_apiModel);
}
else {
else
{
//修改
_apis_Repositories.Update(_apiModel);
@@ -64,7 +63,8 @@ namespace AntSK.Pages.ApiPage
}
private void Back() {
private void Back()
{
NavigationManager.NavigateTo("/plugins/apilist");
}
}

View File

@@ -1,16 +1,14 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.ApiPage
{
public partial class ApiList
{
private Apis [] _data = { };
private Apis[] _data = { };
[Inject]
[Inject]
protected IApis_Repositories _apis_Repositories { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
@@ -33,7 +31,7 @@ namespace AntSK.Pages.ApiPage
{
data = await _apis_Repositories.GetListAsync(p => p.Name.Contains(searchKey));
}
list.AddRange(data);
_data = list.ToArray();
await InvokeAsync(StateHasChanged);
@@ -62,7 +60,7 @@ namespace AntSK.Pages.ApiPage
if (result == ConfirmResult.Yes)
{
await _apis_Repositories.DeleteAsync(id);
await InitData("");
await InitData("");
}
}
}

View File

@@ -28,6 +28,14 @@
<FormItem Label="描述" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Input Placeholder="请输入描述" @bind-Value="@context.Describe" />
</FormItem>
<FormItem Label="会话模型" LabelCol="LayoutModel._formItemLayout.LabelCol" WrapperCol="LayoutModel._formItemLayout.WrapperCol">
<Select DataSource="@_chatList"
@bind-Value="@context.ChatModelID"
ValueProperty="c=>c.Id"
LabelProperty="c=>'【'+c.AIType.ToString()+'】'+c.ModelDescription">
</Select>
</FormItem>
@if (@context.Type == "chat")
{

View File

@@ -1,8 +1,7 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Model.Enum;
using AntSK.Domain.Repositories;
using AntSK.Models;
using System.IO;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.AppPage
{
@@ -23,10 +22,12 @@ namespace AntSK.Pages.AppPage
protected NavigationManager NavigationManager { get; set; }
[Inject]
protected MessageService? Message { get; set; }
[Inject]
protected IAIModels_Repositories _aimodels_Repositories { get; set; }
private Apps _appModel = new Apps() ;
private Apps _appModel = new Apps();
IEnumerable <string> kmsIds;
IEnumerable<string> kmsIds;
private List<Kmss> _kmsList = new List<Kmss>();
@@ -34,66 +35,72 @@ namespace AntSK.Pages.AppPage
private List<Apis> _apiList = new List<Apis>();
private List<AIModels> _chatList { get; set; }
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
_kmsList = _kmss_Repositories.GetList();
_apiList= _apis_Repositories.GetList();
_apiList = _apis_Repositories.GetList();
_chatList = _aimodels_Repositories.GetList(p => p.AIModelType == AIModelType.Chat);
if (!string.IsNullOrEmpty(AppId))
{
//查看
_appModel= _apps_Repositories.GetFirst(p => p.Id == AppId);
_appModel = _apps_Repositories.GetFirst(p => p.Id == AppId);
kmsIds = _appModel.KmsIdList?.Split(",");
apiIds= _appModel.ApiFunctionList?.Split(",");
apiIds = _appModel.ApiFunctionList?.Split(",");
}
}
private void HandleSubmit()
{
if (kmsIds != null && kmsIds.Count() > 0)
{
var kmsList = _kmss_Repositories.GetList(p => kmsIds.Contains(p.Id));
bool allSameEmbeddingModelID = kmsList.Select(k => k.EmbeddingModelID).Distinct().Count() == 1;
if (!allSameEmbeddingModelID)
{
_ = Message.Error("同一个应用的知识库的Embedding模型必须相同", 2);
return;
}
_appModel.KmsIdList = string.Join(",", kmsIds);
}
if (apiIds != null && apiIds.Count() > 0)
{
_appModel.ApiFunctionList = string.Join(",", apiIds);
}
if (string.IsNullOrEmpty(AppId))
{
//新增
_appModel.Id = Guid.NewGuid().ToString();
//秘钥
_appModel.SecretKey="sk-"+ Guid.NewGuid().ToString();
_appModel.SecretKey = "sk-" + Guid.NewGuid().ToString();
if (_apps_Repositories.IsAny(p => p.Name == _appModel.Name))
{
_ = Message.Error("名称已存在!", 2);
return;
}
if (kmsIds != null && kmsIds.Count() > 0)
{
_appModel.KmsIdList = string.Join(",", kmsIds);
}
if (apiIds != null && apiIds.Count() > 0)
{
_appModel.ApiFunctionList = string.Join(",", apiIds);
}
_apps_Repositories.Insert(_appModel);
}
else {
//修改
if (kmsIds != null && kmsIds.Count() > 0)
{
_appModel.KmsIdList = string.Join(",", kmsIds);
}
if (apiIds != null && apiIds.Count() > 0)
{
_appModel.ApiFunctionList = string.Join(",", apiIds);
}
else
{
//修改
_apps_Repositories.Update(_appModel);
}
//NavigationManager.NavigateTo($"/app/detail/{_appModel.Id}");
NavigationManager.NavigateTo($"/applist");
}
private void Back() {
private void Back()
{
NavigationManager.NavigateTo("/applist");
}
}

View File

@@ -1,16 +1,14 @@
using AntDesign;
using Microsoft.AspNetCore.Components;
using AntSK.Domain.Repositories;
using AntSK.Models;
using AntSK.Services;
using Microsoft.AspNetCore.Components;
namespace AntSK.Pages.AppPage
{
public partial class AppList
{
private Apps [] _data = { };
private Apps[] _data = { };
[Inject]
[Inject]
protected IApps_Repositories _apps_Repositories { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
@@ -33,7 +31,7 @@ namespace AntSK.Pages.AppPage
{
data = await _apps_Repositories.GetListAsync(p => p.Name.Contains(searchKey));
}
list.AddRange(data);
_data = list.ToArray();
await InvokeAsync(StateHasChanged);
@@ -68,7 +66,7 @@ namespace AntSK.Pages.AppPage
if (result == ConfirmResult.Yes)
{
await _apps_Repositories.DeleteAsync(id);
await InitData("");
await InitData("");
}
}
}

View File

@@ -1,9 +1,7 @@
using AntDesign;
using AntSK.Domain.Model;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Repositories;
using AntSK.Models;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.AspNetCore.Components;
using Newtonsoft.Json;
@@ -53,7 +51,7 @@ namespace AntSK.Pages.AppPage
await base.OnInitializedAsync();
_appModel = _apps_Repositories.GetFirst(p => p.Id == AppId);
_openApiUrl = NavigationManager.BaseUri + "api/v1/chat/completions";
_openChatUrl= NavigationManager.BaseUri + "openchat/"+AppId;
_openChatUrl = NavigationManager.BaseUri + "openchat/" + AppId;
GetDesc();
GetScript();
}
@@ -63,7 +61,7 @@ namespace AntSK.Pages.AppPage
_desc = @$"为了方便其他应用对接接口符合openai规范省略了温度TopP等参数。{Environment.NewLine}BaseUrl:{Environment.NewLine}{_openApiUrl} {Environment.NewLine}headers:{Environment.NewLine}Authorization: ""{_appModel.SecretKey}"" {Environment.NewLine}Body: {Environment.NewLine}{JsonConvert.SerializeObject(new OpenAIModel() { messages = new List<OpenAIMessage>() { new OpenAIMessage() { role = "user", content = "" } } }, Formatting.Indented)}";
}
private void GetScript()
private void GetScript()
{
_script = $"<script src=\"{NavigationManager.BaseUri}js/iframe.js\" data-width=\"40rem\" data-height=\"80vh\" id=\"antsk-iframe\" data-src=\"{NavigationManager.BaseUri}openchat/{AppId}\" data-color=\"#4e83fd\" data-message-icon-url=\"{NavigationManager.BaseUri}assets/ai.png\"></script>";
}

View File

@@ -30,12 +30,14 @@
<GridRow Style="width:100%">
<GridCol Span="23">
<div class="chat-bubble sent">
@(item.Context)
@* <span class="timestamp">@item.CreateTime</span> *@
<Popover Title="@item.CreateTime.ToString()">
@(item.Context)
</Popover>
</div>
<Icon Style="float:right;margin-top:10px;" Type="copy" Theme="outline" OnClick="async () =>await OnCopyAsync(item)" />
</GridCol>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;margin-right:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
</GridCol>
</GridRow>
}
@@ -43,12 +45,11 @@
{
<GridRow Style="width:100%">
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
</GridCol>
<GridCol Span="23">
<div class="chat-bubble received">
@((MarkupString)(item.HtmlAnswers))
@* <span class="timestamp">@item.CreateTime</span> *@
</div>
</GridCol>
</GridRow>
@@ -59,6 +60,7 @@
<div style="height: 10px;"></div>
<AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding">
<Suffix>
<Button Icon="clear" Type="@(ButtonType.Link)" OnClick="@(async () => await OnClearAsync())" Disabled="@Sendding"></Button>
<Button Icon="send" Type="@(ButtonType.Link)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
</Suffix>
</AntDesign.Input>
@@ -75,8 +77,8 @@
</Extra>
<Body>
<AntList Bordered DataSource="@RelevantSources" Style="padding:10px;">
<ChildContent Context="item" >
<AntList Bordered DataSource="@_relevantSources" Style="padding:10px;">
<ChildContent Context="item">
<span> <b>@item.SourceName </b> 相似度:<Text Mark> @item.Relevance</Text></span>
<Body>
@((MarkupString)(@item.Text))
@@ -129,22 +131,7 @@
background-color: #daf8cb;
align-self: flex-end;
float: right;
}
.timestamp {
display: block;
font-size: 0.75em;
margin-top: 5px;
}
.received .timestamp {
text-align: right;
margin-right: 10px;
}
.sent .timestamp {
text-align: left;
margin-left: 10px;
position: relative;
}
</style>
@code {

View File

@@ -1,24 +1,18 @@
using AntDesign;
using AntSK.Domain.Domain.Dto;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using Azure.AI.OpenAI;
using Azure.Core;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Wordprocessing;
using MarkdownSharp;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Microsoft.KernelMemory;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Newtonsoft.Json;
using RestSharp;
using SqlSugar;
using System;
using System.Text;
using AntSK.Domain.Utils;
namespace AntSK.Pages.ChatPage
{
@@ -36,13 +30,16 @@ namespace AntSK.Pages.ChatPage
protected IKmss_Repositories _kmss_Repositories { get; set; }
[Inject]
protected IKmsDetails_Repositories _kmsDetails_Repositories { get; set; }
[Inject] IJSRuntime _JSRuntime { get; set; }
//[Inject]
//protected Kernel _kernel { get; set; }
[Inject]
protected IKernelService _kernelService { get; set; }
[Inject]
protected IKMService _kMService { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
[Inject]
IChatService _chatService { get; set; }
protected bool _loading = false;
protected List<MessageInfo> MessageList = [];
@@ -50,7 +47,7 @@ namespace AntSK.Pages.ChatPage
protected string _json = "";
protected bool Sendding = false;
List<RelevantSource> RelevantSources = new List<RelevantSource>();
List<RelevantSource> _relevantSources = new List<RelevantSource>();
protected List<Apps> _list = new List<Apps>();
protected override async Task OnInitializedAsync()
@@ -91,7 +88,8 @@ namespace AntSK.Pages.ChatPage
catch (System.Exception ex)
{
Sendding = false;
_ = Message.Error("异常:"+ex.Message, 2);
Console.WriteLine("异常:" + ex.Message);
_ = Message.Error("异常:" + ex.Message, 2);
}
}
protected async Task OnCopyAsync(MessageInfo item)
@@ -102,24 +100,34 @@ namespace AntSK.Pages.ChatPage
});
}
protected async Task OnClearAsync(string id)
protected async Task OnClearAsync()
{
await Task.Run(() =>
if (MessageList.Count > 0)
{
MessageList = MessageList.Where(w => w.ID != id).ToList();
});
var content = "是否要清理会话记录";
var title = "清理";
var result = await _confirmService.Show(content, title, ConfirmButtons.YesNo);
if (result == ConfirmResult.Yes)
{
MessageList.Clear();
_ = Message.Info("清理成功");
}
}
else
{
_ = Message.Info("没有会话记录");
}
}
protected async Task<bool> SendAsync(string questions)
{
string msg = "";
//处理多轮会话
Apps app = _apps_Repositories.GetFirst(p => p.Id == AppId);
if (MessageList.Count > 0)
{
msg = await HistorySummarize(questions);
msg = await HistorySummarize(app, questions);
}
Apps app = _apps_Repositories.GetFirst(p => p.Id == AppId);
switch (app.Type)
{
case "chat":
@@ -144,115 +152,69 @@ namespace AntSK.Pages.ChatPage
/// <returns></returns>
private async Task SendKms(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernel();
var _memory = _kMService.GetMemory();
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance*100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
//输出调试信息
var markdown = new Markdown();
string sourceName = item.SourceName;
var fileDetail = _kmsDetails_Repositories.GetFirst(p => p.FileGuidName == item.SourceName);
if (fileDetail.IsNotNull())
{
sourceName = fileDetail.FileName;
}
RelevantSources.Add(new RelevantSource() { SourceName = sourceName, Text = markdown.Transform(part.Text), Relevance = part.Relevance });
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = msg, ["questions"]=questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
}
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="msg"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernel();
if (string.IsNullOrEmpty(app.Prompt)||!app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString()+"{{$input}}";
}
var temperature = app.Temperature/100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() {Temperature= temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,Temperature = temperature };
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: func, arguments: new KernelArguments() { ["input"] = msg });
MessageInfo info = null;
var markdown = new Markdown();
var markdown1 = new Markdown();
var chatResult = _chatService.SendKmsByAppAsync(app, questions, msg, _relevantSources);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.Context = content?.ConvertToString();
info.HtmlAnswers = content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string history, Apps app)
{
MessageInfo info =null;
var markdown = new Markdown();
var chatResult = _chatService.SendChatByAppAsync(app, questions, history);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.ConvertToString();
info.HtmlAnswers = content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
@@ -261,20 +223,20 @@ namespace AntSK.Pages.ChatPage
{
info!.HtmlAnswers = markdown.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 历史会话的会话总结
/// </summary>
/// <param name="questions"></param>
/// <returns></returns>
private async Task<string> HistorySummarize(string questions)
private async Task<string> HistorySummarize(Apps app, string questions)
{
var _kernel = _kernelService.GetKernel();
var _kernel = _kernelService.GetKernelByApp(app);
if (MessageList.Count > 1)
{
StringBuilder history = new StringBuilder();
@@ -295,24 +257,19 @@ namespace AntSK.Pages.ChatPage
var msg = await _kernelService.HistorySummarize(_kernel, questions, history.ToString());
return msg;
}
else
else
{
var msg = $"history{history.ToString()}{Environment.NewLine} user{questions}"; ;
var msg = $"history{history.ToString()}{Environment.NewLine}";
return msg;
}
}
}
else
else
{
return questions;
return "";
}
}
}
public class RelevantSource
{
public string SourceName { get; set; }
public string Text { get; set; }
public float Relevance { get; set; }
}
}

View File

@@ -14,12 +14,14 @@
<GridRow>
<GridCol Span="23">
<div class="chat-bubble sent">
@(item.Context)
@* <span class="timestamp">@item.CreateTime</span> *@
<Popover Title="@item.CreateTime.ToString()">
@(item.Context)
</Popover>
</div>
<Icon Style="float:right;margin-top:10px;" Type="copy" Theme="outline" OnClick="async () =>await OnCopyAsync(item)" />
</GridCol>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
</GridCol>
</GridRow>
}
@@ -27,13 +29,13 @@
{
<GridRow>
<GridCol Span="1">
<Image Width="100%" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
<Image Width="25px" Height="25px" Style="margin-top:10px;" Src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" />
</GridCol>
<GridCol Span="23">
<div class="chat-bubble received">
@((MarkupString)(item.HtmlAnswers))
@* <span class="timestamp">@item.CreateTime</span> *@
</div>
</GridCol>
</GridRow>
}
@@ -43,6 +45,7 @@
<div style="flex-shrink:0;margin:10px;">
<AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding">
<Suffix>
<Button Icon="clear" Type="@(ButtonType.Link)" OnClick="@(async () => await OnClearAsync())" Disabled="@Sendding"></Button>
<Button Icon="send" Type="@(ButtonType.Link)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
</Suffix>
</AntDesign.Input>
@@ -91,22 +94,6 @@
align-self: flex-end;
float: right;
}
.timestamp {
display: block;
font-size: 0.75em;
margin-top: 5px;
}
.received .timestamp {
text-align: right;
margin-right: 10px;
}
.sent .timestamp {
text-align: left;
margin-left: 10px;
}
</style>
@code {

View File

@@ -1,24 +1,17 @@
using AntDesign;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Model;
using AntSK.Domain.Repositories;
using AntSK.Domain.Utils;
using Azure.AI.OpenAI;
using Azure.Core;
using DocumentFormat.OpenXml.EMMA;
using MarkdownSharp;
using Microsoft.AspNetCore.Components;
using Microsoft.KernelMemory;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Text;
using AntSK.Domain.Utils;
using AntSK.Domain.Domain.Interface;
using AntSK.Domain.Domain.Service;
using Microsoft.JSInterop;
namespace AntSK.Pages.ChatPage
{
@@ -26,7 +19,7 @@ namespace AntSK.Pages.ChatPage
{
[Parameter]
public string AppId { get; set; }
[Inject]
[Inject]
protected MessageService? Message { get; set; }
[Inject]
protected IApps_Repositories _apps_Repositories { get; set; }
@@ -38,6 +31,12 @@ namespace AntSK.Pages.ChatPage
protected IKernelService _kernelService { get; set; }
[Inject]
protected IKMService _kMService { get; set; }
[Inject]
IConfirmService _confirmService { get; set; }
[Inject]
IChatService _chatService { get; set; }
[Inject] IJSRuntime _JSRuntime { get; set; }
protected bool _loading = false;
protected List<MessageInfo> MessageList = [];
@@ -50,7 +49,26 @@ namespace AntSK.Pages.ChatPage
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
app = _apps_Repositories.GetFirst(p=>p.Id==AppId);
app = _apps_Repositories.GetFirst(p => p.Id == AppId);
}
protected async Task OnClearAsync()
{
if (MessageList.Count > 0)
{
var content = "是否要清理会话记录";
var title = "清理";
var result = await _confirmService.Show(content, title, ConfirmButtons.YesNo);
if (result == ConfirmResult.Yes)
{
MessageList.Clear();
_ = Message.Info("清理成功");
}
}
else
{
_ = Message.Info("没有会话记录");
}
}
protected async Task OnSendAsync()
{
@@ -79,7 +97,8 @@ namespace AntSK.Pages.ChatPage
catch (System.Exception ex)
{
Sendding = false;
_ = Message.Error("异常:"+ex.Message, 2);
Console.WriteLine("异常:" + ex.Message);
_ = Message.Error("异常:" + ex.Message, 2);
}
}
@@ -103,12 +122,11 @@ namespace AntSK.Pages.ChatPage
{
string msg = "";
//处理多轮会话
Apps app = _apps_Repositories.GetFirst(p => p.Id == AppId);
if (MessageList.Count > 0)
{
msg = await HistorySummarize(questions);
msg = await HistorySummarize(app, questions);
}
Apps app=_apps_Repositories.GetFirst(p => p.Id == AppId);
switch (app.Type)
{
case "chat":
@@ -133,114 +151,72 @@ namespace AntSK.Pages.ChatPage
/// <returns></returns>
private async Task SendKms(string questions, string msg, Apps app)
{
var _kernel = _kernelService.GetKernel();
var _memory = _kMService.GetMemory();
//知识库问答
var filters = new List<MemoryFilter>();
var kmsidList = app.KmsIdList.Split(",");
foreach (var kmsid in kmsidList)
{
filters.Add(new MemoryFilter().ByTag("kmsid", kmsid));
}
var xlresult = await _memory.SearchAsync(questions, index: "kms", filters: filters);
string dataMsg = "";
if (xlresult != null)
{
foreach (var item in xlresult.Results)
{
foreach (var part in item.Partitions)
{
dataMsg += $"[file:{item.SourceName};Relevance:{(part.Relevance * 100).ToString("F2")}%]:{part.Text}{Environment.NewLine}";
}
}
KernelFunction jsonFun = _kernel.Plugins.GetFunction("KMSPlugin", "Ask");
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: jsonFun,
arguments: new KernelArguments() { ["doc"] = dataMsg, ["history"] = msg, ["questions"] = questions });
MessageInfo info = null;
var markdown1 = new Markdown();
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
}
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="msg"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string msg, Apps app)
{
var _kernel= _kernelService.GetKernel();
if (string.IsNullOrEmpty(app.Prompt) || !app.Prompt.Contains("{{$input}}"))
{
//如果模板为空,给默认提示词
app.Prompt = app.Prompt.ConvertToString() + "{{$input}}";
}
//注册插件
var temperature = app.Temperature / 100;//存的是0~100需要缩小
OpenAIPromptExecutionSettings settings = new() { Temperature = temperature };
if (!string.IsNullOrEmpty(app.ApiFunctionList))
{
_kernelService.ImportFunctionsByApp(app, _kernel);
settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions, Temperature = temperature };
}
var func = _kernel.CreateFunctionFromPrompt(app.Prompt, settings);
var chatResult = _kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function: func, arguments: new KernelArguments() { ["input"] = msg });
MessageInfo info = null;
var markdown = new Markdown();
var markdown1 = new Markdown();
var chatResult=_chatService.SendKmsByAppAsync(app, questions, msg);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content?.Content?.ConvertToString();
info.HtmlAnswers = content?.Content?.ConvertToString();
info.Context = content.ConvertToString();
info.HtmlAnswers = content.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.Content;
await Task.Delay(50);
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
if (info.IsNotNull())
{
info!.HtmlAnswers = markdown1.Transform(info.HtmlAnswers);
}
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
/// 发送普通对话
/// </summary>
/// <param name="questions"></param>
/// <param name="history"></param>
/// <param name="app"></param>
/// <returns></returns>
private async Task SendChat(string questions, string history, Apps app)
{
MessageInfo info = null;
var markdown = new Markdown();
var chatResult = _chatService.SendChatByAppAsync(app, questions, history);
await foreach (var content in chatResult)
{
if (info == null)
{
info = new MessageInfo();
info.ID = Guid.NewGuid().ToString();
info.Context = content.ConvertToString();
info.HtmlAnswers = content.ConvertToString();
info.CreateTime = DateTime.Now;
MessageList.Add(info);
}
else
{
info.HtmlAnswers += content.ConvertToString();
await Task.Delay(50);
}
await InvokeAsync(StateHasChanged);
}
//全部处理完后再处理一次Markdown
info!.HtmlAnswers = markdown.Transform(info.HtmlAnswers);
await InvokeAsync(StateHasChanged);
await _JSRuntime.ScrollToBottomAsync("scrollDiv");
}
/// <summary>
@@ -248,9 +224,9 @@ namespace AntSK.Pages.ChatPage
/// </summary>
/// <param name="questions"></param>
/// <returns></returns>
private async Task<string> HistorySummarize(string questions)
private async Task<string> HistorySummarize(Apps app, string questions)
{
var _kernel = _kernelService.GetKernel();
var _kernel = _kernelService.GetKernelByApp(app);
if (MessageList.Count > 1)
{
StringBuilder history = new StringBuilder();
@@ -277,9 +253,9 @@ namespace AntSK.Pages.ChatPage
return msg;
}
}
else
else
{
return questions;
return "";
}
}
}

Some files were not shown because too many files have changed in this diff Show More