Skip to content

Added custom endpoint for bedrock provider #441

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions R/provider-bedrock.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ NULL
#' If you're using [cross-region inference](https://aws.amazon.com/blogs/machine-learning/getting-started-with-cross-region-inference-in-amazon-bedrock/),
#' you'll need to use the inference profile ID, e.g.
#' `model="us.anthropic.claude-3-5-sonnet-20240620-v1:0"`.
#' @param endpoint Optional. A custom endpoint URL (including protocol,
#' hostname, and any base path) to use instead of the default AWS Bedrock
#' runtime endpoint constructed from the region (e.g.,
#' `https://bedrock-runtime.us-east-1.amazonaws.com`). Trailing slashes
#' will be automatically removed.
#' @param api_args Named list of arbitrary extra arguments appended to the body
#' of every chat API call. Some useful arguments include:
#'
Expand Down Expand Up @@ -54,18 +59,24 @@ chat_aws_bedrock <- function(
model = NULL,
profile = NULL,
api_args = list(),
echo = NULL
echo = NULL,
endpoint = NULL
) {
check_installed("paws.common", "AWS authentication")
cache <- aws_creds_cache(profile)
credentials <- paws_credentials(profile, cache = cache)

model <- set_default(model, "anthropic.claude-3-5-sonnet-20240620-v1:0")
echo <- check_echo(echo)

# Clean endpoint URL if provided
if (!is.null(endpoint) && nzchar(endpoint)) {
endpoint <- sub("/$", "", endpoint)
}

provider <- ProviderAWSBedrock(
name = "AWS/Bedrock",
base_url = "",
base_url = endpoint %||% "",
model = model,
profile = profile,
region = credentials$region,
Expand Down Expand Up @@ -93,11 +104,13 @@ method(chat_request, ProviderAWSBedrock) <- function(
tools = list(),
type = NULL
) {
req <- request(paste0(
"https://bedrock-runtime.",
provider@region,
".amazonaws.com"
))
# Use custom endpoint if provided, otherwise construct default
if (nzchar(provider@base_url)) {
base_url <- provider@base_url
} else {
base_url <- paste0("https://bedrock-runtime.", provider@region, ".amazonaws.com")
}
req <- request(base_url)
req <- req_url_path_append(
req,
"model",
Expand Down
Loading