Skip to content

Commit af92205

Browse files
committed
refactor: apply sugestions from comments in PR Refactor/model configuration #119
- Make TTS models implement ITTSModel - Fix and add tests - Cleanup
1 parent c1f0045 commit af92205

25 files changed

Lines changed: 230 additions & 280 deletions

Examples/Examples.SimpleConsole/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using MaIN.Core;
22
using MaIN.Core.Hub;
3-
using MaIN.Domain.Models.Abstract;
43
using MaIN.Domain.Models.Concrete;
54

65
MaINBootstrapper.Initialize();

Examples/Examples/Chat/ChatExampleGemini.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ await AIHub.Chat()
2727
.WithMessage("Is the killer whale the smartest animal?")
2828
.CompleteAsync(interactive: true);
2929
}
30-
}
30+
}

Examples/Examples/Chat/ChatWithTextToSpeechExample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task Start()
2222
await AIHub.Chat()
2323
.WithModel<Gemma2_2b>()
2424
.WithMessage("Generate a 4 sentence poem.")
25-
.Speak(new TextToSpeechParams("kokoro:82m", voice, true))
25+
.Speak(new TextToSpeechParams(new Kokoro_82m(), voice, true))
2626
.CompleteAsync(interactive: true);
2727

2828
Console.WriteLine("Done!");

src/MaIN.Core.UnitTests/ChatContextTests.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MaIN.Core.Hub.Contexts;
22
using MaIN.Domain.Entities;
3+
using MaIN.Domain.Models.Abstract;
34
using MaIN.Services.Services.Abstract;
45
using MaIN.Services.Services.Models;
56
using Moq;
@@ -88,6 +89,7 @@ public async Task CompleteAsync_ShouldCallChatService()
8889
.ReturnsAsync(chatResult);
8990

9091
_chatContext.WithMessage("User message");
92+
_chatContext.WithModel(new GenericLocalModel("test-model"));
9193

9294
// Act
9395
var result = await _chatContext.CompleteAsync();
@@ -110,4 +112,19 @@ public async Task GetCurrentChat_ShouldCallChatService()
110112
// Assert
111113
Assert.Equal(chat, result);
112114
}
113-
}
115+
116+
[Fact]
117+
public async Task WithModel_ShouldSetModelIdAndInstance()
118+
{
119+
// Arrange
120+
var model = new GenericLocalModel("default");
121+
122+
// Act
123+
await _chatContext.WithModel(model)
124+
.WithMessage("User message")
125+
.CompleteAsync();
126+
127+
// Assert
128+
_mockChatService.Verify(s => s.Completions(It.Is<Chat>(c => c.ModelId == "default" && c.ModelInstance == model), It.IsAny<bool>(), It.IsAny<bool>(), null), Times.Once);
129+
}
130+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using MaIN.Domain.Entities;
44
using MaIN.Domain.Entities.Tools;
55
using MaIN.Domain.Exceptions.Chats;
6+
using MaIN.Domain.Exceptions.Models;
67
using MaIN.Domain.Models;
78
using MaIN.Domain.Models.Abstract;
89
using MaIN.Services;
@@ -53,7 +54,16 @@ public IChatMessageBuilder WithModel(AIModel model)
5354
[Obsolete("Use WithModel(AIModel model) or WithModel<TModel>() instead.")]
5455
public IChatMessageBuilder WithModel(string modelId)
5556
{
56-
var model = ModelRegistry.GetById(modelId);
57+
AIModel model;
58+
try
59+
{
60+
model = ModelRegistry.GetById(modelId);
61+
}
62+
catch (Exception ex)
63+
{
64+
65+
throw new Exception($"Model with ID '{modelId}' not found in registry. Use WithModel(AIModel model) or WithModel<TModel>() instead.", ex);
66+
}
5767
SetModel(model);
5868
return this;
5969
}
@@ -192,7 +202,7 @@ public async Task<ChatResult> CompleteAsync(
192202
{
193203
if (_chat.ModelInstance is null)
194204
{
195-
throw new ChatNotInitializedException();
205+
throw new MissingModelInstanceException();
196206
}
197207
if (_chat.Messages.Count == 0)
198208
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public IModelContext Download(string modelId)
113113
var model = ModelRegistry.GetById(modelId) ?? throw new ModelNotSupportedException(modelId);
114114
if (model is not LocalModel localModel)
115115
{
116-
throw new MissingModelIdException(nameof(LocalModel));
116+
throw new InvalidModelTypeException(nameof(LocalModel));
117117
}
118118

119119
if (localModel.DownloadUrl is null)

src/MaIN.Domain/Entities/Chat.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,25 @@ public class Chat
99
{
1010
public string Id { get; init; } = string.Empty;
1111
public required string Name { get; init; }
12-
public required string ModelId { get; set; }
12+
public required string ModelId
13+
{
14+
get => _modelInstance?.Id ?? string.Empty;
15+
set
16+
{
17+
if (string.IsNullOrEmpty(value))
18+
{
19+
_modelInstance = null;
20+
return;
21+
}
22+
23+
_modelInstance = ModelRegistry.GetById(value);
24+
}
25+
}
1326
private AIModel? _modelInstance;
1427
public AIModel? ModelInstance
1528
{
16-
get => _modelInstance ??= ModelRegistry.GetById(ModelId);
17-
set => (_modelInstance, ModelId) = (value, value?.Id ?? ModelId);
29+
get => _modelInstance;
30+
set => (_modelInstance, ModelId) = (value, value?.Id ?? string.Empty);
1831
}
1932
public List<Message> Messages { get; set; } = [];
2033
public ChatType Type { get; set; } = ChatType.Conversation;
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
namespace MaIN.Domain.Entities;
1+
using MaIN.Domain.Models.Abstract;
22

3-
public class TextToSpeechParams
4-
{
5-
public string Model { get; set; }
6-
public Voice Voice { get; set; }
7-
public bool Playback { get; set; }
3+
namespace MaIN.Domain.Entities;
84

9-
public TextToSpeechParams(string model, Voice voice, bool playback = false)
10-
{
11-
Model = model;
12-
Voice = voice;
13-
Playback = playback;
14-
}
15-
}
5+
public sealed record TextToSpeechParams(AIModel Model, Voice Voice, bool Playback = false);

src/MaIN.Domain/Exceptions/ModelException.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/MaIN.Domain/Exceptions/Models/LocalModels/DownloadUrlNullOrEmptyException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace MaIN.Domain.Exceptions.Models.LocalModels;
55
public class DownloadUrlNullOrEmptyException()
66
: MaINCustomException("Download url cannot be null or empty.")
77
{
8-
public override string PublicErrorMessage => "Download url cannot be null or empty.";
8+
public override string PublicErrorMessage => Message;
99
public override HttpStatusCode HttpStatusCode => HttpStatusCode.BadRequest;
10-
}
10+
}

0 commit comments

Comments
 (0)