spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Calling Flux<ChatResponse>stream() returns unordered results

Open hetao0403 opened this issue 1 year ago • 11 comments
trafficstars

Bug description Calling Flux<ChatResponse>stream() returns unordered results Environment Spring AI version 1.0.0-SNAPSHOT , Java version 17,openai

hetao0403 avatar Apr 30 '24 02:04 hetao0403

Funny never spotted, but I've used a lot the streams... Which client?

Grogdunn avatar Apr 30 '24 10:04 Grogdunn

@Grogdunn

@RestController
@AllArgsConstructor
@RequestMapping("v1/ai")
public class SimpleAiController {

	private final OpenAiChatClient chatClient;

	@GetMapping("/generateStream")
	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
		Prompt prompt = new Prompt(new UserMessage(message));
		return chatClient.stream(prompt);
	}
}

Error:

org.springframework.ai.openai.api.OpenAiApi#chatCompletionStream

image

There's an extra "data:" here.

qq418745 avatar May 01 '24 15:05 qq418745

I reviewed the source code at https://github.com/openai/openai-node and saw that the strings have been processed, with 'data' being used as a 'fieldname'.

image

qq418745 avatar May 01 '24 16:05 qq418745

org.springframework.ai.openai.api.OpenAiApiIT#chatCompletionStream

I noticed that in the automated testing section of the spring-ai test code, despite the tests should have already been passed successfully, I still encountered issues when executing the test code myself, and it failed to pass the tests. This is very confusing, as logically, there should be no problems.

qq418745 avatar May 01 '24 17:05 qq418745

I've tested against OpenAI service and cannot reproduce the wrong behaviour... can you make a test?

Grogdunn avatar May 02 '24 08:05 Grogdunn

@Grogdunn
"/v1/chat/completions" stream mode.

I am not directly using the OpenAI API, but rather accessing it through a proxy service. Debugging revealed that Spring WebFlux selects the appropriate MessageReader based on the "content-type". Since the "content-type: text/plain" did not engage the ServerSentEventHttpMessageReader, it failed to process the "data:" prefix. I suspect this is most likely the case, and I am still confirming this issue.

qq418745 avatar May 03 '24 01:05 qq418745

"/v1/chat/completions" stream mode.

The response header from the proxy is "content-type: text/plain; charset=utf-8", but it should correctly be "text/event-stream".

qq418745 avatar May 03 '24 01:05 qq418745

@Grogdunn My issue has been resolved; it was because of the response header.

qq418745 avatar May 03 '24 04:05 qq418745

Funny never spotted, but I've used a lot the streams... Which client?

OpenAiChatClient,3.5 and 4

hetao0403 avatar May 06 '24 03:05 hetao0403

I've tested against OpenAI service and cannot reproduce the wrong behaviour... can you make a test? Yes, sometimes it happens, not necessarily

hetao0403 avatar May 06 '24 03:05 hetao0403

I wonder if it is in the chatCompletionStream method of the OpenAiApi class that this is used:

.floatMap((mono) -> {
    return mono;
})

It might be worth trying to change this method to:

.concatMap((mono) -> {
    return mono;
})

sennoy11012 avatar May 11 '24 05:05 sennoy11012

I have a similar problem

this is my log:

prompt is "1+1=" expect is "1+1=2" actual is "12+1="(wrong)

2024-05-21 17:05:50.035 |  INFO 39472 | reactor-http-nio-4 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content() result()]
2024-05-21 17:05:50.036 |  INFO 39472 | task-8 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content(1) result(1)]
2024-05-21 17:05:50.037 |  INFO 39472 | task-9 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content(2) result(12)]
2024-05-21 17:05:50.037 |  INFO 39472 | task-10 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content(+) result(12+)]
2024-05-21 17:05:50.038 |  INFO 39472 | task-11 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content(1) result(12+1)]
2024-05-21 17:05:50.038 |  INFO 39472 | task-12 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content() result(12+1)]
2024-05-21 17:05:50.039 |  INFO 39472 | task-13 [TID: N/A] c.i.y.m.a.s.impl.AiChatServiceImpl       | [xxx][content(=) result(12+1=)]

YunaiV avatar May 21 '24 11:05 YunaiV

Bug description Calling Fluxstream() returns unordered results Environment Spring AI version 1.0.0-SNAPSHOT , Java version 17,openai

// 注意:Schedulers.immediate() 目的是,避免默认 Schedulers.parallel() 并发消费 chunk 导致 SSE 响应前端会乱序问题 this work for me

YunaiV avatar May 21 '24 11:05 YunaiV

Bug description Calling Fluxstream() returns unordered results Environment Spring AI version 1.0.0-SNAPSHOT , Java version 17,openai

// 注意:Schedulers.immediate() 目的是,避免默认 Schedulers.parallel() 并发消费 chunk 导致 SSE 响应前端会乱序问题 this work for me

return streamResponse.publishOn(Schedulers.immediate()).map(chunk -> {

YunaiV avatar May 21 '24 11:05 YunaiV

The LLM expecially the generative one, are not good in math. All model currently in production respond with something "similar" a good response, but not the correct response.

Some models (gemini 1.5 pro I think) reach more accuracy, but not correctness at 100%. So :warning: BEWARE!

Grogdunn avatar May 21 '24 12:05 Grogdunn

The LLM expecially the generative one, are not good in math. All model currently in production respond with something "similar" a good response, but not the correct response.

Some models (gemini 1.5 pro I think) reach more accuracy, but not correctness at 100%. So ⚠️ BEWARE!

Thank you.

My purpose is mainly to give llm a simple question to test whether spring webflux + Spring-AI will have sse out-of-order problems

YunaiV avatar May 21 '24 12:05 YunaiV

Bug description Calling Fluxstream() returns unordered results Environment Spring AI version 1.0.0-SNAPSHOT , Java version 17,openai

// 注意:Schedulers.immediate() 目的是,避免默认 Schedulers.parallel() 并发消费 chunk 导致 SSE 响应前端会乱序问题 this work for me

thanks

hetao0403 avatar May 24 '24 10:05 hetao0403

Bug description Calling Fluxstream() returns unordered results Environment Spring AI version 1.0.0-SNAPSHOT , Java version 17,openai

// 注意:Schedulers.immediate() 目的是,避免默认 Schedulers.parallel() 并发消费 chunk 导致 SSE 响应前端会乱序问题 this work for me

thanks

hetao0403 avatar May 24 '24 10:05 hetao0403

Flux<ChatResponse> stream = chatClient.stream(prompt); return stream.publishOn(Schedulers.immediate()).map(chunk -> chunk);

hetao0403 avatar May 27 '24 09:05 hetao0403