paws-r / paws

Paws, a package for Amazon Web Services in R
https://www.paws-r-sdk.com
Other
305 stars 37 forks source link

Missing components from response object for bedrock client #761

Closed DyfanJones closed 3 months ago

DyfanJones commented 3 months ago

Hi @DyfanJones , thanks for the fix and apologies for the delayed response! I was on a lengthy business trip with limited hands-on-keyboard time. You were correct with your remark regarding the return object, which should and now is a json in raw bytes. Your fixes solved the issues described above.

After the latest release of paws.common-v0.7.1, I only found one minor difference in paws compared to boto3 for invoke_model.

The default contentType value is application/json (see boto3 documentation here), as shown below, re-using the Python example from above.

response = bedrock.invoke_model(body=body, modelId="anthropic.claude-v2:1")
print(response.get("contentType"))`

application/json

However, the R equivalent returns the following:

response <- bedrock$invoke_model(body = body, modelId = "anthropic.claude-v2:1")
response$contentType

logical(0)

The behavior stays the same even when setting the contentType explicitly:

response <- bedrock$invoke_model(body = body, contentType = "application/json", modelId = "anthropic.claude-v2:1")
response$contentType

logical(0)

Originally posted by @alex23lemm in https://github.com/paws-r/paws/issues/749#issuecomment-1985972916

DyfanJones commented 3 months ago

I think I have a possible solution.

remotes::install_github("dyfanjones/paws/paws.common", ref ="unmarshal_header")

I believe aws sdk go v1 would have this bug as it skips the unmarshalling of headers that's first character is the same when set to lower case i.e. contentType https://github.com/aws/aws-sdk-go/blob/main/private/protocol/rest/unmarshal.go#L117-L122

However botocore doesn't do this and would unmarshal all headers 🤔 when location is found. https://github.com/boto/botocore/blob/7e24ee2369ef2fbd0bb89294848e2e4fc76e66a7/botocore/parsers.py#L949-L971

library(paws)
client <- bedrockruntime(config(credentials(profile = "paws")))

body <- list(prompt = "\n\nHuman: Tell me a funny joke about outer space\n\nAssistant:") |>
  jsonlite::toJSON(auto_unbox = TRUE)

response <- client$invoke_model(
  body = body,
  modelId = "ai21.j2-ultra",
)

response$contentType
#> [1] "application/json"

Created on 2024-03-12 with reprex v2.1.0