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;

0 commit comments

Comments
 (0)