这是indexloc提供的服务,不要输入任何密码
Skip to content

新增 ChatMessageAccumulatorWrapper,以便于能够在使用chatCompletion时使用原始 chunk 数据… #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2024

Conversation

big-mouth-cn
Copy link

新增 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();

@Lambdua Lambdua merged commit d435a8e into Lambdua:main Oct 20, 2024
@big-mouth-cn big-mouth-cn deleted the wrapper-chunk branch November 6, 2024 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants