Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/Examples.SimpleConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MaIN.Core;
using MaIN.Core.Hub;
using MaIN.Domain.Entities;

MaINBootstrapper.Initialize();

Expand All @@ -8,3 +9,5 @@ await AIHub.Chat()
.WithMessage("Hello, World!")
.CompleteAsync(interactive: true);



2 changes: 1 addition & 1 deletion Examples/Examples/Chat/ChatWithFilesExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task Start()
List<string> files = ["./Files/Nicolaus_Copernicus.pdf", "./Files/Galileo_Galilei.pdf"];

var result = await AIHub.Chat()
.WithModel("gemma2:2b")
.WithModel("gemma3:4b")
.WithMessage("You have 2 documents in memory. Whats the difference of work between Galileo and Copernicus?. Give answer based on the documents.")
.WithFiles(files)
.CompleteAsync();
Expand Down
2 changes: 1 addition & 1 deletion Examples/Examples/Chat/ChatWithFilesFromStreamExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Start()
}

var result = await AIHub.Chat()
.WithModel("gemma2:2b")
.WithModel("qwen2.5:0.5b")
.WithMessage("You have 2 documents in memory. Whats the difference of work between Galileo and Copernicus?. Give answer based on the documents.")
.WithFiles(fileStreams)
.CompleteAsync();
Expand Down
3 changes: 3 additions & 0 deletions Releases/0.1.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 0.1.5 release

- Enable pre processing of documents, it can greatly improve KM performance on small models
2 changes: 1 addition & 1 deletion src/MaIN.Core/.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>MaIN.NET</id>
<version>0.1.4</version>
<version>0.1.5</version>
<authors>Wisedev</authors>
<owners>Wisedev</owners>
<icon>favicon.png</icon>
Expand Down
18 changes: 14 additions & 4 deletions src/MaIN.Core/Hub/Contexts/ChatContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MaIN.Domain.Entities;
using MaIN.Domain.Models;
using MaIN.Services.Constants;
using MaIN.Services.Dtos;
using MaIN.Services.Services.Abstract;
using MaIN.Services.Services.Models;
Expand All @@ -10,8 +11,9 @@ namespace MaIN.Core.Hub.Contexts;
public class ChatContext
{
private readonly IChatService _chatService;
private bool _preProcess;
private Chat _chat { get; set; }
private List<FileInfo> _files { get; set; }
private List<FileInfo> _files { get; set; } = [];

internal ChatContext(IChatService chatService)
{
Expand Down Expand Up @@ -84,7 +86,7 @@ public ChatContext WithSystemPrompt(string systemPrompt)
return this;
}

public ChatContext WithFiles(List<FileStream> fileStreams)
public ChatContext WithFiles(List<FileStream> fileStreams, bool preProcess = false)
{
var files = fileStreams.Select(p => new FileInfo()
{
Expand All @@ -94,17 +96,19 @@ public ChatContext WithFiles(List<FileStream> fileStreams)
StreamContent = p
}).ToList();

_preProcess = preProcess;
_files = files;
return this;
}

public ChatContext WithFiles(List<FileInfo> files)
public ChatContext WithFiles(List<FileInfo> files, bool preProcess = false)
{
_files = files;
_preProcess = preProcess;
return this;
}

public ChatContext WithFiles(List<string> filePaths)
public ChatContext WithFiles(List<string> filePaths, bool preProcess = false)
{
var files = filePaths.Select(p => new FileInfo()
{
Expand All @@ -113,6 +117,7 @@ public ChatContext WithFiles(List<string> filePaths)
Extension = Path.GetExtension(p)
}).ToList();

_preProcess = preProcess;
_files = files;
return this;
}
Expand All @@ -135,6 +140,11 @@ public async Task<ChatResult> CompleteAsync(
throw new InvalidOperationException("Chat has no messages."); //TODO good candidate for domain exception
}
_chat.Messages.Last().Files = _files;
if(_preProcess)
{
_chat.Messages.Last().Properties.Add(ServiceConstants.Messages.PreProcessProperty, string.Empty);
}

if (!await ChatExists(_chat.Id))
{
await _chatService.Create(_chat);
Expand Down
6 changes: 3 additions & 3 deletions src/MaIN.Core/MaIN.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

<ItemGroup>
<PackageReference Include="GTranslate" Version="2.2.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
<PackageReference Include="HtmlAgilityPack" Version="1.12.1" />
<PackageReference Include="LLamaSharp" Version="0.23.0" />
<PackageReference Include="LLamaSharp.Backend.Cuda12" Version="0.23.0" />
<PackageReference Include="LLamaSharp.kernel-memory" Version="0.23.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
<PackageReference Include="Microsoft.KernelMemory" Version="0.96.250120.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.3" />
<PackageReference Include="Microsoft.KernelMemory" Version="0.98.250324.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.30.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.30.0-alpha" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class AgentFileSourceDetails : AgentSourceDetailsBase, IAgentSource
{
public required string Path { get; init; }
public required string Name { get; init; }
public bool PreProcess { get; init; } = false;
}
1 change: 1 addition & 0 deletions src/MaIN.Services/Constants/ServiceConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class ApiUrls
public static class Messages
{
public const string GeneratedImageContent = "Generated Image:";
public const string PreProcessProperty = "Pre_Process";
}

public static class Defaults
Expand Down
8 changes: 3 additions & 5 deletions src/MaIN.Services/MaIN.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

<ItemGroup>
<PackageReference Include="GTranslate" Version="2.2.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
<PackageReference Include="HtmlAgilityPack" Version="1.12.1" />
<PackageReference Include="LLamaSharp" Version="0.23.0" />
<PackageReference Include="LLamaSharp.Backend.Cuda12" Version="0.23.0" />
<PackageReference Include="LLamaSharp.kernel-memory" Version="0.23.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
<PackageReference Include="Microsoft.KernelMemory" Version="0.96.250120.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.30.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.30.0-alpha" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.3" />
<PackageReference Include="Microsoft.KernelMemory" Version="0.98.250324.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="Tesseract" Version="5.2.0" />
<PackageReference Include="Tesseract.Data.English" Version="4.0.0" />
Expand Down
11 changes: 11 additions & 0 deletions src/MaIN.Services/Services/LLMService/ChatMemoryOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MaIN.Services.Services.LLMService;

public class ChatMemoryOptions
{
public Dictionary<string, string>? TextData { get; set; }
public Dictionary<string, string>? FileData { get; set; }
public Dictionary<string, FileStream>? StreamData { get; set; }
public List<string>? WebUrls { get; set; }
public List<string>? Memory { get; set; }
public bool PreProcess { get; set; } = false;
}
7 changes: 6 additions & 1 deletion src/MaIN.Services/Services/LLMService/LLMService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public Task CleanSessionCache(string? id)
CancellationToken cancellationToken = default)
{
var model = KnownModels.GetModel(chat.Model);
var parameters = new ModelParams(Path.Combine(modelsPath, model.FileName));
var parameters = new ModelParams(Path.Combine(modelsPath, model.FileName))
{
GpuLayerCount = chat.MemoryParams.GpuLayerCount,
ContextSize = (uint)chat.MemoryParams.ContextSize,
Embeddings = true
};
var llmModel = await LLamaWeights.LoadFromFileAsync(parameters, cancellationToken);
var kernelMemory = memoryFactory.CreateMemoryWithModel(
modelsPath,
Expand Down
Loading