Skip to content

Commit 1d0e81d

Browse files
Alexxigangruigangliu.lrg
andauthored
feat(extensions): support chat completions API spring web starter (#382)
## AgentScope-Java Version [The version of AgentScope-Java i'm working on: 1.0.7-SNAPSHOT] ## Description [Please describe the background, purpose, changes made, and how to test this PR] add support for [Feature]: Chat Completions API Spring Web Starter [#219](#219) ## Checklist Please check the following items before code is ready to be reviewed. - [ ] Code has been formatted with `mvn spotless:apply` - [ ] All tests are passing (`mvn test`) - [ ] Javadoc comments are complete and follow project conventions - [ ] Related documentation has been updated (e.g. links, examples, etc.) - [ ] Code is ready for review --------- Co-authored-by: ruigangliu.lrg <ruigangliu.lrg@alibaba-inc.com>
1 parent 8b33e90 commit 1d0e81d

37 files changed

Lines changed: 6274 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2026 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+
<relativePath>../pom.xml</relativePath>
27+
</parent>
28+
29+
<groupId>io.agentscope.examples</groupId>
30+
<artifactId>chat-completions-web</artifactId>
31+
<packaging>jar</packaging>
32+
33+
<name>AgentScope Examples - Chat Completions Web</name>
34+
<description>Example Spring Boot app using the Chat Completions Web Starter</description>
35+
36+
<dependencyManagement>
37+
<dependencies>
38+
<!-- Spring Boot BOM -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-dependencies</artifactId>
42+
<version>${spring.boot.version}</version>
43+
<type>pom</type>
44+
<scope>import</scope>
45+
</dependency>
46+
</dependencies>
47+
</dependencyManagement>
48+
49+
<dependencies>
50+
<!-- AgentScope Core -->
51+
<dependency>
52+
<groupId>io.agentscope</groupId>
53+
<artifactId>agentscope-core</artifactId>
54+
</dependency>
55+
56+
<!-- Spring Boot Web (MVC) -->
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-web</artifactId>
60+
</dependency>
61+
62+
<!-- AgentScope Spring Boot auto-configuration (Model / ReActAgent / Toolkit) -->
63+
<dependency>
64+
<groupId>io.agentscope</groupId>
65+
<artifactId>agentscope-spring-boot-starter</artifactId>
66+
</dependency>
67+
68+
<!-- Chat Completions Web Starter we implemented -->
69+
<dependency>
70+
<groupId>io.agentscope</groupId>
71+
<artifactId>agentscope-chat-completions-web-starter</artifactId>
72+
<version>${revision}</version>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
<version>${spring.boot.version}</version>
82+
<executions>
83+
<execution>
84+
<goals>
85+
<goal>repackage</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2024-2026 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+
package io.agentscope.examples.chatcompletions;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
21+
/**
22+
* Minimal Spring Boot application demonstrating how to use
23+
* {@code agentscope-chat-completions-web-starter}.
24+
*
25+
* <p>After starting this app, you can call:
26+
*
27+
* <p>Non-streaming request (stream=false or omitted):
28+
*
29+
* <pre>
30+
* curl -X POST http://localhost:8080/v1/chat/completions \\
31+
* -H 'Content-Type: application/json' \\
32+
* -d '{
33+
* "model": "qwen3-max",
34+
* "stream": false,
35+
* "messages": [
36+
* { "role": "user", "content": "Hello, can you briefly introduce AgentScope Java?" }
37+
* ]
38+
* }'
39+
* </pre>
40+
*
41+
* <p>Streaming request (stream=true, Accept header is optional):
42+
*
43+
* <pre>
44+
* curl -N -X POST http://localhost:8080/v1/chat/completions \\
45+
* -H 'Content-Type: application/json' \\
46+
* -d '{
47+
* "model": "qwen3-max",
48+
* "stream": true,
49+
* "messages": [
50+
* { "role": "user", "content": "Please provide a streamed answer: Describe AgentScope Java in three sentences." }
51+
* ]
52+
* }'
53+
* </pre>
54+
*
55+
* <p><b>Important:</b> If stream=false but Accept: text/event-stream is set, the request will be
56+
* rejected with an error for consistency. Use stream=true for streaming, or omit the Accept header
57+
* for non-streaming.
58+
*/
59+
@SpringBootApplication
60+
public class ChatCompletionsWebApplication {
61+
62+
public static void main(String[] args) {
63+
SpringApplication.run(ChatCompletionsWebApplication.class, args);
64+
printStartupInfo();
65+
}
66+
67+
private static void printStartupInfo() {
68+
System.out.println("\n=== chat completions API spring web Example Application Started ===");
69+
System.out.println("\nExample curl command:");
70+
System.out.println("\nNon-streaming chat completion (stream=false or omitted).\n");
71+
System.out.println(
72+
"""
73+
curl -X POST http://localhost:8080/v1/chat/completions \\
74+
-H 'Content-Type: application/json' \\
75+
-d '{
76+
"model": "qwen3-max",
77+
"stream": false,
78+
"messages": [
79+
{ "role": "user", "content": "Please provide a Non streamed answer: Describe AgentScope Java in three sentences." }
80+
]
81+
}'
82+
""");
83+
System.out.println("\nNote: stream parameter can be omitted (defaults to false)");
84+
System.out.println("===================================================");
85+
System.out.println("\nStreaming chat completion (stream=true).\n");
86+
System.out.println(
87+
"""
88+
curl -N -X POST http://localhost:8080/v1/chat/completions \\
89+
-H 'Content-Type: application/json' \\
90+
-d '{
91+
"model": "qwen3-max",
92+
"stream": true,
93+
"messages": [
94+
{ "role": "user", "content": "Please provide a streamed answer: Describe AgentScope Java in three sentences." }
95+
]
96+
}'
97+
""");
98+
System.out.println("\nNote: Accept: text/event-stream header is optional when stream=true");
99+
System.out.println("===================================================");
100+
System.out.println(
101+
"\n⚠️ Important: stream=false with Accept: text/event-stream will return error");
102+
System.out.println(
103+
" Use stream=true for streaming, or omit Accept header for non-streaming");
104+
}
105+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2024-2026 the original author or authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
server:
15+
port: 8080
16+
17+
logging:
18+
level:
19+
root: INFO
20+
io.agentscope: INFO
21+
22+
agentscope:
23+
# Choose model provider - here we use DashScope as an example.
24+
model:
25+
provider: dashscope
26+
27+
dashscope:
28+
enabled: true
29+
api-key: ${DASHSCOPE_API_KEY:YOUR_DASHSCOPE_API_KEY_HERE}
30+
model-name: qwen3-max
31+
stream: true
32+
33+
agent:
34+
enabled: true
35+
name: "ChatCompletionsAgent"
36+
sys-prompt: |
37+
You are a helpful assistant built with AgentScope Java.
38+
Answer in Chinese when the user speaks Chinese, otherwise answer in English.
39+
max-iters: 8
40+
41+
chat-completions:
42+
enabled: true
43+
base-path: /v1/chat/completions

agentscope-examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<module>werewolf-hitl</module>
4343
<module>model-request-compression</module>
4444
<module>boba-tea-shop</module>
45+
<module>chat-completions-web</module>
4546
<module>hitl-chat</module>
4647
</modules>
4748

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2026 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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
<parent>
23+
<groupId>io.agentscope</groupId>
24+
<artifactId>agentscope-extensions</artifactId>
25+
<version>${revision}</version>
26+
<relativePath>../pom.xml</relativePath>
27+
</parent>
28+
<artifactId>agentscope-extensions-chat-completions-web</artifactId>
29+
30+
<name>AgentScope Java - Extensions - Chat Completions Web</name>
31+
<description>AgentScope Extensions - Chat Completions Web Protocol Support</description>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>io.agentscope</groupId>
36+
<artifactId>agentscope-core</artifactId>
37+
<scope>provided</scope>
38+
<optional>true</optional>
39+
</dependency>
40+
<!-- Reactor Core for Flux/Mono support -->
41+
<!-- Required by ChatCompletionsStreamingAdapter which uses Flux<T> -->
42+
<!-- This is a compile-time dependency since agent.stream() returns Flux<Event> -->
43+
<dependency>
44+
<groupId>io.projectreactor</groupId>
45+
<artifactId>reactor-core</artifactId>
46+
<scope>provided</scope>
47+
<optional>true</optional>
48+
</dependency>
49+
50+
<!-- Test dependencies -->
51+
<dependency>
52+
<groupId>io.projectreactor</groupId>
53+
<artifactId>reactor-test</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.mockito</groupId>
58+
<artifactId>mockito-core</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-junit-jupiter</artifactId>
64+
<scope>test</scope>
65+
<exclusions>
66+
<exclusion>
67+
<groupId>org.junit.jupiter</groupId>
68+
<artifactId>junit-jupiter-api</artifactId>
69+
</exclusion>
70+
</exclusions>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.junit.jupiter</groupId>
74+
<artifactId>junit-jupiter</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
</project>

0 commit comments

Comments
 (0)