|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.Threading.Tasks;
|
6 | 6 | using Amazon.BedrockRuntime;
|
| 7 | +using Amazon.Runtime; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
7 | 9 | using Microsoft.SemanticKernel.Embeddings;
|
8 | 10 | using Microsoft.SemanticKernel.Services;
|
9 | 11 | using Moq;
|
@@ -72,19 +74,37 @@ public void ShouldThrowExceptionForEmptyModelId()
|
72 | 74 | /// Checks that an invalid BedrockRuntime object will throw an exception.
|
73 | 75 | /// </summary>
|
74 | 76 | [Fact]
|
75 |
| - public async Task ShouldThrowExceptionForNullBedrockRuntimeAsync() |
| 77 | + public async Task ShouldThrowExceptionForNullBedrockRuntimeWhenNotConfiguredAsync() |
76 | 78 | {
|
77 | 79 | // Arrange
|
78 | 80 | string modelId = "amazon.titan-embed-text-v2:0";
|
79 | 81 | List<string> prompts = new() { "King", "Queen", "Prince" };
|
80 | 82 | IAmazonBedrockRuntime? nullBedrockRuntime = null;
|
| 83 | + bool notConfigured = false; |
81 | 84 |
|
82 |
| - // Act & Assert |
83 |
| - await Assert.ThrowsAnyAsync<Exception>(async () => |
| 85 | + try |
84 | 86 | {
|
85 |
| - var kernel = Kernel.CreateBuilder().AddBedrockTextEmbeddingGenerationService(modelId, nullBedrockRuntime).Build(); |
86 |
| - var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
87 |
| - await service.GenerateEmbeddingsAsync(prompts).ConfigureAwait(true); |
88 |
| - }).ConfigureAwait(true); |
| 87 | + var runtime = new ServiceCollection() |
| 88 | + .TryAddAWSService<IAmazonBedrockRuntime>() |
| 89 | + .BuildServiceProvider() |
| 90 | + .GetService<IAmazonBedrockRuntime>(); |
| 91 | + } |
| 92 | + catch (AmazonClientException) |
| 93 | + { |
| 94 | + // If cannot grab the runtime from the container then we are not configured |
| 95 | + notConfigured = true; |
| 96 | + } |
| 97 | + |
| 98 | + // Act |
| 99 | + if (notConfigured) |
| 100 | + { |
| 101 | + // If No RegionEndpoint or ServiceURL is configured, the runtime will throw an exception |
| 102 | + await Assert.ThrowsAnyAsync<Exception>(async () => |
| 103 | + { |
| 104 | + var kernel = Kernel.CreateBuilder().AddBedrockTextEmbeddingGenerationService(modelId, nullBedrockRuntime).Build(); |
| 105 | + var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); |
| 106 | + await service.GenerateEmbeddingsAsync(prompts).ConfigureAwait(true); |
| 107 | + }).ConfigureAwait(true); |
| 108 | + } |
89 | 109 | }
|
90 | 110 | }
|
0 commit comments