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

ZhiPu AI use ChatClient setting defaultSystem error

Open lwgCodePlus opened this issue 1 year ago • 3 comments

image image

lwgCodePlus avatar Aug 03 '24 12:08 lwgCodePlus

@mxsl-gr

lwgCodePlus avatar Aug 03 '24 17:08 lwgCodePlus

@mxsl-gr

Got it. I'll test this case later.

mxsl-gr avatar Aug 04 '24 02:08 mxsl-gr

i just tested this case.

	@Test
	void defaultSystemPromptTest() {
		String systemPrompt = "You are a helpful assistant, your name is Bob.";
		String userPrompt = "hi, what's your name?";
		ChatClient chatClient = ChatClient.builder(chatModel).defaultSystem(systemPrompt).build();

		Flux<ChatResponse> response1 = chatClient.prompt().user(userPrompt).stream().chatResponse();
				
		// build messages:
		// [0]: { role:System, text: 'You are a helpful assistant, your name is Bob.' }
		// [1]: { role:User, text: 'hi, what's your name?' }
		
		String content = Objects.requireNonNull(response1.collectList().block())
				.stream()
				.map(ChatResponse::getResults)
				.flatMap(List::stream)
				.map(Generation::getOutput)
				.map(AssistantMessage::getContent)
				.collect(Collectors.joining());
		assertThat(content).containsAnyOf("Bob");


		List<Message> messages = List.of(new UserMessage(userPrompt));
		Flux<ChatResponse> response2 = chatClient.prompt().messages(messages).stream().chatResponse();
				
		// build messages:
		// [0]: { role:User, text: 'hi, what's your name?' }
		// [1]: { role:System, text: 'You are a helpful assistant, your name is Bob.' }
		// [2]: { role:User, text: '' }
		
		String content2 = Objects.requireNonNull(response2.collectList().block())
				.stream()
				.map(ChatResponse::getResults)
				.flatMap(List::stream)
				.map(Generation::getOutput)
				.map(AssistantMessage::getContent)
				.collect(Collectors.joining());
		assertThat(content2).containsAnyOf("Bob");
	}

response1 is worked, the request messages:

[{ "role": "System", "text": "You are a helpful assistant, your name is Bob." }, { "role": "User", "text": "hi, what's your name?" }]

but response2 get an error, the request messages:

[{ "role": "User", "text": "hi, what's your name?" }, { "role": "System", "text": "You are a helpful assistant, your name is Bob." }, { "role": "User", "text": "" }]

check the code, the messages field in ChatClientRequestSpec takes precedence over system and user messages, acting more like chat history of the conversation.

i think is might not an exclusive issue with ZhiPu. it's just that ZhiPu's API validation is stricter and doesn't allow empty user message.

i'm not sure what your business, but i looking at your code, maybe it could use user, like this:

...
chatClient.prompt().user(userPrompt)
...

mxsl-gr avatar Aug 04 '24 03:08 mxsl-gr

Does the current PR fix the issues observed in this conversation?

https://github.com/spring-projects/spring-ai/pull/1188

markpollack avatar Aug 22 '24 15:08 markpollack

@lwgCodePlus Please try the latest Spring AI 1.0 and re-open this issue if you still run into problems. Thanks

ilayaperumalg avatar Jun 05 '25 13:06 ilayaperumalg