Calls fn with ... as calling it does, and gives each event of the call
(and of every call made inside it: a program's steps, a tool that calls an
AI function) to .each as it happens: started, request, text (a
piece of an output's text, e$field, e$text; e$answer is TRUE for
the answer), thinking, tool_call, tool_result, retry (the text so
far is void: the model is asked again), and done or failed. With
.show, the answer's text is written to the console as it comes.
Usage
ai_stream(fn, ..., .each = NULL, .show = interactive())
stop_stream()
ai_text(stream)Value
A stream (invisibly): $value (what the call returned), $error
(the error it ended with, or NULL), $events (every event, in order);
ai_text(stream) is the answer's text.
Details
A call watched this way is streamed from the provider when it is the only
one in flight; the rows of a column go through the connection pool as
always, and each reply is shown whole. Calling stop_stream() inside
.each stops the call: the request in progress at its next piece, no new
request or call starts, and the call ends with the error Cancelled (in
the call log too).
Examples
if (FALSE) { # \dontrun{
s <- ai_stream(reply, "Where is my parcel?")
s$value
ai_stream(reply, "Hi", .each = function(e) if (e$kind == "text") message(e$text))
} # }