-
Notifications
You must be signed in to change notification settings - Fork 114
Return ContentThinking objects from stream_text()
#909
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
b443b80
1463749
41267fa
fef20b8
9cb50e1
b6bfa82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -606,14 +606,11 @@ Chat <- R6::R6Class( | |
| if (stream) { | ||
| result <- NULL | ||
| for (chunk in response) { | ||
| text <- stream_text(private$provider, chunk) | ||
| if (!is.null(text)) { | ||
| content <- stream_content(private$provider, chunk) | ||
| if (!is.null(content)) { | ||
| text <- content_text(content) | ||
| emit(text) | ||
| if (yield_as_content) { | ||
| yield(ContentText(text)) | ||
| } else { | ||
| yield(text) | ||
| } | ||
| yield(if (yield_as_content) content else text) | ||
| any_text <- TRUE | ||
| } | ||
|
|
||
|
|
@@ -688,14 +685,11 @@ Chat <- R6::R6Class( | |
| if (stream) { | ||
| result <- NULL | ||
| for (chunk in await_each(response)) { | ||
| text <- stream_text(private$provider, chunk) | ||
| if (!is.null(text)) { | ||
| content <- stream_content(private$provider, chunk) | ||
| if (!is.null(content)) { | ||
| text <- content_text(content) | ||
| emit(text) | ||
| if (yield_as_content) { | ||
| yield(ContentText(text)) | ||
| } else { | ||
| yield(text) | ||
| } | ||
| yield(if (yield_as_content) content else text) | ||
| any_text <- TRUE | ||
| } | ||
|
|
||
|
|
@@ -774,6 +768,23 @@ Chat <- R6::R6Class( | |
| ) | ||
| ) | ||
|
|
||
| stream_content <- function(provider, event) { | ||
| result <- stream_text(provider, event) | ||
|
||
| if (is.null(result)) { | ||
| return(NULL) | ||
| } | ||
| if (S7_inherits(result, Content)) result else ContentText(result) | ||
| } | ||
|
|
||
| content_text <- function(content) { | ||
| switch( | ||
| class(content)[1], | ||
| "ellmer::ContentThinking" = content@thinking, | ||
| "ellmer::ContentText" = content@text, | ||
| format(content) | ||
| ) | ||
| } | ||
|
|
||
| #' @export | ||
| print.Chat <- function(x, ...) { | ||
| provider <- x$get_provider() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now needs an update, but otherwise looks good I think.