|
| 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 | + * http://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 | + |
| 17 | +/* |
| 18 | + * Copyright 2024-2025 the original author or authors. |
| 19 | + * |
| 20 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | + * you may not use this file except in compliance with the License. |
| 22 | + * You may obtain a copy of the License at |
| 23 | + * |
| 24 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | + * |
| 26 | + * Unless required by applicable law or agreed to in writing, software |
| 27 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | + * See the License for the specific language governing permissions and |
| 30 | + * limitations under the License. |
| 31 | + */ |
| 32 | +package io.agentscope.examples.micronaut; |
| 33 | + |
| 34 | +import io.agentscope.core.ReActAgent; |
| 35 | +import io.agentscope.core.message.Msg; |
| 36 | +import io.agentscope.core.message.MsgRole; |
| 37 | +import io.agentscope.core.message.TextBlock; |
| 38 | +import io.micronaut.context.ApplicationContext; |
| 39 | + |
| 40 | +/** |
| 41 | + * Micronaut Integration Example - demonstrates AgentScope with Micronaut dependency injection. |
| 42 | + * |
| 43 | + * <p>This example shows how to: |
| 44 | + * <ul> |
| 45 | + * <li>Use Micronaut ApplicationContext for dependency injection |
| 46 | + * <li>Configure AgentScope via application.yml |
| 47 | + * <li>Inject ReActAgent from Micronaut factory |
| 48 | + * </ul> |
| 49 | + * |
| 50 | + * <p>Run with: |
| 51 | + * <pre> |
| 52 | + * DASHSCOPE_API_KEY=your-key mvn exec:java |
| 53 | + * </pre> |
| 54 | + */ |
| 55 | +public class Application { |
| 56 | + |
| 57 | + public static void main(String[] args) { |
| 58 | + System.out.println("\n=== AgentScope Micronaut Example ===\n"); |
| 59 | + |
| 60 | + // Start Micronaut ApplicationContext |
| 61 | + try (ApplicationContext context = ApplicationContext.run()) { |
| 62 | + // Get ReActAgent from Micronaut - configured via application.yml |
| 63 | + ReActAgent agent = context.getBean(ReActAgent.class); |
| 64 | + |
| 65 | + System.out.println("✓ Agent created via Micronaut dependency injection"); |
| 66 | + System.out.println("✓ Configuration loaded from application.yml"); |
| 67 | + System.out.println("\nStarting chat...\n"); |
| 68 | + |
| 69 | + // Simple chat example |
| 70 | + String[] questions = { |
| 71 | + "What is AgentScope?", "What programming languages does it support?", "Thank you!" |
| 72 | + }; |
| 73 | + |
| 74 | + for (String question : questions) { |
| 75 | + System.out.println("User: " + question); |
| 76 | + |
| 77 | + // Create user message |
| 78 | + Msg userMsg = |
| 79 | + Msg.builder() |
| 80 | + .role(MsgRole.USER) |
| 81 | + .content(TextBlock.builder().text(question).build()) |
| 82 | + .build(); |
| 83 | + |
| 84 | + // Call agent and get response |
| 85 | + Msg response = agent.call(userMsg).block(); |
| 86 | + System.out.println("Agent: " + response.getContent()); |
| 87 | + System.out.println(); |
| 88 | + } |
| 89 | + |
| 90 | + System.out.println("=== Example completed ===\n"); |
| 91 | + } catch (Exception e) { |
| 92 | + System.err.println("Error: " + e.getMessage()); |
| 93 | + e.printStackTrace(); |
| 94 | + System.exit(1); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments