-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Structured Output with few shot JSON Example not working #2539
Comments
I also encountered this problem. The problem occures in public Prompt toPrompt() {
ArrayList<Message> messages = new ArrayList(this.messages());
String processedSystemText = this.systemText();
if (StringUtils.hasText(processedSystemText)) {
if (!CollectionUtils.isEmpty(this.systemParams())) {
processedSystemText = (new PromptTemplate(processedSystemText, this.systemParams())).render();
}
messages.add(new SystemMessage(processedSystemText));
}
String formatParam = (String)this.adviseContext().get("formatParam");
String processedUserText = StringUtils.hasText(formatParam) ? this.userText() + System.lineSeparator() + "{spring_ai_soc_format}" : this.userText();
if (StringUtils.hasText(processedUserText)) {
Map<String, Object> userParams = new HashMap(this.userParams());
if (StringUtils.hasText(formatParam)) {
userParams.put("spring_ai_soc_format", formatParam);
}
if (!CollectionUtils.isEmpty(userParams)) {
processedUserText = (new PromptTemplate(processedUserText, userParams)).render();
}
messages.add(new UserMessage(processedUserText, this.media()));
}
ChatOptions var6 = this.chatOptions();
if (var6 instanceof FunctionCallingOptions functionCallingOptions) {
if (!this.functionNames().isEmpty()) {
functionCallingOptions.setFunctions(new HashSet(this.functionNames()));
}
if (!this.functionCallbacks().isEmpty()) {
functionCallingOptions.setFunctionCallbacks(this.functionCallbacks());
}
if (!CollectionUtils.isEmpty(this.toolContext())) {
functionCallingOptions.setToolContext(this.toolContext());
}
}
return new Prompt(messages, this.chatOptions());
} Under the hood .entity() method uses AdvisorsApi, adding "formatParam" Adviser to AdviseContext for ChatClient. We have: String processedUserText = StringUtils.hasText(formatParam) ? this.userText() + System.lineSeparator() + "{spring_ai_soc_format}" : this.userText();
Then processedUserText = (new PromptTemplate(processedUserText, userParams)).render(); is called and PromptTemplate tries to substitute the string, but our JSON is treated as a placeholder. Well, I think that the main problem of this method is userText concatenation before the format message substitution. Is there any reason why it is made this way? I think it would be better to allow the client to configure how the template is applied to userText. As stated in other open issues, you can escape { with {{ and } with }} (or \} etc.) |
Hi Team,
First of all thank you so much for your effort on creating this Spring AI module.
I am currently exploring Structured output entity feature to get the structured out in the format I wanted using few a Few Shot JSON example in the prompt itself.
Its throwing an error
java.lang.IllegalArgumentException: The template string is not valid
Complete error details are given below.
Environment
Local Environment
Prompt
String template file : flight_details_fewshot.st
Controller:
Error
Spring AI Version
Expected output
Working example without the entity() function call.
When I run the same example but just use the chatClient.prompt(promptMessage).call.content(), its working.
This kind of interaction is pretty common to drive the LLM to map the right values into JSON properties so that the application can take necessary action on them.
Fixing this would be a really helpful in dealing with Structured outputs.
Thanks,
Dilip Sundarraj
The text was updated successfully, but these errors were encountered: