spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/index.html
Apache License 2.0
2.89k stars 721 forks source link

Uneffective RetryTemplate usage on openai chat stream request #847

Open RikJux opened 3 months ago

RikJux commented 3 months ago

Bug description The OpenAiChatModel method stream calls the chatCompletionStream inside the execute of the RetryTemplate:

`public Flux<ChatResponse> stream(Prompt prompt) {
        OpenAiApi.ChatCompletionRequest request = this.createRequest(prompt, true);
        return (Flux)this.retryTemplate.execute((ctx) -> {
            Flux<OpenAiApi.ChatCompletionChunk> completionChunks = this.openAiApi.chatCompletionStream(request);
            ConcurrentHashMap<String, String> roleMap = new ConcurrentHashMap();
            return completionChunks.map((chunk) -> {
                return this.chunkToChatCompletion(chunk);
            }).switchMap((cc) -> {***...***`

However, the retry template does nothing, as the return object is a FluxMap: no error ever arises at this point

Environment I tried this out on the https://github.com/rd-1-2022/ai-openai-helloworld project

Steps to reproduce Run this test in the https://github.com/rd-1-2022/ai-openai-helloworld project. To try out the behavior in presence of an error, you may provide the wrong api key. @SpringBootTest(classes = OpenAiAutoConfiguration.class) public class OpenAiCallTest {

@Autowired
OpenAiChatModel openAiChatModel;

@Test
public void test(){
    System.out.println(openAiChatModel.stream(
            new Prompt(
                    "Generate the names of 5 famous pirates.",
                    OpenAiChatOptions.builder()
                            .withModel("gpt-3.5-turbo")
                            .withTemperature(0.4F)
                            .build()
            )).collectList().block());
}

}

Expected behavior There should be a working retry mechanism for calls to the WebClient

csterwa commented 1 week ago

This may be related to #1193