forked from agentscope-ai/agentscope-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatCompletionsWebApplication.java
More file actions
188 lines (183 loc) · 7.63 KB
/
ChatCompletionsWebApplication.java
File metadata and controls
188 lines (183 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2024-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.agentscope.examples.chatcompletions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Minimal Spring Boot application demonstrating how to use
* {@code agentscope-chat-completions-web-starter}.
*
* <p>After starting this app, you can call:
*
* <p>Non-streaming request (stream=false or omitted):
*
* <pre>
* curl -X POST http://localhost:8080/v1/chat/completions \\
* -H 'Content-Type: application/json' \\
* -d '{
* "model": "qwen3-max",
* "stream": false,
* "messages": [
* { "role": "user", "content": "Hello, can you briefly introduce AgentScope Java?" }
* ]
* }'
* </pre>
*
* <p>Streaming request (stream=true, Accept header is optional):
*
* <pre>
* curl -N -X POST http://localhost:8080/v1/chat/completions \\
* -H 'Content-Type: application/json' \\
* -d '{
* "model": "qwen3-max",
* "stream": true,
* "messages": [
* { "role": "user", "content": "Please provide a streamed answer: Describe AgentScope Java in three sentences." }
* ]
* }'
* </pre>
*
* <p><b>Important:</b> If stream=false but Accept: text/event-stream is set, the request will be
* rejected with an error for consistency. Use stream=true for streaming, or omit the Accept header
* for non-streaming.
*/
@SpringBootApplication
public class ChatCompletionsWebApplication {
public static void main(String[] args) {
SpringApplication.run(ChatCompletionsWebApplication.class, args);
printStartupInfo();
}
private static void printStartupInfo() {
System.out.println("\n=== chat completions API spring web Example Application Started ===");
System.out.println("\nExample curl command:");
System.out.println("\nNon-streaming chat completion (stream=false or omitted).\n");
System.out.println(
"""
curl -X POST http://localhost:8080/v1/chat/completions \\
-H 'Content-Type: application/json' \\
-d '{
"model": "qwen3-max",
"stream": false,
"messages": [
{ "role": "user", "content": "Please provide a Non streamed answer: Describe AgentScope Java in three sentences." }
]
}'
""");
System.out.println("\nNote: stream parameter can be omitted (defaults to false)");
System.out.println("===================================================");
System.out.println("\nStreaming chat completion (stream=true).\n");
System.out.println(
"""
curl -N -X POST http://localhost:8080/v1/chat/completions \\
-H 'Content-Type: application/json' \\
-d '{
"model": "qwen3-max",
"stream": true,
"messages": [
{ "role": "user", "content": "Please provide a streamed answer: Describe AgentScope Java in three sentences." }
]
}'
""");
System.out.println("\nNote: Accept: text/event-stream header is optional when stream=true");
System.out.println("===================================================");
System.out.println("\nChat completion with Tool Schema (Tool Suspend).\n");
System.out.println(
"""
curl -N -X POST http://localhost:8080/v1/chat/completions \\
-H 'Content-Type: application/json' \\
-d '{
"model": "qwen3-max",
"stream": true,
"messages": [
{ "role": "user", "content": "What is the weather in Hangzhou?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name"
}
},
"required": ["city"]
}
}
}
]
}'
""");
System.out.println("\nNote: Tools are registered as schema-only tools, triggering tool");
System.out.println(" suspension. The response will include tool_calls with");
System.out.println(" finish_reason='tool_calls'. Execute tools externally, then");
System.out.println(" send tool results in the next request:");
System.out.println(
"""
curl -N -X POST http://localhost:8080/v1/chat/completions \\
-H 'Content-Type: application/json' \\
-d '{
"model": "qwen3-max",
"stream": true,
"messages": [
{ "role": "user", "content": "What is the weather in Hangzhou?" },
{
"role": "assistant",
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\\"city\\":\\"Hangzhou\\"}"
}
}]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"name": "get_weather",
"content": "Sunny, 25°C"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "The city name" }
},
"required": ["city"]
}
}
}
]
}'
""");
System.out.println("===================================================");
System.out.println(
"\n⚠️ Important: stream=false with Accept: text/event-stream will return error");
System.out.println(
" Use stream=true for streaming, or omit Accept header for non-streaming");
}
}