Closed big-mouth-cn closed 1 month ago
新增 ChatMessageAccumulatorWrapper,以便于能够在使用chatCompletion时使用原始 chunk 数据。这一需求主要适用于chat API的转发场景。
Flowable<ChatCompletionChunk> streamedChatCompletion = openAiService.streamChatCompletion(requestBody); AssistantMessage accumulatedMessage = openAiService.mapStreamToAccumulatorWrapper(streamedChatCompletion) .doOnNext((chatMessageAccumulatorWrapper -> { ChatMessageAccumulator chatMessageAccumulator = chatMessageAccumulatorWrapper.getChatMessageAccumulator(); if (!chatMessageAccumulator.isFunctionCall()) { ChatCompletionChunk chatCompletionChunk = chatMessageAccumulatorWrapper.getChatCompletionChunk(); String source = chatCompletionChunk.getSource(); byte[] bytes = source.getBytes(StandardCharsets.UTF_8); if (null != bytes) { sseEmitter.send(SseEmitter.event().data(bytes, MediaType.APPLICATION_JSON_UTF8)); } } })) .doOnError(throwable -> { sseEmitter.completeWithError(throwable); log.error("streamChatCompletion error", throwable); }) .lastElement() .blockingGet() .getChatMessageAccumulator() .getAccumulatedMessage();
新增 ChatMessageAccumulatorWrapper,以便于能够在使用chatCompletion时使用原始 chunk 数据。这一需求主要适用于chat API的转发场景。