Skip to content

Commit 0674fed

Browse files
authored
feat: Implement AgentScope Micronaut Starter module (#189)
1 parent 319b26d commit 0674fed

29 files changed

Lines changed: 3864 additions & 0 deletions
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# AgentScope Micronaut Example
2+
3+
Simple example demonstrating AgentScope integration with Micronaut framework dependency injection.
4+
5+
## Features
6+
7+
**Micronaut Dependency Injection** - Beans configured via application.yml
8+
**Multiple LLM Providers** - DashScope, OpenAI, Gemini, Anthropic
9+
**Simple Configuration** - Just set environment variables and run
10+
11+
## Quick Start
12+
13+
### 1. Prerequisites
14+
15+
- Java 17 or later
16+
- Maven 3.8+
17+
18+
### 2. Configuration
19+
20+
The example uses configuration from `src/main/resources/application.yml`. Set your API key via environment variable:
21+
22+
```bash
23+
export DASHSCOPE_API_KEY=your-api-key
24+
```
25+
26+
### 3. Run
27+
28+
```bash
29+
mvn clean compile exec:java
30+
```
31+
32+
## How It Works
33+
34+
This example demonstrates:
35+
36+
1. **Micronaut ApplicationContext** - Starts the DI container
37+
2. **Bean Injection** - `ReActAgent` is injected from Micronaut factory
38+
3. **Configuration** - All settings loaded from `application.yml`
39+
40+
The key difference from manual setup is that beans are created and configured automatically by Micronaut.
41+
42+
## Configuration
43+
44+
You can change the LLM provider in `application.yml`:
45+
46+
```yaml
47+
agentscope:
48+
model:
49+
provider: dashscope # or: openai, gemini, anthropic
50+
51+
dashscope:
52+
api-key: ${DASHSCOPE_API_KEY}
53+
model-name: qwen-plus
54+
```
55+
56+
See the [Micronaut Integration README](../../agentscope-micronaut/README.md) for full configuration options.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<parent>
23+
<groupId>io.agentscope</groupId>
24+
<artifactId>agentscope-examples</artifactId>
25+
<version>${revision}</version>
26+
</parent>
27+
28+
<artifactId>micronaut-example</artifactId>
29+
<name>AgentScope Java - Micronaut Example</name>
30+
<description>Complete example demonstrating AgentScope with Micronaut framework</description>
31+
32+
<properties>
33+
<exec.mainClass>io.agentscope.examples.micronaut.Application</exec.mainClass>
34+
</properties>
35+
36+
<dependencies>
37+
<!-- AgentScope Micronaut Integration -->
38+
<dependency>
39+
<groupId>io.agentscope</groupId>
40+
<artifactId>agentscope-micronaut</artifactId>
41+
<version>${revision}</version>
42+
</dependency>
43+
44+
<!-- Logging -->
45+
<dependency>
46+
<groupId>ch.qos.logback</groupId>
47+
<artifactId>logback-classic</artifactId>
48+
<version>1.5.3</version>
49+
<scope>runtime</scope>
50+
</dependency>
51+
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<!-- Exec Plugin for Running the Application -->
56+
<plugin>
57+
<groupId>org.codehaus.mojo</groupId>
58+
<artifactId>exec-maven-plugin</artifactId>
59+
<version>3.1.0</version>
60+
<configuration>
61+
<mainClass>${exec.mainClass}</mainClass>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
67+
</project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
micronaut:
18+
application:
19+
name: agentscope-micronaut-example
20+
server:
21+
port: 8080
22+
23+
# AgentScope Configuration
24+
agentscope:
25+
model:
26+
# Available providers: dashscope, openai, gemini, anthropic
27+
provider: dashscope
28+
29+
# DashScope Configuration (Default)
30+
dashscope:
31+
enabled: true
32+
api-key: ${DASHSCOPE_API_KEY:sk-test}
33+
model-name: qwen-plus
34+
stream: true
35+
enable-thinking: false
36+
37+
# OpenAI Configuration (Optional)
38+
openai:
39+
enabled: false
40+
api-key: ${OPENAI_API_KEY:}
41+
model-name: gpt-4-mini
42+
stream: true
43+
44+
# Gemini Configuration (Optional)
45+
gemini:
46+
enabled: false
47+
api-key: ${GEMINI_API_KEY:}
48+
model-name: gemini-2.0-flash
49+
stream: true
50+
51+
# Anthropic Configuration (Optional)
52+
anthropic:
53+
enabled: false
54+
api-key: ${ANTHROPIC_API_KEY:}
55+
model-name: claude-sonnet-4.5
56+
stream: true
57+
58+
# Agent Configuration
59+
agent:
60+
enabled: true
61+
name: "AgentScope Assistant"
62+
sys-prompt: |
63+
You are a helpful AI assistant powered by AgentScope.
64+
You can help users with questions, provide information, and assist with various tasks.
65+
Always be respectful, accurate, and helpful.
66+
max-iters: 10
67+
68+
# Logging Configuration
69+
logger:
70+
levels:
71+
io.agentscope: INFO
72+
io.micronaut: INFO
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ You may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<configuration>
19+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
20+
<encoder>
21+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
22+
</encoder>
23+
</appender>
24+
25+
<root level="INFO">
26+
<appender-ref ref="STDOUT"/>
27+
</root>
28+
29+
<!-- Debug logging for test environment -->
30+
<logger name="io.agentscope" level="DEBUG"/>
31+
<logger name="io.micronaut" level="INFO"/>
32+
</configuration>

agentscope-examples/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>werewolf</module>
3737
<module>advanced</module>
3838
<module>multi-component</module>
39+
<module>micronaut-example</module>
3940
<module>quarkus-example</module>
4041
</modules>
4142

@@ -46,10 +47,20 @@
4647
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4748
<maven.deploy.skip>true</maven.deploy.skip>
4849
<spring.boot.version>3.3.0</spring.boot.version>
50+
<micronaut.version>4.8.3</micronaut.version>
4951
</properties>
5052

5153
<dependencyManagement>
5254
<dependencies>
55+
<!-- Micronaut Platform BOM -->
56+
<dependency>
57+
<groupId>io.micronaut.platform</groupId>
58+
<artifactId>micronaut-platform</artifactId>
59+
<version>${micronaut.version}</version>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
</dependency>
63+
5364
<!-- AgentScope Internal Modules -->
5465
<dependency>
5566
<groupId>io.agentscope</groupId>

0 commit comments

Comments
 (0)