Skip to main content

stream_llm

Function stream_llm 

Source
pub async fn stream_llm(
    model: &(dyn BaseChatModel + Send + Sync),
    input: &Value,
    system_prompt: &str,
) -> Result<Value, RunnableError>
Expand description

Stream LLM tokens via StreamWriter and return the final state update.

Calls model.astream() for token-by-token streaming. Each partial message is forwarded through the stream writer (if active) as a JSON payload:

{"type": "token", "content": "Hello"}

The final complete message is returned as a state update in {"messages": [response]} format.

§Example

let model_clone = model.clone();
graph.add_node("chatbot", move |input: JsonValue, _config: RunnableConfig| {
    let model = model_clone.clone();
    async move { stream_llm(model.as_ref(), &input, "You are a helpful assistant.").await }
})?;