-
Notifications
You must be signed in to change notification settings - Fork 862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Microsoft.Extensions.AI's IChatClient / IEmbeddingGenerator for IAmazonBedrockRuntime #3545
Open
stephentoub
wants to merge
8
commits into
aws:v4-development
Choose a base branch
from
stephentoub:meai
base: v4-development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement Microsoft.Extensions.AI's IChatClient / IEmbeddingGenerator for IAmazonBedrockRuntime #3545
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6308716
Implement Microsoft.Extensions.AI's IChatClient on AmazonBedrockRunti…
stephentoub ac5b251
Move chat client implementation to new extension project / method
stephentoub 885c839
Merge remote-tracking branch 'upstream/v4-development' into meai
stephentoub c93882e
Add nuspec
stephentoub a01d9b4
Add IEmbeddingGenerator implementation
stephentoub 6d3d514
Replace tabs with spaces
stephentoub addb25f
Add some arg validation and update M.E.AI version
stephentoub 9bed6d2
Rename file
stephentoub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ons/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetFramework.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net472</TargetFramework> | ||
<AssemblyName>AWSSDK.Extensions.Bedrock.MEAI</AssemblyName> | ||
<PackageId>AWSSDK.Extensions.Bedrock.MEAI</PackageId> | ||
|
||
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute> | ||
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<LangVersion>Latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="**/obj/**" /> | ||
</ItemGroup> | ||
|
||
<Choose> | ||
<When Condition=" '$(AWSKeyFile)' == '' "> | ||
<PropertyGroup> | ||
<AssemblyOriginatorKeyFile>..\..\..\sdk\awssdk.dll.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
</When> | ||
<Otherwise> | ||
<PropertyGroup> | ||
<AssemblyOriginatorKeyFile>$(AWSKeyFile)</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../../sdk/src/Core/AWSSDK.Core.NetFramework.csproj" /> | ||
<ProjectReference Include="../../../sdk/src/Services/BedrockRuntime/AWSSDK.BedrockRuntime.NetFramework.csproj" /> | ||
</ItemGroup> | ||
</Project> |
50 changes: 50 additions & 0 deletions
50
...ions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetStandard.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks> | ||
<AssemblyName>AWSSDK.Extensions.Bedrock.MEAI</AssemblyName> | ||
<PackageId>AWSSDK.Extensions.Bedrock.MEAI</PackageId> | ||
|
||
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute> | ||
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<LangVersion>Latest</LangVersion> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<IsTrimmable>true</IsTrimmable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="**/obj/**" /> | ||
</ItemGroup> | ||
|
||
<Choose> | ||
<When Condition=" '$(AWSKeyFile)' == '' "> | ||
<PropertyGroup> | ||
<AssemblyOriginatorKeyFile>..\..\..\sdk\awssdk.dll.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
</When> | ||
<Otherwise> | ||
<PropertyGroup> | ||
<AssemblyOriginatorKeyFile>$(AWSKeyFile)</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
</Otherwise> | ||
</Choose> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../../sdk/src/Core/AWSSDK.Core.NetStandard.csproj" /> | ||
<ProjectReference Include="../../../sdk/src/Services/BedrockRuntime/AWSSDK.BedrockRuntime.NetStandard.csproj" /> | ||
</ItemGroup> | ||
</Project> |
44 changes: 44 additions & 0 deletions
44
extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.nuspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<package> | ||
<metadata> | ||
<id>AWSSDK.Extensions.Bedrock.MEAI</id> | ||
<title>AWSSDK - Bedrock integration with Microsoft.Extensions.AI.</title> | ||
<version>4.0.0.0-preview.4</version> | ||
<authors>Amazon Web Services</authors> | ||
<description>Implementations of Microsoft.Extensions.AI's abstractions for Bedrock.</description> | ||
<language>en-US</language> | ||
<license type="expression">Apache-2.0</license> | ||
<projectUrl>https://github.com/aws/aws-sdk-net/</projectUrl> | ||
<tags>AWS Amazon aws-sdk-v4</tags> | ||
<icon>images\AWSLogo.png</icon> | ||
<dependencies> | ||
<group targetFramework="net472"> | ||
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.4" /> | ||
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" /> | ||
</group> | ||
<group targetFramework="netstandard2.0"> | ||
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.4" /> | ||
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" /> | ||
</group> | ||
<group targetFramework="net8.0"> | ||
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.4" /> | ||
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" /> | ||
</group> | ||
</dependencies> | ||
</metadata> | ||
<files> | ||
<file src="..\..\..\sdk\nuget-content\AWSLogo.png" target="images\" /> | ||
|
||
<file src=".\bin\Release\net472\AWSSDK.Extensions.Bedrock.MEAI.dll" target="lib\net472" /> | ||
<file src=".\bin\Release\net472\AWSSDK.Extensions.Bedrock.MEAI.pdb" target="lib\net472" /> | ||
<file src=".\bin\Release\net472\AWSSDK.Extensions.Bedrock.MEAI.xml" target="lib\net472" /> | ||
|
||
<file src=".\bin\Release\netstandard2.0\AWSSDK.Extensions.Bedrock.MEAI.dll" target="lib\netstandard2.0"/> | ||
<file src=".\bin\Release\netstandard2.0\AWSSDK.Extensions.Bedrock.MEAI.pdb" target="lib\netstandard2.0"/> | ||
<file src=".\bin\Release\netstandard2.0\AWSSDK.Extensions.Bedrock.MEAI.xml" target="lib\netstandard2.0" /> | ||
|
||
<file src=".\bin\Release\net8.0\AWSSDK.Extensions.Bedrock.MEAI.dll" target="lib\net8.0" /> | ||
<file src=".\bin\Release\net8.0\AWSSDK.Extensions.Bedrock.MEAI.pdb" target="lib\net8.0" /> | ||
<file src=".\bin\Release\net8.0\AWSSDK.Extensions.Bedrock.MEAI.xml" target="lib\net8.0" /> | ||
</files> | ||
</package> |
54 changes: 54 additions & 0 deletions
54
extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AmazonBedrockRuntimeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Microsoft.Extensions.AI; | ||
using System; | ||
|
||
namespace Amazon.BedrockRuntime; | ||
|
||
/// <summary>Provides extensions for working with <see cref="IAmazonBedrockRuntime"/> instances.</summary> | ||
public static class AmazonBedrockRuntimeExtensions | ||
{ | ||
/// <summary>Gets an <see cref="IChatClient"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> | ||
/// <param name="runtime">The runtime instance to be represented as an <see cref="IChatClient"/>.</param> | ||
/// <param name="modelId"> | ||
/// The default model ID to use when no model is specified in a request. If not specified, | ||
/// a model must be provided in the <see cref="ChatOptions.ModelId"/> passed to <see cref="IChatClient.CompleteAsync"/> | ||
/// or <see cref="IChatClient.CompleteStreamingAsync"/>. | ||
/// </param> | ||
/// <returns>A <see cref="IChatClient"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> | ||
public static IChatClient AsChatClient(this IAmazonBedrockRuntime runtime, string? modelId = null) => | ||
runtime is not null ? new BedrockChatClient(runtime, modelId) : | ||
throw new ArgumentNullException(nameof(runtime)); | ||
|
||
/// <summary>Gets an <see cref="IChatClient"/> for the specified <see cref="IAmazonBedrockRuntime"/> instance.</summary> | ||
/// <param name="runtime">The runtime instance to be represented as an <see cref="IChatClient"/>.</param> | ||
/// <param name="modelId"> | ||
/// The default model ID to use when no model is specified in a request. If not specified, | ||
/// a model must be provided in the <see cref="ChatOptions.ModelId"/> passed to <see cref="IChatClient.CompleteAsync"/> | ||
/// or <see cref="IChatClient.CompleteStreamingAsync"/>. | ||
/// </param> | ||
/// <param name="dimensions"> | ||
/// The default number of dimensions to request be generated. This will be overridden by a <see cref="EmbeddingGenerationOptions.Dimensions"/> | ||
/// if that is specified to a request. If neither is specified, the default for the model will be used. | ||
/// </param> | ||
/// <returns>A <see cref="IChatClient"/> instance representing the <see cref="IAmazonBedrockRuntime"/> instance.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="runtime"/> is <see langword="null"/>.</exception> | ||
public static IEmbeddingGenerator<string, Embedding<float>> AsEmbeddingGenerator( | ||
this IAmazonBedrockRuntime runtime, string? modelId = null, int? dimensions = null) => | ||
runtime is not null ? new BedrockEmbeddingGenerator(runtime, modelId, dimensions) : | ||
throw new ArgumentNullException(nameof(runtime)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll let @normj chime in here, but normally we don't set the
LangVersion
forNetStandard
andNetFramework
projects, but since this is an extension package I think there is more flexibility here. For example, I see the null forgiving operator sprinkled in the PR but if we dropped theLangVersion
we wouldn't be able to use that. (For NetStandard2.0 at least). I don't see a problem with it myselfThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. My preference is always to use the latest language version and to keep the code as modern as possible, but I'm happy to adapt to whatever you'd like here.