Skip to content

Commit

Permalink
- [fix] 实现mem0知识库
Browse files Browse the repository at this point in the history
- [fix] 实现量化列表显示
- [fix] 实现自定义文本上传
- [fix] 强依赖Qdrant向量知识库提示性能和查询准确性
  • Loading branch information
239573049 committed Aug 18, 2024
1 parent a08b526 commit 84af2dd
Show file tree
Hide file tree
Showing 54 changed files with 6,031 additions and 662 deletions.
14 changes: 14 additions & 0 deletions FastWiki.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F43B140B-B
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.FastWiki.FunctionCall", "src\test\Test.FastWiki.FunctionCall\Test.FastWiki.FunctionCall.csproj", "{088D9218-E443-4129-846B-F6ECBE283B3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mem0.NET", "mem0.NET\src\mem0.NET\mem0.NET.csproj", "{85BC583E-E653-4826-9307-6B7ACE16D9C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mem0.NET.Qdrant", "mem0.NET\src\mem0.NET.Qdrant\mem0.NET.Qdrant.csproj", "{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -60,6 +64,14 @@ Global
{088D9218-E443-4129-846B-F6ECBE283B3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{088D9218-E443-4129-846B-F6ECBE283B3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{088D9218-E443-4129-846B-F6ECBE283B3E}.Release|Any CPU.Build.0 = Release|Any CPU
{85BC583E-E653-4826-9307-6B7ACE16D9C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85BC583E-E653-4826-9307-6B7ACE16D9C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85BC583E-E653-4826-9307-6B7ACE16D9C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85BC583E-E653-4826-9307-6B7ACE16D9C2}.Release|Any CPU.Build.0 = Release|Any CPU
{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,6 +86,8 @@ Global
{E1FFCC22-637F-41AE-BBB4-6BD7009784E7} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
{F43B140B-B189-41BE-8639-D83BC900B635} = {2A86D1DF-3A54-483F-9F59-0F2F82ED7834}
{088D9218-E443-4129-846B-F6ECBE283B3E} = {F43B140B-B189-41BE-8639-D83BC900B635}
{85BC583E-E653-4826-9307-6B7ACE16D9C2} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
{6E2ABCCD-939A-4CFC-9B37-5903CEB134C5} = {D6D3A051-B8FE-4356-B582-921A6726ED40}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C6848D28-988E-4785-AF4D-2554B6E6F9E4}
Expand Down
2 changes: 0 additions & 2 deletions README-zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ docker run -d \
-e OPENAI_CHAT_EMBEDDING_ENDPOINT=https://api.openai.com \
-e DEFAULT_TYPE=sqlite \
-e DEFAULT_CONNECTION=Data\ Source=/app/data/fast-wiki.db \
-e WIKI_TYPE=sqlite \
-e WIKI_CONNECTION=/app/data/wiki.db \
-e OPENAI_CHAT_TOKEN=您的TokenKey \
-e ASPNETCORE_ENVIRONMENT=Development \
Expand Down Expand Up @@ -183,7 +182,6 @@ FastWikiService环境变量参数:
- OPENAI_EMBEDDING_TOKEN: Embedding的Token, 默认为空,为空则使用对话的Token
- DEFAULT_TYPE:业务数据库类型 默认sqlite|[pgsql|postgres]
- DEFAULT_CONNECTION:业务数据库连接字符串
- WIKI_TYPE: wiki数据库类型 默认disk|[pgsql|postgres]
- WIKI_CONNECTION: wiki数据库连接字符串(如果是disk则是目录)

## 技术交流
Expand Down
26 changes: 25 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
dockerfile: ./src/Service/FastWiki.Service/Dockerfile
depends_on:
- postgres
- qdrant
volumes:
- ./wwwroot/uploads:/app/wwwroot/uploads
- ./data:/app/data
Expand All @@ -24,7 +25,25 @@ services:
- WIKI_CONNECTION=Host=postgres;Port=5432;Database=wiki;Username=token;Password=dd666666
- OPENAI_CHAT_TOKEN=您的TokenKey
- ASPNETCORE_ENVIRONMENT=Development
- TZ=Asia/Shanghai

qdrant:
image: registry.token-ai.cn/qdrant/qdrant:latest
restart: always
container_name: qdrant
ports:
- 6333:6333
- 6334:6334
expose:
- 6333
- 6334
- 6335
configs:
- source: qdrant_config
target: /qdrant/config/production.yaml
volumes:
- ./qdrant_data:/qdrant/storage

postgres: # 当前compose服务名
image: registry.cn-shenzhen.aliyuncs.com/fast-wiki/pgvector:v0.5.0 # 拉取的数据库镜像
container_name: postgres # 容器运行的容器名称
Expand All @@ -35,4 +54,9 @@ services:
POSTGRES_DB: wiki # 默认数据库
TZ: Asia/Shanghai # 数据库时区
volumes:
- ./postgresql:/var/lib/postgresql/data # 将PostgreSql数据持久化
- ./postgresql:/var/lib/postgresql/data # 将PostgreSql数据持久化

configs:
qdrant_config:
content: |
log_level: INFO
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class UpdateChatApplicationInput
/// 参数
/// </summary>
public Dictionary<string, string> Parameter { get; set; }

/// <summary>
/// 匹配相似度
/// </summary>
public double Relevancy { get; set; } = 0.4;

public string Opener { get; set; }

Expand Down
23 changes: 23 additions & 0 deletions src/Contracts/FastWiki.Service.Contracts/QuantizedListState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace FastWiki.Service.Contracts;


/// <summary>
/// 表示量化列表的状态。
/// </summary>
public enum QuantizedListState
{
/// <summary>
/// 待处理状态。
/// </summary>
Pending = 1,

/// <summary>
/// 成功状态。
/// </summary>
Success = 2,

/// <summary>
/// 失败状态。
/// </summary>
Fail = 3
}
14 changes: 14 additions & 0 deletions src/Contracts/FastWiki.Service.Contracts/VectorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FastWiki.Service.Contracts;

public enum VectorType
{
/// <summary>
/// 默认向量方式
/// </summary>
None,

/// <summary>
/// Mem0向量方式
/// </summary>
Mem0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

public class CheckQuantizationStateDto
{
/// <summary>
/// 知识库Id
/// </summary>
public long WikiId { get; set; }

/// <summary>
/// 文件名称
/// </summary>
Expand All @@ -15,5 +10,34 @@ public class CheckQuantizationStateDto
/// <summary>
/// 知识库状态
/// </summary>
public WikiQuantizationState State { get; set; }
public QuantizedListState State { get; set; }

public string StateName
{
get
{
return State switch
{
QuantizedListState.Pending => "处理中",
QuantizedListState.Fail => "处理失败",
QuantizedListState.Success => "处理成功",
_ => "未知"
};
}
}

/// <summary>
/// 备注
/// </summary>
public string Remark { get; set; }

/// <summary>
/// 处理时间
/// </summary>
public DateTime? ProcessTime { get; set; }

/// <summary>
/// 创建时间
/// </summary>
public DateTime CreationTime { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public sealed class CreateWikiInput
/// 知识库向量化模型
/// </summary>
public string EmbeddingModel { get; set; } = "text-embedding-3-small";

/// <summary>
/// 向量数据库类型
/// </summary>
public VectorType VectorType { get; set; }
}
3 changes: 2 additions & 1 deletion src/Infrastructure/FastWiki.FunctionCall/HttpClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Net.Http.Json;
using System.Text;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace FastWiki.FunctionCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ namespace FastWiki.Service.Application.Wikis;
public sealed class WikiCommandHandler(
IWikiRepository wikiRepository,
WikiMemoryService wikiMemoryService,
mem0.NET.Services.MemoryService memoryService,
IMapper mapper,
IEventBus eventBus)
{
[EventHandler]
public async Task CreateWiki(CreateWikiCommand command)
{
var wiki = new Wiki(command.Input.Icon, command.Input.Name, command.Input.Model, command.Input.EmbeddingModel);
var wiki = new Wiki(command.Input.Icon, command.Input.Name, command.Input.Model, command.Input.EmbeddingModel,
command.Input.VectorType);
await wikiRepository.AddAsync(wiki);
}

Expand Down Expand Up @@ -100,12 +102,22 @@ public async Task CreateWikiDetailDataAsync(CreateWikiDetailDataCommand command)
[EventHandler]
public async Task RemoveWikiDetailsCommand(RemoveWikiDetailsCommand command)
{
var wikiDetail = await wikiRepository.GetDetailsAsync(command.Id);

await wikiRepository.RemoveDetailsAsync(command.Id);

try
{
var memoryServerless = wikiMemoryService.CreateMemoryServerless(string.Empty, string.Empty);
await memoryServerless.DeleteDocumentAsync(command.Id.ToString(), "wiki");
var wiki = await wikiRepository.FindAsync(wikiDetail.WikiId);
if (wiki?.VectorType == VectorType.Mem0)
{
await memoryService.DeleteAll(null, wiki.Id.ToString(), command.Id.ToString());
}
else
{
var memoryServerless = wikiMemoryService.CreateMemoryServerless(string.Empty, string.Empty);
await memoryServerless.DeleteDocumentAsync(command.Id.ToString(), "wiki");
}
}
catch (Exception e)
{
Expand All @@ -116,7 +128,6 @@ public async Task RemoveWikiDetailsCommand(RemoveWikiDetailsCommand command)
[EventHandler]
public async Task RemoveWikiDetailVectorQuantityAsync(RemoveWikiDetailVectorQuantityCommand command)
{

var memoryServerless = wikiMemoryService.CreateMemoryServerless(string.Empty, string.Empty);
await memoryServerless.DeleteDocumentAsync(command.DocumentId, "wiki");
}
Expand Down
Loading

0 comments on commit 84af2dd

Please sign in to comment.