|
16 | 16 | package io.agentscope.examples.quickstart; |
17 | 17 |
|
18 | 18 | import io.agentscope.core.ReActAgent; |
| 19 | +import io.agentscope.core.agent.Event; |
| 20 | +import io.agentscope.core.agent.StreamOptions; |
19 | 21 | import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter; |
20 | 22 | import io.agentscope.core.memory.InMemoryMemory; |
21 | 23 | import io.agentscope.core.message.Msg; |
|
24 | 26 | import io.agentscope.core.model.DashScopeChatModel; |
25 | 27 | import io.agentscope.core.tool.Toolkit; |
26 | 28 | import java.util.List; |
| 29 | +import reactor.core.publisher.Flux; |
27 | 30 |
|
28 | 31 | /** |
29 | 32 | * StructuredOutputExample - Demonstrates structured output generation. |
@@ -71,6 +74,10 @@ public static void main(String[] args) throws Exception { |
71 | 74 | System.out.println("\n=== Example 3: Sentiment Analysis ===\n"); |
72 | 75 | runSentimentAnalysisExample(agent); |
73 | 76 |
|
| 77 | + // Example 4: Extract product information by Stream |
| 78 | + System.out.println("=== Example 4: Product Information ===\n"); |
| 79 | + runStreamProductAnalysisExample(agent); |
| 80 | + |
74 | 81 | System.out.println("\n=== All examples completed ==="); |
75 | 82 | } |
76 | 83 |
|
@@ -186,6 +193,46 @@ private static void runSentimentAnalysisExample(ReActAgent agent) { |
186 | 193 | } |
187 | 194 | } |
188 | 195 |
|
| 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 | + |
189 | 236 | // ==================== Structured Output Schema Classes ==================== |
190 | 237 |
|
191 | 238 | /** Schema for product requirements extraction. */ |
|
0 commit comments