|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.agentscope.examples.advanced; |
| 17 | + |
| 18 | +import io.agentscope.core.ReActAgent; |
| 19 | +import io.agentscope.core.message.Msg; |
| 20 | +import io.agentscope.core.message.MsgRole; |
| 21 | +import io.agentscope.core.message.TextBlock; |
| 22 | +import io.agentscope.examples.advanced.util.MsgUtils; |
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStreamReader; |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility class providing common functionality for examples. |
| 29 | + */ |
| 30 | +public class ExampleUtils { |
| 31 | + |
| 32 | + private static final BufferedReader reader = |
| 33 | + new BufferedReader(new InputStreamReader(System.in)); |
| 34 | + |
| 35 | + private ExampleUtils() {} |
| 36 | + |
| 37 | + /** |
| 38 | + * Gets the DashScope API key from environment variable. Exits if not set. |
| 39 | + * |
| 40 | + * @return The API key |
| 41 | + */ |
| 42 | + public static String getDashScopeApiKey() { |
| 43 | + String apiKey = System.getenv("DASHSCOPE_API_KEY"); |
| 44 | + if (apiKey == null || apiKey.isEmpty()) { |
| 45 | + System.err.println("Error: DASHSCOPE_API_KEY environment variable not set."); |
| 46 | + System.err.println("Please set it with: export DASHSCOPE_API_KEY=your_api_key"); |
| 47 | + System.exit(1); |
| 48 | + } |
| 49 | + return apiKey; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Prints a welcome message for an example. |
| 54 | + * |
| 55 | + * @param title The title of the example |
| 56 | + * @param description The description of what the example demonstrates |
| 57 | + */ |
| 58 | + public static void printWelcome(String title, String description) { |
| 59 | + System.out.println("\n" + "=".repeat(80)); |
| 60 | + System.out.println(title); |
| 61 | + System.out.println("=".repeat(80)); |
| 62 | + System.out.println(description); |
| 63 | + System.out.println("=".repeat(80) + "\n"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Starts an interactive chat session with the given agent. |
| 68 | + * |
| 69 | + * @param agent The agent to chat with |
| 70 | + * @throws IOException If reading input fails |
| 71 | + */ |
| 72 | + public static void startChat(ReActAgent agent) throws IOException { |
| 73 | + System.out.println("Chat started! Type 'exit' to quit.\n"); |
| 74 | + |
| 75 | + while (true) { |
| 76 | + System.out.print("You: "); |
| 77 | + String input = reader.readLine(); |
| 78 | + |
| 79 | + if (input == null || input.trim().equalsIgnoreCase("exit")) { |
| 80 | + System.out.println("\nGoodbye!"); |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + if (input.trim().isEmpty()) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + Msg userMsg = |
| 89 | + Msg.builder() |
| 90 | + .role(MsgRole.USER) |
| 91 | + .content(TextBlock.builder().text(input.trim()).build()) |
| 92 | + .build(); |
| 93 | + |
| 94 | + System.out.print("\nAssistant: "); |
| 95 | + try { |
| 96 | + Msg response = agent.call(userMsg).block(); |
| 97 | + if (response != null) { |
| 98 | + System.out.println(MsgUtils.getTextContent(response)); |
| 99 | + } |
| 100 | + System.out.println(); |
| 101 | + } catch (Exception e) { |
| 102 | + System.err.println("\nError: " + e.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments