|
1 | 1 | using Anthropic.SDK; |
2 | 2 | using Anthropic.SDK.Messaging; |
| 3 | +using Amazon; |
| 4 | +using Amazon.BedrockRuntime; |
3 | 5 | using Azure; |
4 | 6 | using Azure.AI.OpenAI; |
5 | 7 | using Microsoft.Extensions.AI; |
@@ -54,6 +56,28 @@ public static IChatClient CreateOpenAIChatClientWithApiKey() |
54 | 56 | return chatClient.AsIChatClient(); |
55 | 57 | } |
56 | 58 |
|
| 59 | + public static IChatClient? CreateAWSBedrockChatClient(out ChatOptions? options) |
| 60 | + { |
| 61 | + var accessKey = EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_ACCESS_KEY") ?? throw new EnvVarSettingException("AWS_BEDROCK_ACCESS_KEY is not set."); |
| 62 | + var secretKey = EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_SECRET_KEY") ?? throw new EnvVarSettingException("AWS_BEDROCK_SECRET_KEY is not set."); |
| 63 | + var region = EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_REGION") ?? "us-east-1"; |
| 64 | + var modelId = EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_MODEL_ID") ?? "anthropic.claude-3-7-sonnet-20250219-v1:0"; |
| 65 | + |
| 66 | + var regionEndpoint = RegionEndpoint.GetBySystemName(region); |
| 67 | + var runtime = new AmazonBedrockRuntimeClient(accessKey, secretKey, regionEndpoint); |
| 68 | + var chatClient = runtime.AsIChatClient(); |
| 69 | + |
| 70 | + options = new ChatOptions |
| 71 | + { |
| 72 | + ModelId = modelId, |
| 73 | + ToolMode = ChatToolMode.Auto, |
| 74 | + MaxOutputTokens = 4000 |
| 75 | + }; |
| 76 | + |
| 77 | + ConsoleHelpers.WriteDebugLine("Using AWS Bedrock API credentials for authentication"); |
| 78 | + return chatClient; |
| 79 | + } |
| 80 | + |
57 | 81 | public static IChatClient CreateCopilotChatClientWithGitHubToken() |
58 | 82 | { |
59 | 83 | var model = EnvironmentHelpers.FindEnvVar("COPILOT_MODEL_NAME") ?? "claude-3.7-sonnet"; |
@@ -114,6 +138,12 @@ public static IChatClient CreateCopilotChatClientWithGitHubToken() |
114 | 138 | { |
115 | 139 | return CreateAnthropicChatClientWithApiKey(out options); |
116 | 140 | } |
| 141 | + else if ((preferredProvider == "aws" || preferredProvider == "bedrock" || preferredProvider == "aws-bedrock") && |
| 142 | + !string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_ACCESS_KEY")) && |
| 143 | + !string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_SECRET_KEY"))) |
| 144 | + { |
| 145 | + return CreateAWSBedrockChatClient(out options); |
| 146 | + } |
117 | 147 | else if ((preferredProvider == "azure-openai" || preferredProvider == "azure") && !string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AZURE_OPENAI_API_KEY"))) |
118 | 148 | { |
119 | 149 | return CreateAzureOpenAIChatClientWithApiKey(); |
@@ -146,6 +176,12 @@ public static IChatClient CreateCopilotChatClientWithGitHubToken() |
146 | 176 | return CreateAnthropicChatClientWithApiKey(out options); |
147 | 177 | } |
148 | 178 |
|
| 179 | + if (!string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_ACCESS_KEY")) && |
| 180 | + !string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AWS_BEDROCK_SECRET_KEY"))) |
| 181 | + { |
| 182 | + return CreateAWSBedrockChatClient(out options); |
| 183 | + } |
| 184 | + |
149 | 185 | if (!string.IsNullOrEmpty(EnvironmentHelpers.FindEnvVar("AZURE_OPENAI_API_KEY"))) |
150 | 186 | { |
151 | 187 | return CreateAzureOpenAIChatClientWithApiKey(); |
@@ -178,6 +214,12 @@ public static IChatClient CreateChatClient(out ChatOptions? options) |
178 | 214 | - ANTHROPIC_API_KEY |
179 | 215 | - ANTHROPIC_MODEL_NAME (optional) |
180 | 216 |
|
| 217 | + To use AWS Bedrock, please set: |
| 218 | + - AWS_BEDROCK_ACCESS_KEY |
| 219 | + - AWS_BEDROCK_SECRET_KEY |
| 220 | + - AWS_BEDROCK_REGION (optional, default: us-east-1) |
| 221 | + - AWS_BEDROCK_MODEL_ID (optional, default: anthropic.claude-3-7-sonnet-20250219-v1:0) |
| 222 | + |
181 | 223 | To use Azure OpenAI, please set: |
182 | 224 | - AZURE_OPENAI_API_KEY |
183 | 225 | - AZURE_OPENAI_ENDPOINT |
|
0 commit comments