Bug description
I'm using spring-ai along with local ollama. The OllamaChatModel's implementation doesn't consider external AbstractMessage implementation in method OllamaApi.ChatRequest ollamaChatRequest(Prompt prompt, boolean stream)
. There method body makes few class cast checks and then throws exception IllegalArgumentException with relevant message about unsupported message type. However, org.springframework.ai.chat.messages.AbstractMessage is open to extend and I can come up with my own independent implementation.
Environment spring-ai:1.0.0
Steps to reproduce
Implement MyChatMemory with method like this
@Override
public List<Message> get(String conversationId) {
return List.of(new AbstractMessage(MessageType.ASSISTANT, "any text", Map.of()) {
});
}
Expose bean
@Bean
public ChatClient chatClient(ChatClient.Builder builder, MyChatMemory chatMemory) {
return builder.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build()).build();
}
Evoke using ChatClient
chatClient.prompt(prompt) //
.advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, id))
.stream() //
.chatResponse() //
.subscribe(response -> {
//process response
});
Expected behavior
The org.springframework.ai.ollama.OllamaChatModel#ollamaChatRequest doesn't throw exception
java.lang.IllegalArgumentException: Unsupported message type: ASSISTANT
Comment From: sunyuhan1998
I agree with your point. Relying solely on instanceof
to determine the message type in this approach is unreasonable, as it imposes limitations on custom implementations of AbstractMessage
and is also inconsistent with the implementation in other models. I've submitted a PR to address this issue, which you can follow here: #3999