pub struct AgentStream<'a> { /* private fields */ }Expand description
Stream of agent events during execution
Implementations§
Source§impl<'a> AgentStream<'a>
impl<'a> AgentStream<'a>
Sourcepub async fn next(&mut self) -> Option<AgentEvent>
pub async fn next(&mut self) -> Option<AgentEvent>
Get the next event from the agent stream
Examples found in repository?
examples/agent_streaming/main.rs (line 138)
96async fn main() -> Result<()> {
97 // Build the model
98 // Using a model that supports tool calling (e.g., Llama 3.1, Qwen, Mistral)
99 let model = TextModelBuilder::new("../hf_models/qwen3_4b")
100 .with_isq(IsqType::Q4K)
101 .with_logging()
102 .with_paged_attn(|| PagedAttentionMetaBuilder::default().build())?
103 .build()
104 .await?;
105
106 // Create the agent with registered tools
107 // - get_weather is a sync tool (runs in spawn_blocking)
108 // - web_search is an async tool (runs natively async)
109 // Both can execute in parallel when the model calls multiple tools
110 let agent = AgentBuilder::new(model)
111 .with_system_prompt(
112 "You are a helpful assistant with access to weather and web search tools. \
113 Use them when needed to answer user questions accurately.",
114 )
115 .with_max_iterations(5)
116 .with_parallel_tool_execution(true) // Enable parallel tool execution (default)
117 .register_tool(get_weather_tool_with_callback())
118 .register_tool(web_search_tool_with_callback())
119 .build();
120
121 println!("=== Agent with Streaming Output ===\n");
122 println!(
123 "User: What's the weather like in Boston, and can you find me some good restaurants there?\n"
124 );
125 print!("Assistant: ");
126
127 // Run the agent with streaming output
128 let mut stream = agent
129 .run_stream(
130 "What's the weather like in Boston, and can you find me some good restaurants there?",
131 )
132 .await?;
133
134 let stdout = std::io::stdout();
135 let mut handle = stdout.lock();
136
137 // Process streaming events
138 while let Some(event) = stream.next().await {
139 match event {
140 AgentEvent::TextDelta(text) => {
141 // Print text as it streams - this gives real-time output
142 write!(handle, "{}", text)?;
143 handle.flush()?;
144 }
145 AgentEvent::ToolCallsStart(calls) => {
146 // Model is about to call tools
147 writeln!(handle, "\n\n[Calling {} tool(s)...]", calls.len())?;
148 for call in &calls {
149 writeln!(
150 handle,
151 " - {}: {}",
152 call.function.name, call.function.arguments
153 )?;
154 }
155 }
156 AgentEvent::ToolResult(result) => {
157 // A single tool finished execution
158 let status = if result.result.is_ok() { "OK" } else { "ERROR" };
159 writeln!(
160 handle,
161 " [Tool {} completed: {}]",
162 result.tool_name, status
163 )?;
164 }
165 AgentEvent::ToolCallsComplete => {
166 // All tools finished, model will continue generating
167 writeln!(handle, "[All tools completed, continuing...]\n")?;
168 write!(handle, "Assistant: ")?;
169 handle.flush()?;
170 }
171 AgentEvent::Complete(response) => {
172 // Agent finished executing
173 writeln!(handle, "\n\n=== Agent Execution Summary ===")?;
174 writeln!(handle, "Completed in {} iteration(s)", response.iterations)?;
175 writeln!(handle, "Stop reason: {:?}", response.stop_reason)?;
176 writeln!(handle, "Steps taken: {}", response.steps.len())?;
177
178 match response.stop_reason {
179 AgentStopReason::TextResponse => {
180 writeln!(handle, "Final response delivered successfully.")?;
181 }
182 AgentStopReason::MaxIterations => {
183 writeln!(
184 handle,
185 "Agent reached maximum iterations without producing a final response."
186 )?;
187 }
188 AgentStopReason::NoAction => {
189 writeln!(handle, "Agent produced no response.")?;
190 }
191 AgentStopReason::Error(e) => {
192 writeln!(handle, "Agent encountered an error: {}", e)?;
193 }
194 }
195 }
196 }
197 }
198
199 Ok(())
200}Auto Trait Implementations§
impl<'a> Freeze for AgentStream<'a>
impl<'a> !RefUnwindSafe for AgentStream<'a>
impl<'a> Send for AgentStream<'a>
impl<'a> Sync for AgentStream<'a>
impl<'a> Unpin for AgentStream<'a>
impl<'a> !UnwindSafe for AgentStream<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for T
impl<T> Downcast for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.