Skip to content

Commit a6cc23d

Browse files
authored
refactor: Separate AIModel from Chat (#123)
* Separate AIModel and Chat - remove Backend field from Chat as it is contained in the AIModel * Remove WithModel(AIModel) and WithModel<TModel> WithModel(AIModel) and WithModel<TModel> makes more questions than answers. It gives user one clear workflow. Add this flow with register as an example." * Make use of the ChatId property of the Agent * Use const modelIDs instead of magic strings in the predefined models * Fix AgentService.Restart() * Fix orphaned culumn in databases * Fix copy paste bug in SqliteChatRepository.cs * Fix noninformative error thrown when modelId is not found * Standarize AgentContext like ChatContext - examples use const model ids instead of magic strings - add flux id (the approach in examples will be refactored) * Minor changes to satisfy rebase.
1 parent 69446b1 commit a6cc23d

File tree

74 files changed

+1085
-916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1085
-916
lines changed

Examples/Examples/Agents/AgentConversationExample.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ namespace Examples.Agents;
44

55
public class AgentConversationExample : IExample
66
{
7-
private static readonly ConsoleColor UserColor = ConsoleColor.Magenta;
8-
private static readonly ConsoleColor AgentColor = ConsoleColor.Green;
9-
private static readonly ConsoleColor SystemColor = ConsoleColor.Yellow;
7+
private static readonly ConsoleColor _userColor = ConsoleColor.Magenta;
8+
private static readonly ConsoleColor _agentColor = ConsoleColor.Green;
9+
private static readonly ConsoleColor _systemColor = ConsoleColor.Yellow;
1010

1111
public async Task Start()
1212
{
13-
PrintColored("Agent conversation example is running!", SystemColor);
14-
15-
PrintColored("Enter agent name: ", SystemColor, false);
13+
PrintColored("Agent conversation example is running!", _systemColor);
14+
15+
PrintColored("Enter agent name: ", _systemColor, false);
1616
var agentName = Console.ReadLine();
17-
18-
PrintColored("Enter agent profile (example: 'Gentle and helpful assistant'): ", SystemColor, false);
17+
18+
PrintColored("Enter agent profile (example: 'Gentle and helpful assistant'): ", _systemColor, false);
1919
var agentProfile = Console.ReadLine();
20-
21-
PrintColored("Enter LLM model (ex: gemma3:4b, llama3.2:3b, yi:6b): ", SystemColor, false);
20+
21+
PrintColored("Enter LLM model (ex: gemma3:4b, llama3.2:3b, yi:6b): ", _systemColor, false);
2222
var model = Console.ReadLine()!;
2323
var systemPrompt =
2424
$"""
@@ -27,35 +27,35 @@ public async Task Start()
2727
Always stay in your role.
2828
""";
2929

30-
PrintColored($"Creating agent '{agentName}' with profile: '{agentProfile}' using model: '{model}'", SystemColor);
30+
PrintColored($"Creating agent '{agentName}' with profile: '{agentProfile}' using model: '{model}'", _systemColor);
3131
AIHub.Extensions.DisableLLamaLogs();
3232
AIHub.Extensions.DisableNotificationsLogs();
3333
var context = await AIHub.Agent()
3434
.WithModel(model)
3535
.WithInitialPrompt(systemPrompt)
3636
.CreateAsync(interactiveResponse: true);
37-
37+
3838
bool conversationActive = true;
3939
while (conversationActive)
4040
{
41-
PrintColored("You > ", UserColor, false);
41+
PrintColored("You > ", _userColor, false);
4242
string userMessage = Console.ReadLine()!;
43-
44-
if (userMessage.ToLower() == "exit" || userMessage.ToLower() == "quit")
43+
44+
if (userMessage.ToLower() is "exit" or "quit")
4545
{
4646
conversationActive = false;
4747
continue;
4848
}
49-
50-
PrintColored($"{agentName} > ", AgentColor, false);
49+
50+
PrintColored($"{agentName} > ", _agentColor, false);
5151
await context.ProcessAsync(userMessage);
52-
53-
Console.WriteLine();
52+
53+
Console.WriteLine();
5454
}
55-
56-
PrintColored("Conversation ended. Goodbye!", SystemColor);
55+
56+
PrintColored("Conversation ended. Goodbye!", _systemColor);
5757
}
58-
58+
5959
private static void PrintColored(string message, ConsoleColor color, bool newLine = true)
6060
{
6161
Console.ForegroundColor = color;
@@ -67,6 +67,7 @@ private static void PrintColored(string message, ConsoleColor color, bool newLin
6767
{
6868
Console.Write(message);
6969
}
70+
7071
Console.ResetColor();
7172
}
72-
}
73+
}

Examples/Examples/Agents/AgentExample.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MaIN.Core.Hub;
2+
using MaIN.Domain.Models;
23

34
namespace Examples.Agents;
45

@@ -24,13 +25,13 @@ has the best possible counsel as she seeks to reclaim the Iron Throne.
2425
""";
2526

2627
var context = AIHub.Agent()
27-
.WithModel("llama3.2:3b")
28+
.WithModel(Models.Local.Llama3_2_3b)
2829
.WithInitialPrompt(systemPrompt)
2930
.Create();
30-
31+
3132
var result = await context
3233
.ProcessAsync("Where is the Iron Throne located? I need this information for Lady Princess");
3334

3435
Console.WriteLine(result.Message.Content);
3536
}
36-
}
37+
}

Examples/Examples/Agents/AgentExampleTools.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Examples.Utils;
22
using MaIN.Core.Hub;
33
using MaIN.Core.Hub.Utils;
4+
using MaIN.Domain.Models;
45

56
namespace Examples.Agents;
67

@@ -12,7 +13,7 @@ public async Task Start()
1213
Console.WriteLine("(Anthropic) Tool example is running!");
1314

1415
var context = await AIHub.Agent()
15-
.WithModel("claude-sonnet-4-5-20250929")
16+
.WithModel(Models.Anthropic.ClaudeSonnet4_5)
1617
.WithSteps(StepBuilder.Instance
1718
.Answer()
1819
.Build())

Examples/Examples/Agents/AgentWithApiDataSourceExample.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MaIN.Core.Hub;
22
using MaIN.Core.Hub.Utils;
33
using MaIN.Domain.Entities.Agents.AgentSource;
4+
using MaIN.Domain.Models;
45

56
namespace Examples.Agents;
67

@@ -9,9 +10,9 @@ public class AgentWithApiDataSourceExample : IExample
910
public async Task Start()
1011
{
1112
Console.WriteLine("Agent with api source");
12-
13+
1314
var context = AIHub.Agent()
14-
.WithModel("llama3.2:3b")
15+
.WithModel(Models.Local.Llama3_2_3b)
1516
.WithInitialPrompt("Extract at least 4 jobs offers (try to include title, company name, salary and location if possible)")
1617
.WithSource(new AgentApiSourceDetails()
1718
{
@@ -24,9 +25,9 @@ public async Task Start()
2425
.FetchData()
2526
.Build())
2627
.Create();
27-
28+
2829
var result = await context
2930
.ProcessAsync("I am looking for work as javascript developer");
3031
Console.WriteLine(result.Message.Content);
3132
}
32-
}
33+
}

Examples/Examples/Agents/AgentWithBecomeExample.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MaIN.Core.Hub;
22
using MaIN.Core.Hub.Utils;
33
using MaIN.Domain.Entities.Agents.AgentSource;
4+
using MaIN.Domain.Models;
45

56
namespace Examples.Agents;
67

@@ -9,7 +10,7 @@ public class AgentWithBecomeExample : IExample
910
public async Task Start()
1011
{
1112
var becomeAgent = AIHub.Agent()
12-
.WithModel("llama3.1:8b")
13+
.WithModel(Models.Local.Llama3_1_8b)
1314
.WithInitialPrompt("Extract 5 best books that you can find in your memory")
1415
.WithSource(new AgentFileSourceDetails
1516
{
@@ -41,4 +42,4 @@ public async Task Start()
4142
await becomeAgent
4243
.ProcessAsync("I am looking for good fantasy book to buy");
4344
}
44-
}
45+
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MaIN.Core.Hub;
22
using MaIN.Core.Hub.Utils;
3+
using MaIN.Domain.Models;
34

45
namespace Examples.Agents;
56

@@ -10,16 +11,17 @@ public async Task Start()
1011
Console.WriteLine("Agent with knowledge base example");
1112
AIHub.Extensions.DisableLLamaLogs();
1213
var context = await AIHub.Agent()
13-
.WithModel("gemma3:4b")
14-
.WithInitialPrompt("""
15-
You are a helpful assistant that answers questions about a company. Try to
16-
help employees find answers to their questions. Company you work for is TechVibe Solutions.
17-
""")
14+
.WithModel(Models.Local.Gemma3_4b)
15+
.WithInitialPrompt(
16+
"""
17+
You are a helpful assistant that answers questions about a company. Try to
18+
help employees find answers to their questions. Company you work for is TechVibe Solutions.
19+
""")
1820
.WithKnowledge(KnowledgeBuilder.Instance
19-
.AddFile("people.md", "./Files/Knowledge/people.md",
21+
.AddFile("people.md", "./Files/Knowledge/people.md",
2022
tags: ["workers", "employees", "company"])
2123
.AddFile("organization.md", "./Files/Knowledge/organization.md",
22-
tags:["company structure", "company policy", "company culture", "company overview"])
24+
tags: ["company structure", "company policy", "company culture", "company overview"])
2325
.AddFile("events.md", "./Files/Knowledge/events.md",
2426
tags: ["company events", "company calendar", "company agenda"])
2527
.AddFile("office_layout.md", "./Files/Knowledge/office_layout.md",
@@ -28,10 +30,10 @@ You are a helpful assistant that answers questions about a company. Try to
2830
.AnswerUseKnowledge()
2931
.Build())
3032
.CreateAsync();
31-
32-
var result = await context
33-
.ProcessAsync("Hey! Where I can find some printer paper?");
34-
Console.WriteLine(result.Message.Content);;
33+
34+
var result = await context.ProcessAsync("Hey! Where I can find some printer paper?");
35+
Console.WriteLine(result.Message.Content);
36+
;
3537

3638
}
37-
}
39+
}

Examples/Examples/Agents/AgentWithKnowledgeWebExample.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MaIN.Core.Hub;
22
using MaIN.Core.Hub.Utils;
33
using MaIN.Domain.Entities;
4+
using MaIN.Domain.Models;
45

56
namespace Examples.Agents;
67

@@ -12,14 +13,15 @@ public async Task Start()
1213

1314
AIHub.Extensions.DisableLLamaLogs();
1415
var context = await AIHub.Agent()
15-
.WithModel("llama3.2:3b")
16-
.WithMemoryParams(new MemoryParams(){ContextSize = 4096})
17-
.WithInitialPrompt("""
18-
You are an expert piano instructor specializing in teaching specific pieces,
19-
techniques, and solving common playing problems. Help students learn exact
20-
fingerings, chord progressions, and troubleshoot technical issues with
21-
detailed, step-by-step guidance for both classical and popular music.
22-
""")
16+
.WithModel(Models.Local.Llama3_2_3b)
17+
.WithMemoryParams(new MemoryParams() { ContextSize = 4096 })
18+
.WithInitialPrompt(
19+
"""
20+
You are an expert piano instructor specializing in teaching specific pieces,
21+
techniques, and solving common playing problems. Help students learn exact
22+
fingerings, chord progressions, and troubleshoot technical issues with
23+
detailed, step-by-step guidance for both classical and popular music.
24+
""")
2325
.WithKnowledge(KnowledgeBuilder.Instance
2426
.AddUrl("piano_scales_major", "https://www.pianoscales.org/major.html",
2527
tags: ["scale_fingerings", "c_major_scale", "d_major_scale", "fingering_patterns"])
@@ -43,9 +45,8 @@ public async Task Start()
4345
.Build())
4446
.CreateAsync();
4547

46-
var result = await context
47-
.ProcessAsync("I want to learn the C major scale. What's the exact fingering pattern for both hands?" + "I want short and concrete answer");
48+
var result = await context.ProcessAsync("I want to learn the C major scale. What's the exact fingering pattern for both hands?" + "I want short and concrete answer");
4849

4950
Console.WriteLine(result.Message.Content);
5051
}
51-
}
52+
}

Examples/Examples/Agents/AgentWithWebDataSourceOpenAiExample.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MaIN.Core.Hub;
33
using MaIN.Core.Hub.Utils;
44
using MaIN.Domain.Entities.Agents.AgentSource;
5+
using MaIN.Domain.Models;
56

67
namespace Examples.Agents;
78

@@ -10,11 +11,11 @@ public class AgentWithWebDataSourceOpenAiExample : IExample
1011
public async Task Start()
1112
{
1213
Console.WriteLine("Agent with web source (OpenAi)");
13-
14+
1415
OpenAiExample.Setup(); //We need to provide OpenAi API key
1516

1617
var context = await AIHub.Agent()
17-
.WithModel("gpt-4o-mini")
18+
.WithModel(Models.OpenAi.Gpt4oMini)
1819
.WithInitialPrompt($"Find useful information about daily news, try to include title, description and link.")
1920
.WithBehaviour("Journalist", $"Base on data provided in chat find useful information about what happen today. Build it in form of newsletter - Name of newsletter is MaIN_Letter and today`s date is {DateTime.UtcNow}")
2021
.WithSource(new AgentWebSourceDetails()
@@ -27,9 +28,9 @@ public async Task Start()
2728
.Answer()
2829
.Build())
2930
.CreateAsync(interactiveResponse: true);
30-
31+
3132
await context
3233
.ProcessAsync("Provide today's newsletter");
3334

3435
}
35-
}
36+
}

Examples/Examples/Agents/AgentsTalkingToEachOther.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MaIN.Core.Hub;
22
using MaIN.Core.Hub.Utils;
3+
using MaIN.Domain.Models;
34

45
namespace Examples.Agents;
56

@@ -15,7 +16,7 @@ public async Task Start()
1516
You prioritize kindness, patience, and understanding in every interaction. You speak calmly, using gentle words,
1617
and always try to de-escalate tension with warmth and care."
1718
""";
18-
19+
1920
var systemPromptSecond =
2021
"""
2122
You are intense, blunt, and always on edge. Your tone is sharp, impatient, and confrontational.
@@ -24,28 +25,28 @@ and always try to de-escalate tension with warmth and care."
2425
""";
2526

2627
var idFirst = Guid.NewGuid().ToString();
27-
28+
2829
var contextSecond = AIHub.Agent()
29-
.WithModel("gemma2:2b")
30+
.WithModel(Models.Local.Gemma2_2b)
3031
.WithInitialPrompt(systemPromptSecond)
3132
.WithSteps(StepBuilder.Instance
3233
.Answer()
3334
.Redirect(agentId: idFirst, mode: "USER")
3435
.Build())
3536
.Create(interactiveResponse: true);
36-
37+
3738
var context = AIHub.Agent()
38-
.WithModel("llama3.2:3b")
39+
.WithModel(Models.Local.Llama3_2_3b)
3940
.WithId(idFirst)
4041
.WithInitialPrompt(systemPrompt)
4142
.WithSteps(StepBuilder.Instance
4243
.Answer()
4344
.Redirect(agentId: contextSecond.GetAgentId(), mode: "USER")
4445
.Build())
4546
.Create(interactiveResponse: true);
46-
47+
4748
await context
4849
.ProcessAsync("Introduce yourself, and start conversation!");
4950

5051
}
51-
}
52+
}

0 commit comments

Comments
 (0)