Repository navigation
Expand file tree
/
Copy pathNativeModelIntegrationTest.java
More file actions
266 lines (233 loc) · 11.6 KB
/
Copy pathNativeModelIntegrationTest.java
File metadata and controls
266 lines (233 loc) · 11.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cc.projectargus.workbench.runtime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import cc.projectargus.workbench.chat.ChatService;
import cc.projectargus.workbench.chat.InvalidChatRequestException;
import cc.projectargus.workbench.config.ArgusLangChain4jConfig;
import cc.projectargus.workbench.config.ArgusWorkbenchProperties;
import cc.projectargus.workbench.web.dto.ChatRequestDto;
import cc.projectargus.workbench.web.dto.ChatResponseDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.TokenCountEstimator;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.request.ChatRequestParameters;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Opt-in end-to-end native integration test that loads a real GGUF model via {@code libargus-jni}
* and exercises token counting, synchronous chat, multi-token streaming, live model recovery,
* and deterministic teardown.
*
* <p>Skipped automatically when {@code ARGUS_CHAT_MODEL_PATH} (or {@code -Dargus.chat.model-path})
* is not provided.
*/
class NativeModelIntegrationTest {
@TempDir
Path tempDir;
private static Path resolveConfiguredModelPath() {
String configuredPath = System.getProperty("argus.chat.model-path");
if (configuredPath == null || configuredPath.isBlank()) {
configuredPath = System.getenv("ARGUS_CHAT_MODEL_PATH");
}
Assumptions.assumeTrue(
configuredPath != null && !configuredPath.isBlank(),
"Skipping native GGUF integration test: set ARGUS_CHAT_MODEL_PATH or -Dargus.chat.model-path=/path/to/model.gguf to run."
);
Path modelPath = Path.of(configuredPath.trim());
Assumptions.assumeTrue(
Files.isRegularFile(modelPath),
"Skipping native GGUF integration test: configured model file does not exist: " + modelPath
);
return modelPath;
}
@Test
void loadsRealGgufModelExecutesSyncAndStreamingChatAndClosesCleanly() throws Exception {
Path modelPath = resolveConfiguredModelPath();
ArgusWorkbenchProperties props = new ArgusWorkbenchProperties();
props.setModelPath(modelPath.toString());
props.setContextLength(512);
props.setMaxOutputTokens(8);
props.setGpuLayers(0);
props.setTemperature(0.0f);
props.setFailFastStartup(true);
InferenceConcurrencyGate gate = new InferenceConcurrencyGate(props);
ArgusRuntimeManager runtimeManager = new ArgusRuntimeManager(props, gate);
ChatService chatService = new ChatService(runtimeManager, gate, props, new ObjectMapper());
try {
runtimeManager.afterPropertiesSet();
assertThat(runtimeManager.isReady()).isTrue();
RuntimeStatusSnapshot status = runtimeManager.getStatusSnapshot(0);
assertThat(status.status()).isEqualTo("ready");
assertThat(status.chatEnabled()).isTrue();
assertThat(status.binding()).isEqualTo(ArgusRuntimeManager.detectActiveBinding());
assertThat(status.modelName()).isEqualTo(modelPath.getFileName().toString());
// 1. Verify native BPE token estimation (including raw text vs formatted chat message via @Bean adapter)
ArgusLangChain4jConfig langChainConfig = new ArgusLangChain4jConfig();
TokenCountEstimator estimatorBean = langChainConfig.argusTokenCountEstimator(runtimeManager);
ChatModel chatBean = langChainConfig.argusChatModel(runtimeManager);
int rawHelloTokens = estimatorBean.estimateTokenCountInText("Hello");
int formattedHelloTokens = estimatorBean.estimateTokenCountInMessage(UserMessage.from("Hello"));
assertThat(rawHelloTokens).isPositive();
assertThat(formattedHelloTokens).isGreaterThan(rawHelloTokens);
assertThat(
chatBean.doChat(
ChatRequest.builder()
.messages(UserMessage.from("Hi"))
.parameters(ChatRequestParameters.builder().temperature(0.0).maxOutputTokens(2).build())
.build()
).aiMessage().text()
).isNotEmpty();
int tokens = runtimeManager.countTokens(List.of(
SystemMessage.from(props.getSystemPrompt()),
UserMessage.from("Hello")
));
assertThat(tokens).isPositive();
// 2. Verify synchronous chat through ChatService
ChatResponseDto syncResponse = chatService.chat(
new ChatRequestDto(null, "Hello", 0.0f, 4, "native-sync-1")
);
assertThat(syncResponse.requestId()).isEqualTo("native-sync-1");
assertThat(syncResponse.usage().inputTokens()).isPositive();
assertThat(syncResponse.usage().outputTokens()).isEqualTo(4);
assertThat(syncResponse.finishReason()).isNotBlank();
// 3. Verify incremental streaming chat with CancellationToken
List<String> fragments = new ArrayList<>();
CompletableFuture<ChatResponse> streamDone = new CompletableFuture<>();
ChatRequest streamReq = ChatRequest.builder()
.messages(UserMessage.from("Hello"))
.parameters(
ChatRequestParameters.builder()
.temperature(0.0)
.maxOutputTokens(4)
.build()
)
.build();
try (CancellationToken cancelToken = runtimeManager.createCancellationToken()) {
runtimeManager.streamChat(
streamReq,
new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String partialResponse) {
fragments.add(partialResponse);
}
@Override
public void onCompleteResponse(ChatResponse completeResponse) {
streamDone.complete(completeResponse);
}
@Override
public void onError(Throwable error) {
streamDone.completeExceptionally(error);
}
},
cancelToken
);
}
ChatResponse completed = streamDone.get(30, TimeUnit.SECONDS);
assertThat(completed).isNotNull();
assertThat(completed.tokenUsage().inputTokenCount()).isPositive();
assertThat(fragments).hasSizeGreaterThanOrEqualTo(2);
assertThat(String.join("", fragments)).isEqualTo(completed.aiMessage().text());
} finally {
chatService.destroy();
runtimeManager.destroy();
assertThat(runtimeManager.isReady()).isFalse();
}
}
@Test
void recoversLiveWhenModelFileArrivesAfterDegradedStartup() throws Exception {
Path sourceModel = resolveConfiguredModelPath();
Path delayedDestination = tempDir.resolve("delayed-arrival.gguf");
ArgusWorkbenchProperties props = new ArgusWorkbenchProperties();
props.setModelPath(delayedDestination.toString());
props.setContextLength(256);
props.setMaxOutputTokens(4);
props.setGpuLayers(0);
props.setTemperature(0.0f);
props.setFailFastStartup(false);
InferenceConcurrencyGate gate = new InferenceConcurrencyGate(props);
ArgusRuntimeManager runtimeManager = new ArgusRuntimeManager(props, gate);
try {
runtimeManager.afterPropertiesSet();
assertThat(runtimeManager.isReady()).isFalse();
assertThat(runtimeManager.getStatusSnapshot(0).status()).isEqualTo("not_ready");
// Simulate user finishing model download to the configured path
Files.copy(sourceModel, delayedDestination, StandardCopyOption.REPLACE_EXISTING);
assertThat(runtimeManager.isReady()).isTrue();
RuntimeStatusSnapshot recovered = runtimeManager.getStatusSnapshot(0);
assertThat(recovered.status()).isEqualTo("ready");
assertThat(recovered.chatEnabled()).isTrue();
assertThat(recovered.modelName()).isEqualTo("delayed-arrival.gguf");
} finally {
runtimeManager.destroy();
}
}
@Test
void streamOnlyChatModeAllocatesSingleStreamingAdapterAndRejectsSyncRequests() throws Exception {
Path modelPath = resolveConfiguredModelPath();
ArgusWorkbenchProperties props = new ArgusWorkbenchProperties();
props.setModelPath(modelPath.toString());
props.setChatMode("stream");
props.setContextLength(256);
props.setMaxOutputTokens(4);
props.setGpuLayers(0);
props.setTemperature(0.0f);
props.setFailFastStartup(true);
InferenceConcurrencyGate gate = new InferenceConcurrencyGate(props);
ArgusRuntimeManager runtimeManager = new ArgusRuntimeManager(props, gate);
ChatService chatService = new ChatService(runtimeManager, gate, props, new ObjectMapper());
try {
runtimeManager.afterPropertiesSet();
assertThat(runtimeManager.isReady()).isTrue();
assertThatThrownBy(() -> chatService.chat(new ChatRequestDto(null, "Hello", 0.0f, 4, "sync-disabled-1")))
.isInstanceOf(InvalidChatRequestException.class)
.extracting(ex -> ((InvalidChatRequestException) ex).getErrorCode())
.isEqualTo("sync_chat_disabled");
CompletableFuture<ChatResponse> streamDone = new CompletableFuture<>();
ChatRequest streamReq = ChatRequest.builder()
.messages(UserMessage.from("Hello"))
.parameters(
ChatRequestParameters.builder()
.temperature(0.0)
.maxOutputTokens(4)
.build()
)
.build();
try (CancellationToken cancelToken = runtimeManager.createCancellationToken()) {
runtimeManager.streamChat(
streamReq,
new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String partialResponse) {
}
@Override
public void onCompleteResponse(ChatResponse completeResponse) {
streamDone.complete(completeResponse);
}
@Override
public void onError(Throwable error) {
streamDone.completeExceptionally(error);
}
},
cancelToken
);
}
assertThat(streamDone.get(30, TimeUnit.SECONDS)).isNotNull();
} finally {
chatService.destroy();
runtimeManager.destroy();
}
}
}