Repository navigation
Expand file tree
/
Copy pathChatService.java
More file actions
745 lines (685 loc) · 29.3 KB
/
Copy pathChatService.java
File metadata and controls
745 lines (685 loc) · 29.3 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package cc.projectargus.workbench.chat;
import cc.projectargus.workbench.config.ArgusWorkbenchProperties;
import cc.projectargus.workbench.runtime.CancellationToken;
import cc.projectargus.workbench.runtime.InferenceConcurrencyGate;
import cc.projectargus.workbench.runtime.InferenceConcurrencyGate.PermitLease;
import cc.projectargus.workbench.runtime.InferenceGateway;
import cc.projectargus.workbench.web.dto.ApiErrorDto;
import cc.projectargus.workbench.web.dto.ChatMessageDto;
import cc.projectargus.workbench.web.dto.ChatRequestDto;
import cc.projectargus.workbench.web.dto.ChatResponseDto;
import cc.projectargus.workbench.web.dto.StopResponseDto;
import cc.projectargus.workbench.web.dto.StreamCompleteEventDto;
import cc.projectargus.workbench.web.dto.StreamStartEventDto;
import cc.projectargus.workbench.web.dto.StreamTokenEventDto;
import cc.projectargus.workbench.web.dto.TokenUsageDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
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 dev.langchain4j.model.output.FinishReason;
import dev.langchain4j.model.output.TokenUsage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
/**
* Orchestrates request validation, server-owned {@link SystemMessage} injection, BPE token budgeting
* with sliding turn eviction, bounded concurrency leasing, synchronous chat, and SSE streaming chat.
*/
@Service
public class ChatService implements DisposableBean {
private static final Logger log = LoggerFactory.getLogger(ChatService.class);
private static final Pattern REQUEST_ID_PATTERN = Pattern.compile("^[A-Za-z0-9_-]{1,64}$");
private final InferenceGateway inferenceGateway;
private final InferenceConcurrencyGate concurrencyGate;
private final ArgusWorkbenchProperties properties;
private final ObjectMapper objectMapper;
private final ExecutorService streamingExecutor;
private final ScheduledExecutorService timeoutScheduler;
public ChatService(
InferenceGateway inferenceGateway,
InferenceConcurrencyGate concurrencyGate,
ArgusWorkbenchProperties properties,
ObjectMapper objectMapper
) {
this.inferenceGateway = Objects.requireNonNull(inferenceGateway, "inferenceGateway");
this.concurrencyGate = Objects.requireNonNull(concurrencyGate, "concurrencyGate");
this.properties = Objects.requireNonNull(properties, "properties");
this.objectMapper = Objects.requireNonNull(objectMapper, "objectMapper");
int poolSize = Math.max(1, properties.getMaxConcurrentInference());
AtomicInteger workerIndex = new AtomicInteger(1);
ThreadFactory workerFactory = runnable -> {
Thread t = new Thread(runnable, "argus-inference-worker-" + workerIndex.getAndIncrement());
t.setDaemon(true);
return t;
};
this.streamingExecutor = Executors.newFixedThreadPool(poolSize, workerFactory);
this.timeoutScheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
Thread t = new Thread(runnable, "argus-inference-watchdog");
t.setDaemon(true);
return t;
});
}
/**
* Executes a synchronous multi-turn chat generation.
*/
public ChatResponseDto chat(ChatRequestDto requestDto) {
ensureChatModeAllowsSync();
NormalizedRequest normalized = validateAndNormalizeRequest(requestDto);
CancellationToken cancellationToken = inferenceGateway.createCancellationToken();
try (PermitLease lease = concurrencyGate.acquireOrReject(normalized.requestId(), cancellationToken)) {
BudgetedConversation budgeted = buildBudgetedConversation(normalized);
ChatRequest chatRequest = buildLangChainRequest(budgeted.messages(), normalized);
long startNanos = System.nanoTime();
ScheduledFuture<?> timeoutHandle = scheduleTimeoutWatchdog(lease.cancellationToken());
try {
ChatResponse response = inferenceGateway.chat(chatRequest, lease.cancellationToken());
long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
String replyText = response != null && response.aiMessage() != null && response.aiMessage().text() != null
? response.aiMessage().text()
: "";
TokenUsageDto usageDto = toTokenUsageDto(response != null ? response.tokenUsage() : null, budgeted.promptTokens());
String finishReason = resolveFinishReason(
response != null ? response.finishReason() : null,
lease.cancellationToken().isAborted()
);
log.info(
"Completed sync chat requestId={} promptTokens={} outputTokens={} evicted={} finishReason={} durationMs={}",
normalized.requestId(),
usageDto.inputTokens(),
usageDto.outputTokens(),
budgeted.evictedMessages(),
finishReason,
durationMs
);
return new ChatResponseDto(
normalized.requestId(),
replyText,
finishReason,
usageDto,
budgeted.evictedMessages(),
durationMs
);
} finally {
timeoutHandle.cancel(false);
}
}
}
/**
* Initiates a Server-Sent Events (SSE) streaming chat generation.
*
* <p>Validation, readiness checks, permit acquisition, and token budgeting execute synchronously
* before the {@link SseEmitter} is returned so HTTP 400/429/503 errors are reported cleanly.
*/
public SseEmitter streamChat(ChatRequestDto requestDto) {
ensureChatModeAllowsStream();
NormalizedRequest normalized = validateAndNormalizeRequest(requestDto);
CancellationToken cancellationToken = inferenceGateway.createCancellationToken();
PermitLease lease = concurrencyGate.acquireOrReject(normalized.requestId(), cancellationToken);
final BudgetedConversation budgeted;
final ChatRequest chatRequest;
try {
budgeted = buildBudgetedConversation(normalized);
chatRequest = buildLangChainRequest(budgeted.messages(), normalized);
} catch (Throwable t) {
lease.close();
throw t;
}
long timeoutMs = TimeUnit.SECONDS.toMillis(properties.getInferenceTimeoutSeconds());
SseEmitter emitter = new SseEmitter(timeoutMs);
AtomicBoolean clientDisconnected = new AtomicBoolean(false);
emitter.onTimeout(() -> {
clientDisconnected.set(true);
lease.cancellationToken().abort();
emitter.complete();
});
emitter.onCompletion(() -> {
lease.cancellationToken().abort();
});
emitter.onError(err -> {
clientDisconnected.set(true);
lease.cancellationToken().abort();
});
try {
streamingExecutor.execute(() -> runStreamingJob(
emitter,
lease,
normalized,
budgeted,
chatRequest,
clientDisconnected
));
} catch (RejectedExecutionException rejected) {
lease.close();
throw new IllegalStateException("Streaming executor rejected inference task", rejected);
}
return emitter;
}
/**
* Signals cooperative cancellation to an active request (or all active requests if {@code requestId} is blank).
*/
public StopResponseDto stopGeneration(String requestId) {
if (requestId != null && !requestId.isBlank()) {
String trimmed = requestId.trim();
boolean stopped = concurrencyGate.cancelRequest(trimmed);
return new StopResponseDto(stopped, trimmed);
}
int count = concurrencyGate.cancelAllActive();
return new StopResponseDto(count > 0, null);
}
private void ensureChatModeAllowsSync() {
String mode = properties.getChatMode() == null
? "both"
: properties.getChatMode().trim().toLowerCase(Locale.ROOT);
if (!"both".equals(mode) && !"sync".equals(mode)) {
throw new InvalidChatRequestException(
"sync_chat_disabled",
"Synchronous chat is disabled when ARGUS_CHAT_MODE="
+ mode
+ ". Use POST /api/chat/stream or set ARGUS_CHAT_MODE=both."
);
}
}
private void ensureChatModeAllowsStream() {
String mode = properties.getChatMode() == null
? "both"
: properties.getChatMode().trim().toLowerCase(Locale.ROOT);
if (!"both".equals(mode) && !"stream".equals(mode)) {
throw new InvalidChatRequestException(
"stream_chat_disabled",
"Streaming chat is disabled when ARGUS_CHAT_MODE="
+ mode
+ ". Use POST /api/chat or set ARGUS_CHAT_MODE=both."
);
}
}
private void runStreamingJob(
SseEmitter emitter,
PermitLease lease,
NormalizedRequest normalized,
BudgetedConversation budgeted,
ChatRequest chatRequest,
AtomicBoolean clientDisconnected
) {
long startNanos = System.nanoTime();
ScheduledFuture<?> timeoutHandle = scheduleTimeoutWatchdog(lease.cancellationToken());
StringBuilder accumulatedText = new StringBuilder();
AtomicBoolean terminalEventSent = new AtomicBoolean(false);
try (PermitLease ignored = lease) {
if (!sendSseEvent(
emitter,
"start",
new StreamStartEventDto(
normalized.requestId(),
budgeted.promptTokens(),
budgeted.evictedMessages()
),
clientDisconnected,
lease.cancellationToken()
)) {
return;
}
inferenceGateway.streamChat(
chatRequest,
new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String partialResponse) {
if (partialResponse == null || partialResponse.isEmpty()) {
return;
}
if (clientDisconnected.get()) {
lease.cancellationToken().abort();
return;
}
accumulatedText.append(partialResponse);
sendSseEvent(
emitter,
"token",
new StreamTokenEventDto(partialResponse),
clientDisconnected,
lease.cancellationToken()
);
}
@Override
public void onCompleteResponse(ChatResponse completeResponse) {
if (!terminalEventSent.compareAndSet(false, true)) {
return;
}
long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
boolean cancelled = lease.cancellationToken().isAborted();
String finalText = completeResponse != null
&& completeResponse.aiMessage() != null
&& completeResponse.aiMessage().text() != null
&& !completeResponse.aiMessage().text().isEmpty()
? completeResponse.aiMessage().text()
: accumulatedText.toString();
TokenUsageDto usageDto = toTokenUsageDto(
completeResponse != null ? completeResponse.tokenUsage() : null,
budgeted.promptTokens()
);
String finishReason = resolveFinishReason(
completeResponse != null ? completeResponse.finishReason() : null,
cancelled
);
log.info(
"Completed stream chat requestId={} promptTokens={} outputTokens={} evicted={} finishReason={} cancelled={} durationMs={}",
normalized.requestId(),
usageDto.inputTokens(),
usageDto.outputTokens(),
budgeted.evictedMessages(),
finishReason,
cancelled,
durationMs
);
sendSseEvent(
emitter,
"complete",
new StreamCompleteEventDto(
normalized.requestId(),
finalText,
finishReason,
cancelled,
usageDto,
budgeted.evictedMessages(),
durationMs
),
clientDisconnected,
lease.cancellationToken()
);
completeEmitterQuietly(emitter);
}
@Override
public void onError(Throwable error) {
handleStreamFailure(
emitter,
lease.cancellationToken(),
normalized,
budgeted,
accumulatedText,
startNanos,
terminalEventSent,
clientDisconnected,
error
);
}
},
lease.cancellationToken()
);
} catch (Throwable t) {
handleStreamFailure(
emitter,
lease.cancellationToken(),
normalized,
budgeted,
accumulatedText,
startNanos,
terminalEventSent,
clientDisconnected,
t
);
} finally {
timeoutHandle.cancel(false);
}
}
private void handleStreamFailure(
SseEmitter emitter,
CancellationToken cancellationToken,
NormalizedRequest normalized,
BudgetedConversation budgeted,
StringBuilder accumulatedText,
long startNanos,
AtomicBoolean terminalEventSent,
AtomicBoolean clientDisconnected,
Throwable error
) {
if (!terminalEventSent.compareAndSet(false, true)) {
return;
}
long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
if (cancellationToken.isAborted()) {
log.info(
"Stream chat cancelled requestId={} durationMs={}",
normalized.requestId(),
durationMs
);
TokenUsageDto usage = new TokenUsageDto(budgeted.promptTokens(), 0, budgeted.promptTokens());
sendSseEvent(
emitter,
"complete",
new StreamCompleteEventDto(
normalized.requestId(),
accumulatedText.toString(),
"CANCELLED",
true,
usage,
budgeted.evictedMessages(),
durationMs
),
clientDisconnected,
cancellationToken
);
completeEmitterQuietly(emitter);
return;
}
log.error("Streaming inference error for requestId={}", normalized.requestId(), error);
sendSseEvent(
emitter,
"error",
new ApiErrorDto(
"inference_failed",
"Local inference encountered an internal runtime error during generation."
),
clientDisconnected,
cancellationToken
);
completeEmitterQuietly(emitter);
}
private boolean sendSseEvent(
SseEmitter emitter,
String eventName,
Object payload,
AtomicBoolean clientDisconnected,
CancellationToken cancellationToken
) {
if (clientDisconnected.get()) {
cancellationToken.abort();
return false;
}
try {
String json = objectMapper.writeValueAsString(payload);
emitter.send(
SseEmitter.event()
.name(eventName)
.data(json, MediaType.APPLICATION_JSON)
);
return true;
} catch (IOException | IllegalStateException sendErr) {
clientDisconnected.set(true);
cancellationToken.abort();
return false;
}
}
private static void completeEmitterQuietly(SseEmitter emitter) {
try {
emitter.complete();
} catch (Throwable ignored) {
}
}
private ScheduledFuture<?> scheduleTimeoutWatchdog(CancellationToken token) {
return timeoutScheduler.schedule(
token::abort,
properties.getInferenceTimeoutSeconds(),
TimeUnit.SECONDS
);
}
NormalizedRequest validateAndNormalizeRequest(ChatRequestDto dto) {
if (dto == null) {
throw new InvalidChatRequestException(
"invalid_request",
"Request body must be a non-empty JSON object."
);
}
boolean hasMessages = dto.messages() != null && !dto.messages().isEmpty();
boolean hasPrompt = dto.prompt() != null && !dto.prompt().isBlank();
if (hasMessages && hasPrompt) {
throw new InvalidChatRequestException(
"ambiguous_request",
"Provide either 'messages' or 'prompt', not both."
);
}
if (!hasMessages && !hasPrompt) {
throw new InvalidChatRequestException(
"empty_messages",
"Request must include a non-empty 'messages' array or a non-blank 'prompt' string."
);
}
List<ChatMessageDto> rawTurns = hasMessages
? dto.messages()
: List.of(new ChatMessageDto("user", dto.prompt()));
if (rawTurns.size() > properties.getMaxHistoryMessages()) {
throw new InvalidChatRequestException(
"too_many_messages",
"Conversation contains "
+ rawTurns.size()
+ " messages, exceeding the configured limit of "
+ properties.getMaxHistoryMessages()
+ "."
);
}
List<ChatMessage> validatedHistory = new ArrayList<>(rawTurns.size());
int totalChars = 0;
for (int i = 0; i < rawTurns.size(); i++) {
ChatMessageDto turn = rawTurns.get(i);
if (turn == null) {
throw new InvalidChatRequestException(
"invalid_message",
"Message at index " + i + " must not be null."
);
}
if (turn.role() == null || turn.role().isBlank()) {
throw new InvalidChatRequestException(
"invalid_message_role",
"Message at index " + i + " is missing a role."
);
}
if (turn.content() == null || turn.content().isBlank()) {
throw new InvalidChatRequestException(
"empty_message_content",
"Message at index " + i + " must contain non-blank text."
);
}
String content = turn.content();
if (content.length() > properties.getMaxPromptChars()) {
throw new InvalidChatRequestException(
"prompt_too_long",
"Message at index "
+ i
+ " ("
+ content.length()
+ " chars) exceeds the maximum allowed size of "
+ properties.getMaxPromptChars()
+ " characters."
);
}
totalChars += content.length();
if (totalChars > properties.getMaxPromptChars()) {
throw new InvalidChatRequestException(
"prompt_too_long",
"Total conversation character length ("
+ totalChars
+ " chars) exceeds the maximum allowed size of "
+ properties.getMaxPromptChars()
+ " characters."
);
}
String normalizedRole = turn.role().trim().toLowerCase(Locale.ROOT);
switch (normalizedRole) {
case "user" -> validatedHistory.add(UserMessage.from(content));
case "assistant" -> validatedHistory.add(AiMessage.from(content));
case "system", "tool" -> throw new InvalidChatRequestException(
"forbidden_message_role",
"Client messages may only use 'user' or 'assistant' roles; system instructions are owned by the server."
);
default -> throw new InvalidChatRequestException(
"invalid_message_role",
"Unsupported message role '" + turn.role() + "'. Allowed roles: 'user', 'assistant'."
);
}
}
ChatMessage lastMessage = validatedHistory.get(validatedHistory.size() - 1);
if (!(lastMessage instanceof UserMessage)) {
throw new InvalidChatRequestException(
"last_message_must_be_user",
"The final message in the conversation must have role 'user'."
);
}
float effectiveTemperature = properties.getTemperature();
if (dto.temperature() != null) {
float candidateTemp = dto.temperature();
if (Float.isNaN(candidateTemp) || Float.isInfinite(candidateTemp) || candidateTemp < 0.0f || candidateTemp > 2.0f) {
throw new InvalidChatRequestException(
"invalid_temperature",
"temperature must be between 0.0 and 2.0."
);
}
effectiveTemperature = candidateTemp;
}
int effectiveMaxTokens = properties.getMaxOutputTokens();
if (dto.maxOutputTokens() != null) {
int candidateMax = dto.maxOutputTokens();
if (candidateMax < 1 || candidateMax > properties.getMaxOutputTokens()) {
throw new InvalidChatRequestException(
"invalid_max_output_tokens",
"maxOutputTokens must be between 1 and " + properties.getMaxOutputTokens() + "."
);
}
effectiveMaxTokens = candidateMax;
}
String resolvedRequestId;
if (dto.requestId() != null && !dto.requestId().isBlank()) {
String candidateId = dto.requestId().trim();
if (!REQUEST_ID_PATTERN.matcher(candidateId).matches()) {
throw new InvalidChatRequestException(
"invalid_request_id",
"requestId must match ^[A-Za-z0-9_-]{1,64}$."
);
}
resolvedRequestId = candidateId;
} else {
resolvedRequestId = UUID.randomUUID().toString();
}
return new NormalizedRequest(
resolvedRequestId,
List.copyOf(validatedHistory),
effectiveTemperature,
effectiveMaxTokens
);
}
BudgetedConversation buildBudgetedConversation(NormalizedRequest normalized) {
int maxPromptTokenBudget = properties.getContextLength() - normalized.maxOutputTokens();
if (maxPromptTokenBudget <= 0) {
throw new InvalidChatRequestException(
"context_length_exceeded",
"Requested maxOutputTokens ("
+ normalized.maxOutputTokens()
+ ") leaves no prompt token budget within contextLength ("
+ properties.getContextLength()
+ ")."
);
}
SystemMessage systemMessage = SystemMessage.from(properties.getSystemPrompt());
List<ChatMessage> workingHistory = new ArrayList<>(normalized.history());
int evictedCount = 0;
while (true) {
List<ChatMessage> candidate = new ArrayList<>(workingHistory.size() + 1);
candidate.add(systemMessage);
candidate.addAll(workingHistory);
int tokenCount = inferenceGateway.countTokens(candidate);
if (tokenCount <= maxPromptTokenBudget) {
return new BudgetedConversation(List.copyOf(candidate), tokenCount, evictedCount);
}
if (workingHistory.size() <= 1) {
throw new InvalidChatRequestException(
"context_length_exceeded",
"Prompt requires "
+ tokenCount
+ " tokens, which exceeds the available prompt budget of "
+ maxPromptTokenBudget
+ " tokens (contextLength="
+ properties.getContextLength()
+ ", maxOutputTokens="
+ normalized.maxOutputTokens()
+ "). Shorten the prompt."
);
}
// Evict oldest turn; if the next remaining turn is an orphaned assistant reply, evict it too
workingHistory.remove(0);
evictedCount++;
while (workingHistory.size() > 1 && workingHistory.get(0) instanceof AiMessage) {
workingHistory.remove(0);
evictedCount++;
}
}
}
private static ChatRequest buildLangChainRequest(
List<ChatMessage> messages,
NormalizedRequest normalized
) {
return ChatRequest.builder()
.messages(messages)
.parameters(
ChatRequestParameters.builder()
.temperature((double) normalized.temperature())
.maxOutputTokens(normalized.maxOutputTokens())
.build()
)
.build();
}
private static TokenUsageDto toTokenUsageDto(TokenUsage usage, int fallbackPromptTokens) {
if (usage == null) {
return new TokenUsageDto(fallbackPromptTokens, 0, fallbackPromptTokens);
}
int inTokens = usage.inputTokenCount() != null ? usage.inputTokenCount() : fallbackPromptTokens;
int outTokens = usage.outputTokenCount() != null ? usage.outputTokenCount() : 0;
int total = usage.totalTokenCount() != null ? usage.totalTokenCount() : (inTokens + outTokens);
return new TokenUsageDto(inTokens, outTokens, total);
}
private static String resolveFinishReason(FinishReason reason, boolean cancelled) {
if (cancelled) {
return "CANCELLED";
}
if (reason == null) {
return "STOP";
}
return reason.name();
}
@Override
public void destroy() {
concurrencyGate.cancelAllActive();
timeoutScheduler.shutdownNow();
streamingExecutor.shutdown();
try {
if (!streamingExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
streamingExecutor.shutdownNow();
}
} catch (InterruptedException ie) {
streamingExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
}
record NormalizedRequest(
String requestId,
List<ChatMessage> history,
float temperature,
int maxOutputTokens
) {}
record BudgetedConversation(
List<ChatMessage> messages,
int promptTokens,
int evictedMessages
) {}
}