-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #848 from DyfanJones/handler_stream
Handler stream
- Loading branch information
Showing
27 changed files
with
971 additions
and
264 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Streaming | ||
|
||
As of `paws v-0.8.0+` streaming is supported in `paws`. | ||
|
||
|
||
## Basic usage: | ||
|
||
Example taken from: https://docs.aws.amazon.com/code-library/latest/ug/python_3_bedrock-runtime_code_examples.html | ||
|
||
|
||
### Internal function: | ||
|
||
Paws allows for a function to be passed to the returning stream for processing. | ||
|
||
```r | ||
library(paws) | ||
|
||
client <- bedrockruntime() | ||
|
||
# Set the model ID, e.g., Titan Text Premier. | ||
model_id <- "amazon.titan-text-premier-v1:0" | ||
|
||
# Start a conversation with the user message. | ||
user_message <- "Describe the purpose of a 'hello world' program in one line." | ||
conversation <- list( | ||
list( | ||
role = "user", | ||
content = list(list(text= user_message)), | ||
) | ||
) | ||
|
||
resp <- client.converse_stream( | ||
modelId=model_id, | ||
messages=conversation, | ||
inferenceConfig=list(maxTokens = 512, temperature = 0.5, topP = 0.9) | ||
) | ||
|
||
resp$stream(\(chunk) chunk$contentBlockDelta$delta$text) | ||
``` | ||
|
||
### paws_connection: | ||
|
||
`paws` allows for the raw connection to be retrieved. The connection is a sub class of `httr2::httr2_response` class. | ||
This allows paws_connection to be handle both a paws parser or httr2 stream parser. | ||
|
||
```r | ||
library(paws) | ||
|
||
client <- bedrockruntime() | ||
|
||
# Set the model ID, e.g., Titan Text Premier. | ||
model_id <- "amazon.titan-text-premier-v1:0" | ||
|
||
# Start a conversation with the user message. | ||
user_message <- "Describe the purpose of a 'hello world' program in one line." | ||
conversation <- list( | ||
list( | ||
role = "user", | ||
content = list(list(text= user_message)), | ||
) | ||
) | ||
|
||
resp <- client.converse_stream( | ||
modelId=model_id, | ||
messages=conversation, | ||
inferenceConfig=list(maxTokens = 512, temperature = 0.5, topP = 0.9) | ||
) | ||
|
||
con <- resp$stream(.connection = TRUE) | ||
|
||
while(!is.null(chunk <- paws_stream_parser(con))) { | ||
print(chunk$contentBlockDelta$delta$text) | ||
} | ||
``` | ||
|
||
Note: the paws_stream_parser return the stream in the response syntax. In this case please check https://paws-r.github.io/docs/bedrockruntime_converse_stream/ | ||
|
||
For full flexibility you can use [httr2::resp_stream_aws](https://httr2.r-lib.org/reference/req_perform_stream.html?search-input=resp_stream_aws) to get the raw response from AWS. | ||
|
||
```r | ||
library(paws) | ||
|
||
client <- bedrockruntime() | ||
|
||
# Set the model ID, e.g., Titan Text Premier. | ||
model_id <- "amazon.titan-text-premier-v1:0" | ||
|
||
# Start a conversation with the user message. | ||
user_message <- "Describe the purpose of a 'hello world' program in one line." | ||
conversation <- list( | ||
list( | ||
role = "user", | ||
content = list(list(text= user_message)), | ||
) | ||
) | ||
|
||
resp <- client.converse_stream( | ||
modelId=model_id, | ||
messages=conversation, | ||
inferenceConfig=list(maxTokens = 512, temperature = 0.5, topP = 0.9) | ||
) | ||
|
||
con <- resp$stream(.connection = TRUE) | ||
repeat{ | ||
event <- resp_stream_aws(con) | ||
if (is.null(event)) { | ||
close(con) | ||
break | ||
} | ||
|
||
str(event) | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: make.paws | ||
Type: Package | ||
Title: Generate Paws AWS SDKs for R | ||
Version: 0.9.1 | ||
Version: 0.9.2 | ||
Authors@R: c( | ||
person("David", "Kretch", email = "[email protected]", role = "aut"), | ||
person("Adam", "Banker", email = "[email protected]", role = "aut"), | ||
|
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: paws.common | ||
Type: Package | ||
Title: Paws Low-Level Amazon Web Services API | ||
Version: 0.7.7.9000 | ||
Version: 0.8.0 | ||
Authors@R: c( | ||
person("David", "Kretch", email = "[email protected]", role = "aut"), | ||
person("Adam", "Banker", email = "[email protected]", role = "aut"), | ||
|
@@ -64,17 +64,17 @@ Collate: | |
'head_bucket.R' | ||
'http_status.R' | ||
'error.R' | ||
'tags.R' | ||
'xmlutil.R' | ||
'stream.R' | ||
'custom_s3.R' | ||
'handlers_core.R' | ||
'handlers_ec2query.R' | ||
'handlers_jsonrpc.R' | ||
'handlers_query.R' | ||
'handlers_rest.R' | ||
'handlers_restjson.R' | ||
'tags.R' | ||
'xmlutil.R' | ||
'handlers_restxml.R' | ||
'handlers_stream.R' | ||
'idempotency.R' | ||
'jsonutil.R' | ||
'onLoad.R' | ||
|
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
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
Oops, something went wrong.