| stream_text | R Documentation |
Generate text using a language model with streaming output. This function provides a real-time stream of tokens through a callback.
stream_text(
model = NULL,
prompt,
callback = NULL,
system = NULL,
temperature = 0.7,
max_tokens = NULL,
tools = NULL,
max_steps = 1,
max_tool_result_errors = 2,
require_post_tool_protocol = FALSE,
sandbox = FALSE,
skills = NULL,
session = NULL,
hooks = NULL,
registry = NULL,
renderer = NULL,
.stream_event_callback = NULL,
...
)
model |
Either a LanguageModelV1 object, or a string ID like "openai:gpt-4o". |
prompt |
A character string prompt, or a list of messages. |
callback |
A function called for each text chunk: |
system |
Optional system prompt. |
temperature |
Sampling temperature (0-2). Default 0.7. |
max_tokens |
Maximum tokens to generate. |
tools |
Optional list of Tool objects for function calling. |
max_steps |
Number of model/tool steps in one execution window. Default 1. The runtime treats this as a budget checkpoint, not as a hard task stop. |
max_tool_result_errors |
Historical compatibility option. Tool result errors are recorded as task observations; runtime policy decides whether to continue, finalize, ask the user, or block. |
require_post_tool_protocol |
Logical. If TRUE, after any tool results
are returned the model must either make another tool call or wrap its final
answer in a |
sandbox |
Logical. If TRUE, enables R-native programmatic sandbox mode.
See |
skills |
Optional path to skills directory, or a SkillRegistry object. |
session |
Optional ChatSession object for shared state. |
hooks |
Optional HookHandler object. |
registry |
Optional ProviderRegistry to use. |
renderer |
Optional Renderer for agent output. Defaults to the cli
terminal backend ( |
.stream_event_callback |
Internal callback for typed stream events. |
... |
Additional arguments passed to the model. |
A GenerateResult object (accumulated from the stream).
if (interactive()) {
model <- create_openai()$language_model("gpt-4o")
stream_text(model, "Tell me a story", callback = function(text, done) {
if (!done) cat(text)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.