Skip to content

Commit 018132b

Browse files
feat(agent): support agent.stream for structuredModel (#161)
1 parent c325160 commit 018132b

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎agentscope-core/src/main/java/io/agentscope/core/agent/Agent.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,30 @@ default Flux<Event> stream(Msg msg) {
261261
default Flux<Event> stream(List<Msg> msgs) {
262262
return stream(msgs, StreamOptions.defaults());
263263
}
264+
265+
/**
266+
* Stream with default options (all event types except AGENT_RESULT, incremental mode).
267+
*
268+
* @param msg Input message
269+
* @param options Stream configuration options
270+
* @param structuredModel Optional class defining the structure
271+
*
272+
* @return Flux of events emitted during execution
273+
*/
274+
Flux<Event> stream(Msg msg, StreamOptions options, Class<?> structuredModel);
275+
276+
/**
277+
* Process multiple input messages with structured model and generate a response.
278+
*
279+
* <p>The structured model parameter defines the expected structure of input or output data.
280+
* The structured data will be stored in the returned message's metadata field.
281+
*
282+
* <p>Default implementation ignores the structuredModel parameter. Agents that support
283+
* structured input/output should override this method.
284+
*
285+
* @param msgs Input messages
286+
* @param structuredModel Optional class defining the structure
287+
* @return Response message with structured data in metadata
288+
*/
289+
Flux<Event> stream(List<Msg> msgs, StreamOptions options, Class<?> structuredModel);
264290
}

‎agentscope-core/src/main/java/io/agentscope/core/agent/AgentBase.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,19 @@ public final Flux<Event> stream(Msg msg, StreamOptions options) {
564564
return stream(List.of(msg), options);
565565
}
566566

567+
/**
568+
* Stream execution events in real-time as the agent processes the input with structured output.
569+
*
570+
* @param msg Input message
571+
* @param options Stream configuration options
572+
* @param structuredModel Optional class defining the structure of the output
573+
* @return Flux of events emitted during execution
574+
*/
575+
@Override
576+
public final Flux<Event> stream(Msg msg, StreamOptions options, Class<?> structuredModel) {
577+
return stream(List.of(msg), options, structuredModel);
578+
}
579+
567580
/**
568581
* Stream with multiple input messages.
569582
*
@@ -576,6 +589,20 @@ public final Flux<Event> stream(List<Msg> msgs, StreamOptions options) {
576589
return createEventStream(options, () -> call(msgs));
577590
}
578591

592+
/**
593+
* Stream with multiple input messages.
594+
*
595+
* @param msgs Input messages
596+
* @param options Stream configuration options
597+
* @param structuredModel Optional class defining the structure
598+
* @return Flux of events emitted during execution
599+
*/
600+
@Override
601+
public final Flux<Event> stream(
602+
List<Msg> msgs, StreamOptions options, Class<?> structuredModel) {
603+
return createEventStream(options, () -> call(msgs, structuredModel));
604+
}
605+
579606
/**
580607
* Helper method to create an event stream with proper hook lifecycle management.
581608
*

‎agentscope-examples/quickstart/src/main/java/io/agentscope/examples/quickstart/StructuredOutputExample.java‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.agentscope.examples.quickstart;
1717

1818
import io.agentscope.core.ReActAgent;
19+
import io.agentscope.core.agent.Event;
20+
import io.agentscope.core.agent.StreamOptions;
1921
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
2022
import io.agentscope.core.memory.InMemoryMemory;
2123
import io.agentscope.core.message.Msg;
@@ -24,6 +26,7 @@
2426
import io.agentscope.core.model.DashScopeChatModel;
2527
import io.agentscope.core.tool.Toolkit;
2628
import java.util.List;
29+
import reactor.core.publisher.Flux;
2730

2831
/**
2932
* StructuredOutputExample - Demonstrates structured output generation.
@@ -71,6 +74,10 @@ public static void main(String[] args) throws Exception {
7174
System.out.println("\n=== Example 3: Sentiment Analysis ===\n");
7275
runSentimentAnalysisExample(agent);
7376

77+
// Example 4: Extract product information by Stream
78+
System.out.println("=== Example 4: Product Information ===\n");
79+
runStreamProductAnalysisExample(agent);
80+
7481
System.out.println("\n=== All examples completed ===");
7582
}
7683

@@ -186,6 +193,46 @@ private static void runSentimentAnalysisExample(ReActAgent agent) {
186193
}
187194
}
188195

196+
/** Example 4: Extract product information from natural language description. */
197+
private static void runStreamProductAnalysisExample(ReActAgent agent) {
198+
String query =
199+
"I'm looking for a laptop. I need at least 16GB RAM, "
200+
+ "prefer Apple brand, and my budget is around $2000. "
201+
+ "It should be lightweight for travel.";
202+
203+
System.out.println("Query: " + query);
204+
System.out.println("\nRequesting structured output...\n");
205+
206+
Msg userMsg =
207+
Msg.builder()
208+
.role(MsgRole.USER)
209+
.content(
210+
TextBlock.builder()
211+
.text(
212+
"Extract the product requirements from this query: "
213+
+ query)
214+
.build())
215+
.build();
216+
217+
try {
218+
Flux<Event> eventFlux =
219+
agent.stream(userMsg, StreamOptions.defaults(), ProductRequirements.class);
220+
ProductRequirements result =
221+
eventFlux.blockLast().getMessage().getStructuredData(ProductRequirements.class);
222+
223+
System.out.println("Extracted structured data:");
224+
System.out.println(" Product Type: " + result.productType);
225+
System.out.println(" Brand: " + result.brand);
226+
System.out.println(" Min RAM: " + result.minRam + " GB");
227+
System.out.println(" Max Budget: $" + result.maxBudget);
228+
System.out.println(" Features: " + result.features);
229+
230+
} catch (Exception e) {
231+
System.err.println("Error: " + e.getMessage());
232+
e.printStackTrace();
233+
}
234+
}
235+
189236
// ==================== Structured Output Schema Classes ====================
190237

191238
/** Schema for product requirements extraction. */

0 commit comments

Comments
 (0)