Skip to content
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

Add api args and verbose parameters to Bedrock provider #292

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

billsanto
Copy link

Added api_args and verbose parameters to the Bedrock provider. api_args is a list for parameters such as temperature, top_p, top_k, max_tokens, and stop_sequences. Added validation for each of these parameters. The verbose parameter adds useful information for developers and end users to inspect request and response headers and payloads. Updated the Roxygen documentation.

@atheriel
Copy link
Collaborator

atheriel commented Feb 3, 2025

It's definitely an oversight that chat_bedrock() does not yet have an api_args parameter. There's a lot going on here, though, some of which is discussed in #280 (a better parameters story) or #287 (debugging credentials, which is what I think you're mostly using verbose for -- emitting request details is already possible using httr2 features).

@hadley
Copy link
Member

hadley commented Feb 3, 2025

@billsanto thanks for working on this! As @atheriel says, I think this is probably a bit big, and anticipates some changes (like #280) that we'd rather tackle globally. Would you mind refocussing this PR on just enabling api_args? That should unblock you, without doing a bunch of work that we'll later have to roll back.

@billsanto
Copy link
Author

Thanks--I figured the verbose option was more appropriate as a central feature, so yes I can refocus on the api_args for the PR. I really needed that functionality to figure out what was going on under the hood. Is there a way I get both headers and payloads using httr2 directly? This is what the verbose output currently looks like:

> devtools::load_all()
ℹ Loading ellmer
> 
> model <-"us.amazon.nova-lite-v1:0" #"us.meta.llama3-2-3b-instruct-v1:0" # "us.anthropic.claude-3-5-haiku-20241022-v1:0" #
> profile <- "default"
> api_args <- list(temperature = 0, max_tokens = 2000)#, top_k=2, top_p=.5, stop_sequences=list("STOP"))
> verbose <- TRUE
> 
> client <- chat_bedrock(
+   model = model,
+   profile = profile,
+   api_args = api_args,
+   verbose = verbose,
+ )
> resp <- client$chat("What is 1+1", echo=FALSE)

── AWS Credentials 
ℹ Profile: default; Key: --REDACTED--; Secret: T6****; Session: ; Region: us-east-2

── Request Body 
{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "What is 1+1"
        }
      ]
    }
  ],
  "system": {},
  "toolConfig": {},
  "inferenceConfig": {
    "maxTokens": 2000,
    "temperature": 0
  }
} 
-> POST /model/us.amazon.nova-lite-v1:0/converse HTTP/2
-> Host: bedrock-runtime.us-east-2.amazonaws.com
-> User-Agent: httr2/1.1.0 r-curl/6.2.0 libcurl/8.11.1
-> Accept: */*
-> Accept-Encoding: deflate, gzip
-> x-amz-date: 20250203T151500Z
-> Authorization: AWS4-HMAC-SHA256 Credential=--REDACTED--/20250203/us-east-2/bedrock/aws4_request,SignedHeaders=host;x-amz-date;x-amz-security-token,Signature=4238b597731a55**6d8a6e885e7a
-> Content-Type: application/json
-> Content-Length: 150
-> 
<- HTTP/2 200 
<- date: Mon, 03 Feb 2025 15:15:02 GMT
<- content-type: application/json
<- content-length: 293
<- x-amzn-requestid: 63973db1-8600-4618-95f6-52f0d2d57f0c
<- 

── Response Body 
{
  "metrics": {
    "latencyMs": 475
  },
  "output": {
    "message": {
      "content": [
        {
          "text": "1 + 1 equals 2. This is a fundamental concept in arithmetic, where the sum of two ones results in the number two."
        }
      ],
      "role": "assistant"
    }
  },
  "stopReason": "end_turn",
  "usage": {
    "inputTokens": 6,
    "outputTokens": 29,
    "totalTokens": 35
  }
} 
> resp
[1] "1 + 1 equals 2. This is a fundamental concept in arithmetic, where the sum of two ones results in the number two."

> resp <- client$chat("What is 1+1", echo=TRUE)

── AWS Credentials 
ℹ Profile: default; Key: --REDACTED--; Secret: T6****; Session: ; Region: us-east-2

── Request Body 
{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "What is 1+1"
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "text": "1 + 1 equals 2. This is a fundamental concept in arithmetic, where the sum of two ones results in the number two."
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "text": "What is 1+1"
        }
      ]
    }
  ],
  "system": {},
  "toolConfig": {},
  "inferenceConfig": {
    "maxTokens": 2000,
    "temperature": 0
  }
} 
-> POST /model/us.amazon.nova-lite-v1:0/converse-stream HTTP/2
-> Host: bedrock-runtime.us-east-2.amazonaws.com
-> User-Agent: httr2/1.1.0 r-curl/6.2.0 libcurl/8.11.1
-> Accept: */*
-> Accept-Encoding: deflate, gzip
-> x-amz-date: 20250203T153454Z
-> Authorization: AWS4-HMAC-SHA256 Credential=--REDACTED--/20250203/us-east-2/bedrock/aws4_request,SignedHeaders=host;x-amz-date;x-amz-security-token,Signature=6153f4782efb0dcb**383c4
-> Content-Type: application/json
-> Content-Length: 359
-> 
<- HTTP/2 200 
<- date: Mon, 03 Feb 2025 15:34:56 GMT
<- content-type: application/vnd.amazon.eventstream
<- x-amzn-requestid: 138bb922-d99e-4b8c-92ac-578e9e85fe07
<- 

── Response Stream 

── Response Chunk 
{
  "role": "assistant",
  "event_type": "messageStart"
} 

── Response Stream 

── Response Chunk 
{
  "contentBlockIndex": 0,
  "delta": {
    "text": "As"
  },
  "event_type": "contentBlockDelta"
} 

── Response Stream 

── Response Chunk 
{
  "contentBlockIndex": 0,
  "delta": {
    "text": " previously"
  },
  "event_type": "contentBlockDelta"
} 
As 
── Response Stream 

...

(Some manual redactions made for the post).

@hadley
Copy link
Member

hadley commented Feb 3, 2025

You can wrap any call with httrr2::with_vebosity(code, 2) in order to see the headers and bodies/streaming responses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants