Skip to content

Commit

Permalink
优化框架设计
Browse files Browse the repository at this point in the history
优化代码结构
  • Loading branch information
239573049 committed Jul 31, 2024
1 parent d89531f commit 3bf7bcb
Show file tree
Hide file tree
Showing 33 changed files with 166 additions and 328 deletions.
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>0.1.3</Version>
<AssemblyVersion>0.1.3</AssemblyVersion>
<Version>0.1.4</Version>
<AssemblyVersion>0.1.4</AssemblyVersion>
<Authors>AIDotNet</Authors>
<Company>AIDotNet</Company>
<PackageId>$(AssemblyName)</PackageId>
Expand All @@ -12,7 +12,7 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<SKVersion>1.16.1</SKVersion>
<MicrosoftVersion>8.0.0</MicrosoftVersion>
<EntityFrameworkVersion>8.0.7</EntityFrameworkVersion>
<EntityFrameworkVersion>8.0.0</EntityFrameworkVersion>
<QdrantVersion>1.10.0</QdrantVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
14 changes: 0 additions & 14 deletions mem0.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mem0.NET.Qdrant", "src\mem0.NET.Qdrant\mem0.NET.Qdrant.csproj", "{F6D8FF6D-7376-479B-B673-DCC0BE715336}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mem0.Core", "src\mem0.Core\mem0.Core.csproj", "{89EA032E-1A1B-4B19-9121-7258C818FD00}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "mem0.NET.Service", "src\mem0.NET.Service\mem0.NET.Service.csproj", "{4755664B-6109-441B-97D6-1EE3117533EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mem0.NET.EntityFramework", "src\mem0.NET.EntityFramework\mem0.NET.EntityFramework.csproj", "{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,27 +31,17 @@ Global
{F6D8FF6D-7376-479B-B673-DCC0BE715336}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6D8FF6D-7376-479B-B673-DCC0BE715336}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6D8FF6D-7376-479B-B673-DCC0BE715336}.Release|Any CPU.Build.0 = Release|Any CPU
{89EA032E-1A1B-4B19-9121-7258C818FD00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89EA032E-1A1B-4B19-9121-7258C818FD00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89EA032E-1A1B-4B19-9121-7258C818FD00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89EA032E-1A1B-4B19-9121-7258C818FD00}.Release|Any CPU.Build.0 = Release|Any CPU
{4755664B-6109-441B-97D6-1EE3117533EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4755664B-6109-441B-97D6-1EE3117533EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4755664B-6109-441B-97D6-1EE3117533EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4755664B-6109-441B-97D6-1EE3117533EF}.Release|Any CPU.Build.0 = Release|Any CPU
{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E33A18E4-609D-4565-89B3-03F83D8810D3} = {D2C48892-B1D7-4C7E-B82E-B059CAD9B95F}
{F6D8FF6D-7376-479B-B673-DCC0BE715336} = {D2C48892-B1D7-4C7E-B82E-B059CAD9B95F}
{89EA032E-1A1B-4B19-9121-7258C818FD00} = {D2C48892-B1D7-4C7E-B82E-B059CAD9B95F}
{4755664B-6109-441B-97D6-1EE3117533EF} = {D2C48892-B1D7-4C7E-B82E-B059CAD9B95F}
{7DC30AE7-0C55-4CFF-9C30-33B12296B4CE} = {D2C48892-B1D7-4C7E-B82E-B059CAD9B95F}
EndGlobalSection
EndGlobal
13 changes: 0 additions & 13 deletions src/mem0.Core/mem0.Core.csproj

This file was deleted.

20 changes: 0 additions & 20 deletions src/mem0.NET.EntityFramework/ServiceCollectionExtensions.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/mem0.NET.EntityFramework/mem0.NET.EntityFramework.csproj

This file was deleted.

9 changes: 3 additions & 6 deletions src/mem0.NET.Qdrant/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ public static class ServiceCollectionExtensions
/// 注册Vector Qdrant实现
/// </summary>
/// <param name="services"></param>
/// <param name="qdrantOptions"></param>
/// <param name="options"></param>
/// <returns></returns>
public static Mem0Builder AddVectorQdrant(this Mem0Builder services,
Action<QdrantOptions> qdrantOptions)
QdrantOptions options)
{
var options = new QdrantOptions();
qdrantOptions.Invoke(options);

services.Services.AddSingleton(_ =>
{
var client = new QdrantClient(options.Host, options.Port, options.Https, options.ApiKey,
Expand All @@ -27,7 +24,7 @@ public static Mem0Builder AddVectorQdrant(this Mem0Builder services,
return client;
});

services.Services.AddScoped<IVectorStoreService, QdrantVectorStoresService>();
services.Services.AddSingleton<IVectorStoreService, QdrantVectorStoresService>();

return services;
}
Expand Down
1 change: 1 addition & 0 deletions src/mem0.NET.Qdrant/Services/QdrantVectorStoresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public async Task<List<SearchHit>> Search(string name, float[] query, ulong limi
var filter = CreateFilter(filters);
var hits = await client.SearchAsync(name, query.ToArray(), filter, limit: limit);


return hits.Select(hit => new SearchHit
{
Id = hit.Id,
Expand Down
6 changes: 1 addition & 5 deletions src/mem0.NET.Qdrant/mem0.NET.Qdrant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mem0.Core\mem0.Core.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mem0.Core\mem0.Core.csproj" />
<ProjectReference Include="..\mem0.NET\mem0.NET.csproj" />
</ItemGroup>

</Project>
35 changes: 16 additions & 19 deletions src/mem0.NET.Service/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using mem0.NET.Options;
using mem0.NET.Service.DataAccess;
using mem0.NET.Service.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection.Options;

namespace mem0.NET.Service;

Expand All @@ -10,28 +12,23 @@ public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMem0DotNetEntityFramework(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("Default"));
});

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddMem0DotNet(options =>
{
options.OpenAIEndpoint = "https://api.token-ai.cn";
options.OpenAIKey = "请在 https://api.token-ai.cn 注册获取";
options.OpenAIChatCompletionModel = "gpt-4o-mini";
options.OpenAITextEmbeddingModel = "text-embedding-ada-002";
options.CollectionName = "mem0.NET";
}).AddVectorQdrant(options =>
{
options.Host = "localhost";
options.Port = 6334;
options.Https = false;
options.ApiKey = "dd666666";
});
builder.Services.AddOptions<Mem0Options>()
.Bind(builder.Configuration.GetSection("Mem0"));

builder.Services.AddOptions<QdrantOptions>()
.Bind(builder.Configuration.GetSection("Qdrant"));

var options = builder.Configuration.GetSection("Mem0")
.Get<Mem0Options>();
var qdrantOptions = builder.Configuration.GetSection("Qdrant")
.Get<QdrantOptions>();

builder.Services.AddMem0DotNet(options,
optionsBuilder => { optionsBuilder.UseNpgsql(builder.Configuration.GetConnectionString("Default")); })
.AddVectorQdrant(qdrantOptions);

var app = builder.Build();

Expand Down
44 changes: 16 additions & 28 deletions src/mem0.NET.Service/Services/MemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using mem0.NET.Functions;
using mem0.NET.Options;
using mem0.NET.Services;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Embeddings;

#pragma warning disable SKEXP0001

Expand All @@ -21,76 +18,67 @@ public static WebApplication MapMemoryService(this WebApplication app)
.WithDisplayName("Memory");

memoryService.MapPost("/memory", async (MemoryService memoryService,
CreateMemoryInput input,
Mem0DotNetOptions mem0DotNetOptions) =>
CreateMemoryInput input) =>
{
await memoryService.CreateMemoryAsync(input,
mem0DotNetOptions);
await memoryService.CreateMemoryAsync(input);
}).WithDescription("创建记忆").WithDisplayName("创建记忆").WithTags("记忆").WithName("CreateMemory");

memoryService.MapPost("memory_tool", async (MemoryService memoryService,
CreateMemoryToolInput input,
Mem0DotNetOptions mem0DotNetOptions) =>
CreateMemoryToolInput input) =>
{
await memoryService.CreateMemoryToolAsync(input,
mem0DotNetOptions);
await memoryService.CreateMemoryToolAsync(input);
}).WithDescription("创建记忆工具").WithDisplayName("创建记忆工具").WithTags("记忆");

memoryService.MapGet("history/{memoryId}",
async (MemoryService memoryService, string memoryId) => await memoryService.GetHistory(memoryId))
.WithDescription("获取历史").WithDisplayName("获取历史").WithTags("记忆");

memoryService.MapGet("memory/{memoryId}", async (MemoryService memoryService, string memoryId,
Mem0DotNetOptions options) =>
await memoryService.GetMemory(memoryId, options))
memoryService.MapGet("memory/{memoryId}", async (MemoryService memoryService, string memoryId) =>
await memoryService.GetMemory(memoryId))
.WithDescription("获取记忆")
.WithDisplayName("获取记忆")
.WithTags("记忆");

memoryService.MapGet("memory", async (MemoryService memoryService,
Mem0DotNetOptions options,
string? userId,
string? agentId, string? runId, uint limit) =>
await memoryService.GetMemoryAll(options, userId, agentId, runId, limit))
await memoryService.GetMemoryAll(userId, agentId, runId, limit))
.WithDescription("获取所有记忆")
.WithDisplayName("获取所有记忆")
.WithTags("记忆");

memoryService.MapGet("search", async (MemoryService memoryService,
Mem0DotNetOptions options,
string query,
string? userId,
string? agentId, string? runId, uint limit) =>
await memoryService.SearchMemory(options, query,
await memoryService.SearchMemory(query,
userId,
agentId, runId, limit))
.WithDescription("搜索记忆")
.WithDisplayName("搜索记忆")
.WithTags("记忆");

memoryService.MapPut("memory", async (MemoryService memoryService, UpdateMemoryInput input,
MemoryTool memoryTool) => await memoryService.Update(input, memoryTool))
memoryService.MapPut("memory",
async (MemoryService memoryService, UpdateMemoryInput input) => await memoryService.Update(input))
.WithDescription("更新记忆")
.WithDisplayName("更新记忆")
.WithTags("记忆");

memoryService.MapDelete("memory/{memoryId}", async (MemoryService memoryService, string memoryId,
MemoryTool memoryTool) => await memoryService.Delete(memoryId, memoryTool))
memoryService.MapDelete("memory/{memoryId}",
async (MemoryService memoryService, string memoryId) => await memoryService.Delete(memoryId))
.WithDescription("删除记忆")
.WithDisplayName("删除记忆")
.WithTags("记忆");

memoryService.MapDelete("memory", async (MemoryService memoryService, string? userId,
string? agentId, string? runId,
Mem0DotNetOptions options, MemoryTool memoryTool) =>
await memoryService.DeleteAll(userId, agentId, runId, options, memoryTool))
string? agentId, string? runId) =>
await memoryService.DeleteAll(userId, agentId, runId))
.WithDescription("删除所有记忆")
.WithDisplayName("删除所有记忆")
.WithTags("记忆");

memoryService.MapDelete("reset", async (MemoryService memoryService, IVectorStoreService vectorStoreService,
Mem0DotNetOptions mem0DotNetOptions, IHistoryService historyService) =>
await memoryService.Reset(vectorStoreService, mem0DotNetOptions, historyService))
memoryService.MapDelete("reset", async (MemoryService memoryService) =>
await memoryService.Reset())
.WithDescription("重置记忆")
.WithDisplayName("重置记忆")
.WithTags("记忆");
Expand Down
13 changes: 13 additions & 0 deletions src/mem0.NET.Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,18 @@
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": "Host=localhost;Port=5432;Database=mem0;Username=token;Password=dd666666"
},
"Mem0": {
"OpenAIEndpoint": "https://api.token-ai.cn",
"OpenAIKey": "",
"OpenAIChatCompletionModel": "gpt-4o-mini",
"OpenAITextEmbeddingModel": "text-embedding-ada-002",
"CollectionName": "mem0"
},
"Qdrant": {
"Host": "127.0.0.1",
"Port": 6334,
"Https": false,
"ApiKey": "dd666666"
}
}
2 changes: 0 additions & 2 deletions src/mem0.NET.Service/mem0.NET.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\mem0.Core\mem0.Core.csproj"/>
<ProjectReference Include="..\mem0.NET.EntityFramework\mem0.NET.EntityFramework.csproj"/>
<ProjectReference Include="..\mem0.NET.Qdrant\mem0.NET.Qdrant.csproj"/>
<ProjectReference Include="..\mem0.NET\mem0.NET.csproj"/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace mem0.Core;

public class Mem0Context
public class ApplicationContext
{
/// <summary>
/// 用于存储当前上下文的对象
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3bf7bcb

Please sign in to comment.