Skip to content

Commit f3921c6

Browse files
Merge pull request #47 from wisedev-code/feat/perf_improvements_and_beyond
Feat/perf improvements and beyond
2 parents 9a75771 + 180f91a commit f3921c6

30 files changed

+403
-149
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using MaIN.Core.Hub;
2+
3+
namespace Examples.Agents;
4+
5+
public class AgentConversationExample : IExample
6+
{
7+
private static readonly ConsoleColor UserColor = ConsoleColor.Magenta;
8+
private static readonly ConsoleColor AgentColor = ConsoleColor.Green;
9+
private static readonly ConsoleColor SystemColor = ConsoleColor.Yellow;
10+
11+
public async Task Start()
12+
{
13+
PrintColored("Agent conversation example is running!", SystemColor);
14+
15+
PrintColored("Enter agent name: ", SystemColor, false);
16+
var agentName = Console.ReadLine();
17+
18+
PrintColored("Enter agent profile (example: 'Gentle and helpful assistant'): ", SystemColor, false);
19+
var agentProfile = Console.ReadLine();
20+
21+
PrintColored("Enter LLM model (ex: gemma3:4b, llama3.2:3b, yi:6b): ", SystemColor, false);
22+
var model = Console.ReadLine()!;
23+
var systemPrompt =
24+
$"""
25+
Your name is: {agentName}
26+
You are: {agentProfile}
27+
Always stay in your role.
28+
""";
29+
30+
PrintColored($"Creating agent '{agentName}' with profile: '{agentProfile}' using model: '{model}'", SystemColor);
31+
AIHub.Extensions.DisableLLamaLogs();
32+
AIHub.Extensions.DisableNotificationsLogs();
33+
var context = await AIHub.Agent()
34+
.WithModel(model)
35+
.WithInitialPrompt(systemPrompt)
36+
.CreateAsync(interactiveResponse: true);
37+
38+
bool conversationActive = true;
39+
while (conversationActive)
40+
{
41+
PrintColored("You > ", UserColor, false);
42+
string userMessage = Console.ReadLine()!;
43+
44+
if (userMessage.ToLower() == "exit" || userMessage.ToLower() == "quit")
45+
{
46+
conversationActive = false;
47+
continue;
48+
}
49+
50+
PrintColored($"{agentName} > ", AgentColor, false);
51+
await context.ProcessAsync(userMessage);
52+
53+
Console.WriteLine();
54+
}
55+
56+
PrintColored("Conversation ended. Goodbye!", SystemColor);
57+
}
58+
59+
private static void PrintColored(string message, ConsoleColor color, bool newLine = true)
60+
{
61+
Console.ForegroundColor = color;
62+
if (newLine)
63+
{
64+
Console.WriteLine(message);
65+
}
66+
else
67+
{
68+
Console.Write(message);
69+
}
70+
Console.ResetColor();
71+
}
72+
}

Examples/Examples/Agents/AgentWithApiDataSourceExample.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ public async Task Start()
2929
var result = await context
3030
.ProcessAsync("I am looking for work as javascript developer");
3131
Console.WriteLine(result.Message.Content);
32-
3332
}
3433
}

Examples/Examples/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static void RegisterExamples(IServiceCollection services)
4848
services.AddTransient<ChatFromExistingExample>();
4949
services.AddTransient<ChatWithReasoningExample>();
5050
services.AddTransient<AgentExample>();
51+
services.AddTransient<AgentConversationExample>();
5152
services.AddTransient<AgentWithRedirectExample>();
5253
services.AddTransient<MultiBackendAgentWithRedirectExample>();
5354
services.AddTransient<AgentWithRedirectImageExample>();
@@ -130,6 +131,7 @@ public class ExampleRegistry(IServiceProvider serviceProvider)
130131
("\u25a0 Chat from Existing", _serviceProvider.GetRequiredService<ChatFromExistingExample>()),
131132
("\u25a0 Chat with reasoning", _serviceProvider.GetRequiredService<ChatWithReasoningExample>()),
132133
("\u25a0 Basic Agent", _serviceProvider.GetRequiredService<AgentExample>()),
134+
("\u25a0 Conversation Agent", _serviceProvider.GetRequiredService<AgentConversationExample>()),
133135
("\u25a0 Agent with Redirect", _serviceProvider.GetRequiredService<AgentWithRedirectExample>()),
134136
("\u25a0 Agent with Redirect (Multi backends)", _serviceProvider.GetRequiredService<MultiBackendAgentWithRedirectExample>()),
135137
("\u25a0 Agent with Redirect Image", _serviceProvider.GetRequiredService<AgentWithRedirectImageExample>()),
@@ -141,7 +143,6 @@ public class ExampleRegistry(IServiceProvider serviceProvider)
141143
("\u25a0 OpenAi Chat", _serviceProvider.GetRequiredService<ChatExampleOpenAi>()),
142144
("\u25a0 OpenAi Chat with image", _serviceProvider.GetRequiredService<ChatWithImageGenOpenAiExample>()),
143145
("\u25a0 OpenAi Agent with Web Data Source", _serviceProvider.GetRequiredService<AgentWithWebDataSourceOpenAiExample>())
144-
145146
};
146147
}
147148
}

Releases/0.1.9.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 0.1.9 release
2+
3+
**Perf improvements**
4+
- (LLamaSharp) migrate from ChatSession to Conversation and BatchExecutor
5+
- Fixes for KernelMemory, now memory is properly disposed
6+
- New example (conversation agent)
7+
- CLI release (big perf improvement for infer)
8+
- Allow to enable or disable cache for model weights
9+
- Allow to disable buildin llama.cpp logs and MaIN notification
10+
11+
New version of CLI can be downloaded from this page:
12+
https://maindoc.link/#/doc/cli
13+
14+
*described changes are documented*

src/MaIN.Core.UnitTests/AgentContextTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public async Task CreateAsync_ShouldCallAgentServiceCreateAgent()
133133
It.IsAny<bool>(),
134134
It.IsAny<bool>(),
135135
It.IsAny<InferenceParams>(),
136-
It.IsAny<MemoryParams>()))
136+
It.IsAny<MemoryParams>(),
137+
It.IsAny<bool>()))
137138
.ReturnsAsync(agent);
138139

139140
// Act
@@ -146,7 +147,8 @@ public async Task CreateAsync_ShouldCallAgentServiceCreateAgent()
146147
It.Is<bool>(f => f == true),
147148
It.Is<bool>(r => r == false),
148149
It.IsAny<InferenceParams>(),
149-
It.IsAny<MemoryParams>()),
150+
It.IsAny<MemoryParams>(),
151+
It.IsAny<bool>()),
150152
Times.Once);
151153
Assert.Equal(_agentContext, result);
152154
}

src/MaIN.Core/.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>MaIN.NET</id>
5-
<version>0.1.8</version>
5+
<version>0.1.9</version>
66
<authors>Wisedev</authors>
77
<owners>Wisedev</owners>
88
<icon>favicon.png</icon>

src/MaIN.Core/Hub/AiHub.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using LLama.Native;
12
using MaIN.Core.Hub.Contexts;
23
using MaIN.Core.Interfaces;
4+
using MaIN.Services.Services.Abstract;
35

46
namespace MaIN.Core.Hub;
57

@@ -17,8 +19,21 @@ internal static void Initialize(IAIHubServices services)
1719
throw new InvalidOperationException(
1820
"AIHub has not been initialized. Make sure to call AddAIHub() in your service configuration.");
1921

20-
public static ChatContext Chat() => new ChatContext(Services.ChatService);
21-
public static AgentContext Agent() => new AgentContext(Services.AgentService);
22-
public static FlowContext Flow() => new FlowContext(Services.FlowService, Services.AgentService);
22+
public static ChatContext Chat() => new(Services.ChatService);
23+
public static AgentContext Agent() => new(Services.AgentService);
24+
public static FlowContext Flow() => new(Services.FlowService, Services.AgentService);
25+
26+
public abstract class Extensions
27+
{
28+
public static void DisableLLamaLogs()
29+
{
30+
NativeLogConfig.llama_log_set((_,_) => {});
31+
}
32+
33+
public static void DisableNotificationsLogs()
34+
{
35+
INotificationService.Disable = true;
36+
}
37+
}
2338
}
2439

src/MaIN.Core/Hub/Contexts/AgentContext.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AgentContext
1313
private readonly IAgentService _agentService;
1414
private InferenceParams? _inferenceParams;
1515
private MemoryParams? _memoryParams;
16+
private bool _disableCache;
1617
private Agent _agent;
1718

1819
internal AgentContext(IAgentService agentService)
@@ -57,6 +58,12 @@ public AgentContext WithOrder(int order)
5758
_agent.Order = order;
5859
return this;
5960
}
61+
62+
public AgentContext DisableCache()
63+
{
64+
_disableCache = true;
65+
return this;
66+
}
6067
public AgentContext WithSource(IAgentSource source, AgentSourceType type)
6168
{
6269
_agent.Context.Source = new AgentSource()
@@ -127,13 +134,13 @@ public AgentContext WithBehaviour(string name, string instruction)
127134

128135
public async Task<AgentContext> CreateAsync(bool flow = false, bool interactiveResponse = false)
129136
{
130-
await _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams);
137+
await _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams, _disableCache);
131138
return this;
132139
}
133140

134141
public AgentContext Create(bool flow = false, bool interactiveResponse = false)
135142
{
136-
_ = _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams).Result;
143+
_ = _agentService.CreateAgent(_agent, flow, interactiveResponse, _inferenceParams, _memoryParams, _disableCache).Result;
137144
return this;
138145
}
139146

src/MaIN.Core/Hub/Contexts/ChatContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MaIN.Domain.Configuration;
22
using MaIN.Domain.Entities;
33
using MaIN.Domain.Models;
4+
using MaIN.Services;
45
using MaIN.Services.Constants;
56
using MaIN.Services.Dtos;
67
using MaIN.Services.Services.Abstract;
@@ -134,6 +135,12 @@ public ChatContext EnableVisual()
134135
_chat.Visual = true;
135136
return this;
136137
}
138+
139+
public ChatContext DisableCache()
140+
{
141+
_chat.Properties.AddProperty(ServiceConstants.Properties.DisableCacheProperty);
142+
return this;
143+
}
137144

138145
public string GetChatId() => _chat.Id;
139146

@@ -149,7 +156,7 @@ public async Task<ChatResult> CompleteAsync(
149156
_chat.Messages.Last().Files = _files;
150157
if(_preProcess)
151158
{
152-
_chat.Messages.Last().Properties.Add(ServiceConstants.Messages.PreProcessProperty, string.Empty);
159+
_chat.Messages.Last().Properties.AddProperty(ServiceConstants.Properties.PreProcessProperty);
153160
}
154161

155162
if (!await ChatExists(_chat.Id))

src/MaIN.Domain/Entities/Chat.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using LLama.Batched;
12
using MaIN.Domain.Configuration;
23

34
namespace MaIN.Domain.Entities;
@@ -15,6 +16,7 @@ public class Chat
1516
public Dictionary<string, string> Properties { get; init; } = [];
1617
public List<string> Memory { get; } = [];
1718
public BackendType? Backend { get; set; }
19+
public Conversation.State? ConversationState { get; set; }
1820

1921
public bool Interactive = false;
2022
public bool Translate = false;

src/MaIN.Domain/MaIN.Domain.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="LLamaSharp" Version="0.23.0" />
12+
</ItemGroup>
13+
1014
</Project>

src/MaIN.Infrastructure/Configuration/SqlExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ [Type] NVARCHAR(MAX) NOT NULL, -- Stored as JSON
4949
[Properties] NVARCHAR(MAX) NULL, -- Stored as JSON
5050
[Visual] BIT NOT NULL DEFAULT 0,
5151
[BackendType] INT NOT NULL DEFAULT 0,
52+
[ConvState] NVARCHAR(MAX) NULL, -- Stored as JSON,
5253
[InferenceParams] NVARCHAR(MAX) NULL, -- Stored as JSON,
5354
[MemoryParams] NVARCHAR(MAX) NULL, -- Stored as JSON
5455
[Interactive] BIT NOT NULL DEFAULT 0

src/MaIN.Infrastructure/Configuration/SqliteExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ CREATE TABLE IF NOT EXISTS Chats (
4747
Properties TEXT, -- Stored as JSON
4848
Visual INTEGER NOT NULL DEFAULT 0,
4949
BackendType INTEGER NOT NULL DEFAULT 0,
50+
ConvState TEXT, -- Stored as JSON
5051
InferenceParams TEXT, -- Stored as JSON
5152
MemoryParams TEXT, -- Stored as JSON
5253
Interactive INTEGER NOT NULL DEFAULT 0

src/MaIN.Infrastructure/MaIN.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0-preview.5.24306.7" />
1515
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
16-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
16+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.3" />
1717
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0-preview.5.24306.7" />
1818
<PackageReference Include="MongoDB.Driver" Version="3.2.1" />
1919
<PackageReference Include="MongoDB.Driver.Core" Version="2.30.0" />

src/MaIN.Infrastructure/Models/ChatDocument.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ public class ChatDocument
1818
public bool Translate { get; init; }
1919
public InferenceParamsDocument? InferenceParams { get; init; }
2020
public MemoryParamsDocument? MemoryParams { get; init; }
21+
public object? ConvState { get; init; }
2122
}

src/MaIN.Infrastructure/Repositories/Sql/SqlChatRepository.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ private ChatDocument MapChatDocument(dynamic row)
2727
Type = row.Type != null ?
2828
JsonSerializer.Deserialize<ChatTypeDocument>(row.Type.ToString(), _jsonOptions) :
2929
default,
30+
ConvState = row.ConvState != null ?
31+
JsonSerializer.Deserialize<dynamic>(row.ConvState.ToString(), _jsonOptions) :
32+
default,
3033
InferenceParams = row.InferenceParams != null ?
3134
JsonSerializer.Deserialize<InferenceParamsDocument>(row.InferenceParams.ToString(), _jsonOptions) :
3235
default,
@@ -55,6 +58,7 @@ private object MapChatToParameters(ChatDocument chat)
5558
chat.Model,
5659
Messages = JsonSerializer.Serialize(chat.Messages, _jsonOptions),
5760
Type = JsonSerializer.Serialize(chat.Type, _jsonOptions),
61+
ConvState = JsonSerializer.Serialize(chat.ConvState, _jsonOptions),
5862
InferenceParams = JsonSerializer.Serialize(chat.InferenceParams, _jsonOptions),
5963
MemoryParams = JsonSerializer.Serialize(chat.MemoryParams, _jsonOptions),
6064
Properties = JsonSerializer.Serialize(chat.Properties, _jsonOptions),
@@ -87,10 +91,10 @@ public async Task AddChat(ChatDocument chat)
8791
await connection.ExecuteAsync(@"
8892
INSERT INTO Chats (
8993
Id, Name, Model, Messages, Type, Properties,
90-
Stream, Visual, InferenceParams, MemoryParams, Interactive
94+
Stream, Visual, ConvState, InferenceParams, MemoryParams, Interactive
9195
) VALUES (
9296
@Id, @Name, @Model, @Messages, @Type, @Properties,
93-
@Visual, @InferenceParams, @MemoryParams, @Interactive)",
97+
@Visual, @ConvState, @InferenceParams, @MemoryParams, @Interactive)",
9498
parameters);
9599
}
96100

@@ -106,6 +110,7 @@ UPDATE Chats
106110
Messages = @Messages,
107111
Type = @Type,
108112
Properties = @Properties,
113+
ConvState = @ConvState,
109114
Visual = @Visual
110115
WHERE Id = @Id",
111116
parameters);

src/MaIN.Infrastructure/Repositories/Sqlite/SqliteChatRepository.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ private ChatDocument MapChatDocument(dynamic row)
2525
Type = row.Type != null ?
2626
JsonSerializer.Deserialize<ChatTypeDocument>(row.Type, _jsonOptions) :
2727
default,
28+
ConvState = row.Type != null ?
29+
JsonSerializer.Deserialize<dynamic>(row.ConvState, _jsonOptions) :
30+
default,
2831
InferenceParams = row.Type != null ?
2932
JsonSerializer.Deserialize<InferenceParamsDocument>(row.InferenceParams, _jsonOptions) :
3033
default,
@@ -49,6 +52,7 @@ private object MapChatToParameters(ChatDocument chat)
4952
chat.Name,
5053
chat.Model,
5154
Messages = JsonSerializer.Serialize(chat.Messages, _jsonOptions),
55+
ConvState = JsonSerializer.Serialize(chat.ConvState, _jsonOptions),
5256
InferenceParams = JsonSerializer.Serialize(chat.InferenceParams, _jsonOptions),
5357
MemoryParams = JsonSerializer.Serialize(chat.MemoryParams, _jsonOptions),
5458
Type = JsonSerializer.Serialize(chat.Type, _jsonOptions),
@@ -78,8 +82,8 @@ public async Task AddChat(ChatDocument chat)
7882
{
7983
var parameters = MapChatToParameters(chat);
8084
await connection.ExecuteAsync(@"
81-
INSERT INTO Chats (Id, Name, Model, Messages, [Type], Properties, Visual, InferenceParams, MemoryParams, Interactive) VALUES (@Id, @Name, @Model, @Messages, @Type, @Properties,
82-
@Visual, @InferenceParams, @MemoryParams, @Interactive)", parameters);
85+
INSERT INTO Chats (Id, Name, Model, Messages, [Type], Properties, Visual, ConvState, InferenceParams, MemoryParams, Interactive) VALUES (@Id, @Name, @Model, @Messages, @Type, @Properties,
86+
@Visual, @ConvState, @InferenceParams, @MemoryParams, @Interactive)", parameters);
8387
}
8488

8589
public async Task UpdateChat(string id, ChatDocument chat)
@@ -92,6 +96,7 @@ UPDATE Chats
9296
Messages = @Messages,
9397
Type = @Type,
9498
Properties = @Properties,
99+
ConvState = @ConvState,
95100
Visual = @Visual
96101
WHERE Id = @Id", parameters);
97102
}

src/MaIN.Services/Constants/ServiceConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ public static class ApiUrls
2020
public static class Messages
2121
{
2222
public const string GeneratedImageContent = "Generated Image:";
23+
}
24+
25+
public static class Properties
26+
{
2327
public const string PreProcessProperty = "Pre_Process";
28+
public const string DisableCacheProperty = "DisableCache";
2429
}
2530

2631
public static class Defaults

0 commit comments

Comments
 (0)