Fix an issue related to the Azure OpenAI streamig response

This issue in the Flux streaming logic was causing chunks
  to be aggreagated in a single response.

  Re-enable the AzureOpenAiAutoConfigurationIT
This commit is contained in:
Christian Tzolov
2024-05-27 00:51:36 +02:00
parent dc656ce3bc
commit 178a607cf6
4 changed files with 25 additions and 5 deletions

View File

@@ -183,8 +183,8 @@ public class AzureOpenAiChatModel
isFunctionCall.set(false);
return true;
}
return false;
}, false)
return !isFunctionCall.get();
})
.concatMapIterable(window -> {
final var reduce = window.reduce(MergeUtils.emptyChatCompletions(), MergeUtils::mergeChatCompletions);
return List.of(reduce);

View File

@@ -80,6 +80,29 @@ class OpenAiChatModelIT extends AbstractIT {
// needs fine tuning... evaluateQuestionAndAnswer(request, response, false);
}
@Test
void streamRoleTest() {
UserMessage userMessage = new UserMessage(
"Tell me about 3 famous pirates from the Golden Age of Piracy and what they did.");
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
Flux<ChatResponse> flux = streamingChatModel.stream(prompt);
List<ChatResponse> responses = flux.collectList().block();
assertThat(responses.size()).isGreaterThan(1);
String stitchedResponseContent = responses.stream()
.map(ChatResponse::getResults)
.flatMap(List::stream)
.map(Generation::getOutput)
.map(AssistantMessage::getContent)
.collect(Collectors.joining());
assertThat(stitchedResponseContent).contains("Blackbeard");
}
@Test
void listOutputConverter() {
DefaultConversionService conversionService = new DefaultConversionService();

View File

@@ -19,7 +19,6 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.azure.openai.AzureOpenAiChatModel;
@@ -44,7 +43,6 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Christian Tzolov
* @since 0.8.0
*/
@Disabled("streaming response on mark p machine is not returning a list of size > 1")
@EnabledIfEnvironmentVariable(named = "AZURE_OPENAI_API_KEY", matches = ".+")
@EnabledIfEnvironmentVariable(named = "AZURE_OPENAI_ENDPOINT", matches = ".+")
public class AzureOpenAiAutoConfigurationIT {

View File

@@ -35,7 +35,6 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.autoconfigure.azure.tool.DeploymentNameUtil.getDeploymentName;