|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using AWS.Bedrock.MEAI; |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | +using System; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | + |
| 9 | +namespace Amazon.BedrockRuntime; |
| 10 | + |
| 11 | +/// <summary>Provides extensions for working with <see cref="IAmazonBedrockRuntime"/> instances.</summary> |
| 12 | +public static class AmazonBedrockRuntimeExtensions |
| 13 | +{ |
| 14 | + /// <summary>The provider name to use in metadata.</summary> |
| 15 | + internal const string ProviderName = "aws.bedrock"; |
| 16 | + |
| 17 | +#if NET8_0_OR_GREATER |
| 18 | + /// <summary>Gets an <see cref="IRealtimeClient"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> |
| 19 | + /// <param name="runtime">The runtime instance to be represented as an <see cref="IRealtimeClient"/>.</param> |
| 20 | + /// <param name="defaultModelId"> |
| 21 | + /// The default model ID to use when no model is specified in session options. If not specified, |
| 22 | + /// a model must be provided in the <see cref="RealtimeSessionOptions.Model"/> passed to |
| 23 | + /// <see cref="IRealtimeClient.CreateSessionAsync"/>. |
| 24 | + /// </param> |
| 25 | + /// <returns>An <see cref="IRealtimeClient"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> |
| 26 | + /// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> |
| 27 | + [Experimental("MEAI001")] |
| 28 | + public static IRealtimeClient AsIRealtimeClient(this IAmazonBedrockRuntime runtime, string? defaultModelId = null) => |
| 29 | + runtime is not null ? new BedrockNovaRealtimeClient(runtime, defaultModelId) : |
| 30 | + throw new ArgumentNullException(nameof(runtime)); |
| 31 | +#endif |
| 32 | + |
| 33 | + /// <summary>Gets an <see cref="IChatClient"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> |
| 34 | + /// <param name="runtime">The runtime instance to be represented as an <see cref="IChatClient"/>.</param> |
| 35 | + /// <param name="defaultModelId"> |
| 36 | + /// The default model ID to use when no model is specified in a request. If not specified, |
| 37 | + /// a model must be provided in the <see cref="ChatOptions.ModelId"/> passed to <see cref="IChatClient.GetResponseAsync"/> |
| 38 | + /// or <see cref="IChatClient.GetStreamingResponseAsync"/>. |
| 39 | + /// </param> |
| 40 | + /// <returns>A <see cref="IChatClient"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> |
| 41 | + /// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> |
| 42 | + public static IChatClient AsIChatClient(this IAmazonBedrockRuntime runtime, string? defaultModelId = null) => |
| 43 | + runtime is not null ? new BedrockChatClient(runtime, defaultModelId) : |
| 44 | + throw new ArgumentNullException(nameof(runtime)); |
| 45 | + |
| 46 | + /// <summary>Gets an <see cref="IChatClient"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> |
| 47 | + /// <param name="runtime">The runtime instance to be represented as an <see cref="IChatClient"/>.</param> |
| 48 | + /// <param name="defaultModelId"> |
| 49 | + /// The default model ID to use when no model is specified in a request. If not specified, |
| 50 | + /// a model must be provided in the <see cref="ChatOptions.ModelId"/> passed to <see cref="IChatClient.GetResponseAsync"/> |
| 51 | + /// or <see cref="IChatClient.GetStreamingResponseAsync"/>. |
| 52 | + /// </param> |
| 53 | + /// <param name="structuredOutputMode"> |
| 54 | + /// Controls how <see cref="ChatOptions.ResponseFormat"/> is realized. |
| 55 | + /// <see cref="BedrockStructuredOutputMode.Native"/> uses Bedrock native structured outputs |
| 56 | + /// (composes with user-provided tools and supports streaming) and requires a model that supports |
| 57 | + /// the feature. Use <see cref="BedrockStructuredOutputMode.SyntheticTool"/> for models without |
| 58 | + /// native support. |
| 59 | + /// </param> |
| 60 | + /// <returns>A <see cref="IChatClient"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> |
| 61 | + /// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> |
| 62 | + public static IChatClient AsIChatClient(this IAmazonBedrockRuntime runtime, string? defaultModelId, |
| 63 | + BedrockStructuredOutputMode structuredOutputMode) => |
| 64 | + runtime is not null ? new BedrockChatClient(runtime, defaultModelId, structuredOutputMode) : |
| 65 | + throw new ArgumentNullException(nameof(runtime)); |
| 66 | + |
| 67 | + /// <summary>Gets an <see cref="IEmbeddingGenerator{TInput, TEmbedding}"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> |
| 68 | + /// <param name="runtime">The runtime instance to be represented as an <see cref="IEmbeddingGenerator{TInput, TEmbedding}"/>.</param> |
| 69 | + /// <param name="defaultModelId"> |
| 70 | + /// The default model ID to use when no model is specified in a request. If not specified, |
| 71 | + /// a model must be provided in the <see cref="EmbeddingGenerationOptions.ModelId"/> passed to <see cref="IEmbeddingGenerator{TInput, TEmbedding}.GenerateAsync"/>. |
| 72 | + /// </param> |
| 73 | + /// <param name="defaultModelDimensions"> |
| 74 | + /// The default number of dimensions to request be generated. This will be overridden by a <see cref="EmbeddingGenerationOptions.Dimensions"/> |
| 75 | + /// if that is specified to a request. If neither is specified, the default for the model will be used. |
| 76 | + /// </param> |
| 77 | + /// <returns>An <see cref="IEmbeddingGenerator{TInput, TEmbedding}"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> |
| 78 | + /// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> |
| 79 | + public static IEmbeddingGenerator<string, Embedding<float>> AsIEmbeddingGenerator( |
| 80 | + this IAmazonBedrockRuntime runtime, string? defaultModelId = null, int? defaultModelDimensions = null) => |
| 81 | + runtime is not null ? new BedrockEmbeddingGenerator(runtime, defaultModelId, defaultModelDimensions) : |
| 82 | + throw new ArgumentNullException(nameof(runtime)); |
| 83 | + |
| 84 | + /// <summary>Gets an <see cref="IImageGenerator"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> |
| 85 | + /// <param name="runtime">The runtime instance to be represented as an <see cref="IImageGenerator"/>.</param> |
| 86 | + /// <param name="defaultModelId"> |
| 87 | + /// The default model ID to use when no model is specified in a request. If not specified, |
| 88 | + /// a model must be provided in the <see cref="ImageGenerationOptions.ModelId"/> passed to <see cref="IImageGenerator.GenerateAsync"/>. |
| 89 | + /// </param> |
| 90 | + /// <returns>An <see cref="IImageGenerator"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> |
| 91 | + /// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> |
| 92 | + [Experimental("MEAI001")] |
| 93 | + public static IImageGenerator AsIImageGenerator( |
| 94 | + this IAmazonBedrockRuntime runtime, string? defaultModelId = null) => |
| 95 | + runtime is not null ? new BedrockImageGenerator(runtime, defaultModelId) : |
| 96 | + throw new ArgumentNullException(nameof(runtime)); |
| 97 | +} |
0 commit comments