|
| 1 | +using MaIN.Core.Hub; |
| 2 | +using MaIN.Domain.Configuration; |
| 3 | +using MaIN.Domain.Entities; |
| 4 | +using MaIN.Domain.Configuration.BackendInferenceParams; |
| 5 | +using MaIN.Domain.Exceptions; |
| 6 | +using MaIN.Domain.Models; |
| 7 | +using MaIN.Domain.Models.Concrete; |
| 8 | + |
| 9 | +namespace MaIN.Core.IntegrationTests; |
| 10 | + |
| 11 | +public class BackendParamsTests : IntegrationTestBase |
| 12 | +{ |
| 13 | + private const string TestQuestion = "What is 2+2? Answer with just the number."; |
| 14 | + |
| 15 | + [SkippableFact] |
| 16 | + public async Task OpenAi_Should_RespondWithParams() |
| 17 | + { |
| 18 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.OpenAi)?.ApiKeyEnvName!); |
| 19 | + |
| 20 | + var result = await AIHub.Chat() |
| 21 | + .WithModel(Models.OpenAi.Gpt4oMini) |
| 22 | + .WithMessage(TestQuestion) |
| 23 | + .WithInferenceParams(new OpenAiInferenceParams |
| 24 | + { |
| 25 | + Temperature = 0.3f, |
| 26 | + MaxTokens = 100, |
| 27 | + TopP = 0.9f |
| 28 | + }) |
| 29 | + .CompleteAsync(); |
| 30 | + |
| 31 | + Assert.True(result.Done); |
| 32 | + Assert.NotNull(result.Message); |
| 33 | + Assert.NotEmpty(result.Message.Content); |
| 34 | + Assert.Contains("4", result.Message.Content); |
| 35 | + } |
| 36 | + |
| 37 | + [SkippableFact] |
| 38 | + public async Task Anthropic_Should_RespondWithParams() |
| 39 | + { |
| 40 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.Anthropic)?.ApiKeyEnvName!); |
| 41 | + |
| 42 | + var result = await AIHub.Chat() |
| 43 | + .WithModel(Models.Anthropic.ClaudeSonnet4) |
| 44 | + .WithMessage(TestQuestion) |
| 45 | + .WithInferenceParams(new AnthropicInferenceParams |
| 46 | + { |
| 47 | + Temperature = 0.3f, |
| 48 | + MaxTokens = 100, |
| 49 | + TopP = 0.9f |
| 50 | + }) |
| 51 | + .CompleteAsync(); |
| 52 | + |
| 53 | + Assert.True(result.Done); |
| 54 | + Assert.NotNull(result.Message); |
| 55 | + Assert.NotEmpty(result.Message.Content); |
| 56 | + Assert.Contains("4", result.Message.Content); |
| 57 | + } |
| 58 | + |
| 59 | + [SkippableFact] |
| 60 | + public async Task Gemini_Should_RespondWithParams() |
| 61 | + { |
| 62 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.Gemini)?.ApiKeyEnvName!); |
| 63 | + |
| 64 | + var result = await AIHub.Chat() |
| 65 | + .WithModel(Models.Gemini.Gemini2_0Flash) |
| 66 | + .WithMessage(TestQuestion) |
| 67 | + .WithInferenceParams(new GeminiInferenceParams |
| 68 | + { |
| 69 | + Temperature = 0.3f, |
| 70 | + MaxTokens = 100, |
| 71 | + TopP = 0.9f |
| 72 | + }) |
| 73 | + .CompleteAsync(); |
| 74 | + |
| 75 | + Assert.True(result.Done); |
| 76 | + Assert.NotNull(result.Message); |
| 77 | + Assert.NotEmpty(result.Message.Content); |
| 78 | + Assert.Contains("4", result.Message.Content); |
| 79 | + } |
| 80 | + |
| 81 | + [SkippableFact] |
| 82 | + public async Task DeepSeek_Should_RespondWithParams() |
| 83 | + { |
| 84 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.DeepSeek)?.ApiKeyEnvName!); |
| 85 | + |
| 86 | + var result = await AIHub.Chat() |
| 87 | + .WithModel(Models.DeepSeek.Reasoner) |
| 88 | + .WithMessage(TestQuestion) |
| 89 | + .WithInferenceParams(new DeepSeekInferenceParams |
| 90 | + { |
| 91 | + Temperature = 0.3f, |
| 92 | + MaxTokens = 100, |
| 93 | + TopP = 0.9f |
| 94 | + }) |
| 95 | + .CompleteAsync(); |
| 96 | + |
| 97 | + Assert.True(result.Done); |
| 98 | + Assert.NotNull(result.Message); |
| 99 | + Assert.NotEmpty(result.Message.Content); |
| 100 | + Assert.Contains("4", result.Message.Content); |
| 101 | + } |
| 102 | + |
| 103 | + [SkippableFact] |
| 104 | + public async Task GroqCloud_Should_RespondWithParams() |
| 105 | + { |
| 106 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.GroqCloud)?.ApiKeyEnvName!); |
| 107 | + |
| 108 | + var result = await AIHub.Chat() |
| 109 | + .WithModel(Models.Groq.Llama3_1_8bInstant) |
| 110 | + .WithMessage(TestQuestion) |
| 111 | + .WithInferenceParams(new GroqCloudInferenceParams |
| 112 | + { |
| 113 | + Temperature = 0.3f, |
| 114 | + MaxTokens = 100, |
| 115 | + TopP = 0.9f |
| 116 | + }) |
| 117 | + .CompleteAsync(); |
| 118 | + |
| 119 | + Assert.True(result.Done); |
| 120 | + Assert.NotNull(result.Message); |
| 121 | + Assert.NotEmpty(result.Message.Content); |
| 122 | + Assert.Contains("4", result.Message.Content); |
| 123 | + } |
| 124 | + |
| 125 | + [SkippableFact] |
| 126 | + public async Task Xai_Should_RespondWithParams() |
| 127 | + { |
| 128 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.Xai)?.ApiKeyEnvName!); |
| 129 | + |
| 130 | + var result = await AIHub.Chat() |
| 131 | + .WithModel(Models.Xai.Grok3Beta) |
| 132 | + .WithMessage(TestQuestion) |
| 133 | + .WithInferenceParams(new XaiInferenceParams |
| 134 | + { |
| 135 | + Temperature = 0.3f, |
| 136 | + MaxTokens = 100, |
| 137 | + TopP = 0.9f |
| 138 | + }) |
| 139 | + .CompleteAsync(); |
| 140 | + |
| 141 | + Assert.True(result.Done); |
| 142 | + Assert.NotNull(result.Message); |
| 143 | + Assert.NotEmpty(result.Message.Content); |
| 144 | + Assert.Contains("4", result.Message.Content); |
| 145 | + } |
| 146 | + |
| 147 | + [SkippableFact] |
| 148 | + public async Task Self_Should_RespondWithParams() |
| 149 | + { |
| 150 | + Skip.If(!File.Exists("C:/Models/gemma2-2b.gguf"), "Local model not found at C:/Models/gemma2-2b.gguf"); |
| 151 | + |
| 152 | + var result = await AIHub.Chat() |
| 153 | + .WithModel(Models.Local.Gemma2_2b) |
| 154 | + .WithMessage(TestQuestion) |
| 155 | + .WithInferenceParams(new LocalInferenceParams |
| 156 | + { |
| 157 | + Temperature = 0.3f, |
| 158 | + ContextSize = 8192, |
| 159 | + MaxTokens = 100, |
| 160 | + TopK = 40, |
| 161 | + TopP = 0.9f |
| 162 | + }) |
| 163 | + .CompleteAsync(); |
| 164 | + |
| 165 | + Assert.True(result.Done); |
| 166 | + Assert.NotNull(result.Message); |
| 167 | + Assert.NotEmpty(result.Message.Content); |
| 168 | + Assert.Contains("4", result.Message.Content); |
| 169 | + } |
| 170 | + |
| 171 | + [SkippableFact] |
| 172 | + public async Task LocalOllama_Should_RespondWithParams() |
| 173 | + { |
| 174 | + SkipIfOllamaNotRunning(); |
| 175 | + |
| 176 | + var result = await AIHub.Chat() |
| 177 | + .WithModel(Models.Ollama.Gemma3_4b) |
| 178 | + .WithMessage(TestQuestion) |
| 179 | + .WithInferenceParams(new OllamaInferenceParams |
| 180 | + { |
| 181 | + Temperature = 0.3f, |
| 182 | + MaxTokens = 100, |
| 183 | + TopK = 40, |
| 184 | + TopP = 0.9f, |
| 185 | + NumCtx = 2048 |
| 186 | + }) |
| 187 | + .CompleteAsync(); |
| 188 | + |
| 189 | + Assert.True(result.Done); |
| 190 | + Assert.NotNull(result.Message); |
| 191 | + Assert.NotEmpty(result.Message.Content); |
| 192 | + Assert.Contains("4", result.Message.Content); |
| 193 | + } |
| 194 | + |
| 195 | + [SkippableFact] |
| 196 | + public async Task ClaudOllama_Should_RespondWithParams() |
| 197 | + { |
| 198 | + SkipIfMissingKey(LLMApiRegistry.GetEntry(BackendType.Ollama)?.ApiKeyEnvName!); |
| 199 | + |
| 200 | + var result = await AIHub.Chat() |
| 201 | + .WithModel(Models.Ollama.Gemma3_4b) |
| 202 | + .WithMessage(TestQuestion) |
| 203 | + .WithInferenceParams(new OllamaInferenceParams |
| 204 | + { |
| 205 | + Temperature = 0.3f, |
| 206 | + MaxTokens = 100, |
| 207 | + TopK = 40, |
| 208 | + TopP = 0.9f, |
| 209 | + NumCtx = 2048 |
| 210 | + }) |
| 211 | + .CompleteAsync(); |
| 212 | + |
| 213 | + Assert.True(result.Done); |
| 214 | + Assert.NotNull(result.Message); |
| 215 | + Assert.NotEmpty(result.Message.Content); |
| 216 | + Assert.Contains("4", result.Message.Content); |
| 217 | + } |
| 218 | + |
| 219 | + // --- Params mismatch validation (no API key required) --- |
| 220 | + |
| 221 | + [Fact] |
| 222 | + public async Task Self_Should_ThrowWhenGivenWrongParams() |
| 223 | + { |
| 224 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 225 | + AIHub.Chat() |
| 226 | + .WithModel(Models.Local.Gemma2_2b) |
| 227 | + .WithMessage(TestQuestion) |
| 228 | + .WithInferenceParams(new OpenAiInferenceParams()) |
| 229 | + .CompleteAsync()); |
| 230 | + } |
| 231 | + |
| 232 | + [Fact] |
| 233 | + public async Task OpenAi_Should_ThrowWhenGivenWrongParams() |
| 234 | + { |
| 235 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 236 | + AIHub.Chat() |
| 237 | + .WithModel(Models.OpenAi.Gpt4oMini) |
| 238 | + .WithMessage(TestQuestion) |
| 239 | + .WithInferenceParams(new DeepSeekInferenceParams()) |
| 240 | + .CompleteAsync()); |
| 241 | + } |
| 242 | + |
| 243 | + [Fact] |
| 244 | + public async Task Anthropic_Should_ThrowWhenGivenWrongParams() |
| 245 | + { |
| 246 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 247 | + AIHub.Chat() |
| 248 | + .WithModel(Models.Anthropic.ClaudeSonnet4) |
| 249 | + .WithMessage(TestQuestion) |
| 250 | + .WithInferenceParams(new OpenAiInferenceParams()) |
| 251 | + .CompleteAsync()); |
| 252 | + } |
| 253 | + |
| 254 | + [Fact] |
| 255 | + public async Task Gemini_Should_ThrowWhenGivenWrongParams() |
| 256 | + { |
| 257 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 258 | + AIHub.Chat() |
| 259 | + .WithModel(Models.Gemini.Gemini2_0Flash) |
| 260 | + .WithMessage(TestQuestion) |
| 261 | + .WithInferenceParams(new AnthropicInferenceParams()) |
| 262 | + .CompleteAsync()); |
| 263 | + } |
| 264 | + |
| 265 | + [Fact] |
| 266 | + public async Task DeepSeek_Should_ThrowWhenGivenWrongParams() |
| 267 | + { |
| 268 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 269 | + AIHub.Chat() |
| 270 | + .WithModel(Models.DeepSeek.Reasoner) |
| 271 | + .WithMessage(TestQuestion) |
| 272 | + .WithInferenceParams(new GeminiInferenceParams()) |
| 273 | + .CompleteAsync()); |
| 274 | + } |
| 275 | + |
| 276 | + [Fact] |
| 277 | + public async Task GroqCloud_Should_ThrowWhenGivenWrongParams() |
| 278 | + { |
| 279 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 280 | + AIHub.Chat() |
| 281 | + .WithModel(Models.Groq.Llama3_1_8bInstant) |
| 282 | + .WithMessage(TestQuestion) |
| 283 | + .WithInferenceParams(new OpenAiInferenceParams()) |
| 284 | + .CompleteAsync()); |
| 285 | + } |
| 286 | + |
| 287 | + [Fact] |
| 288 | + public async Task Xai_Should_ThrowWhenGivenWrongParams() |
| 289 | + { |
| 290 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 291 | + AIHub.Chat() |
| 292 | + .WithModel(Models.Xai.Grok3Beta) |
| 293 | + .WithMessage(TestQuestion) |
| 294 | + .WithInferenceParams(new AnthropicInferenceParams()) |
| 295 | + .CompleteAsync()); |
| 296 | + } |
| 297 | + |
| 298 | + [Fact] |
| 299 | + public async Task Ollama_Should_ThrowWhenGivenWrongParams() |
| 300 | + { |
| 301 | + await Assert.ThrowsAsync<InvalidBackendParamsException>(() => |
| 302 | + AIHub.Chat() |
| 303 | + .WithModel(Models.Ollama.Gemma3_4b) |
| 304 | + .WithMessage(TestQuestion) |
| 305 | + .WithInferenceParams(new DeepSeekInferenceParams()) |
| 306 | + .CompleteAsync()); |
| 307 | + } |
| 308 | + |
| 309 | + private static void SkipIfMissingKey(string envName) |
| 310 | + { |
| 311 | + Skip.If(string.IsNullOrEmpty(Environment.GetEnvironmentVariable(envName)), |
| 312 | + $"{envName} environment variable not set"); |
| 313 | + } |
| 314 | + |
| 315 | + private static void SkipIfOllamaNotRunning() |
| 316 | + { |
| 317 | + Skip.If(!Helpers.NetworkHelper.PingHost("127.0.0.1", 11434, 3), |
| 318 | + "Ollama is not running on localhost:11434"); |
| 319 | + } |
| 320 | +} |
0 commit comments