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

Use default stringtemplate delimiters for prompt templates

Open pherklotz opened this issue 11 months ago • 10 comments

The current set delimiters { and } do not follow the stringtemplate default and need escaping if you want to build a prompt with JSON (as described in issue #355). This pull request will address this issue.

pherklotz avatar Mar 14 '24 08:03 pherklotz

Upon reviewing the PR, I've considered that using { and } as delimiters in the current template writing approach might offer the advantage of allowing variables to be used directly in the frontend layer with frameworks like React and Vue, without the need for additional modifications.

Furthermore, since this delimiter usage is common across several frameworks, it could also lead to improved readability. It seems to me that this might have been the author's intention.

nit

youngmoneee avatar Mar 14 '24 14:03 youngmoneee

I don't follow the comment regarding { and } offering an advantage for frontend frameworks. Do you mean sending frontend code as part of the prompt? JSON is a more common case than embedding react code.

Between easier json embedding into the prompt or sending react code, I'd go with easier json embedding. That said, it sounds like the delimiter should be configurable to better accommodate all cases.

My intention was to support the python F-string syntax. How does python handle the case of inserting json into a f-string?

I'd need to reproduce the original issue, what does embedding json into the TextTemplate look like?

markpollack avatar Mar 21 '24 21:03 markpollack

What I intended was similar to what you mentioned about F-String, as various languages or frameworks use { , } based variable templates. This makes it useful when importing templates from external sources or exporting them, and given its widespread use, it should be easy for developers to read.

Changing the delimiter to <, > due to the difficulty of embedding JSON in prompts might introduce new challenges when inserting HTML or XML.

To fundamentally solve these issues, developing custom parsers, renderers, or using flexible variable templates through the injection of ST with various delimiters might be a good approach.

youngmoneee avatar Mar 22 '24 12:03 youngmoneee

I agree, that it might be the best to introduce a configuration option for the delimiters. I can change the PR accordingly. The default option would still be < and >. I agree that this introduces error with HTML or XML snippets. But I assume these cases are rarer than JSON snippets.

@markpollack an example prompt could be:

List all European countries and their capitals. Answer with the following JSON format: {"Country name": "capital name"}

This prompt fails if used with the ST Template Prompt mechanism.

pherklotz avatar Mar 25 '24 11:03 pherklotz

I shared some thoughts on the issue for this PR about the templating strategy: https://github.com/spring-projects/spring-ai/issues/355#issuecomment-2018926008

ThomasVitale avatar Mar 25 '24 21:03 ThomasVitale

So how do we proceed with this? I am fine with changing it, but will only start working once there is a commitment to a solution ;)

pherklotz avatar Apr 10 '24 12:04 pherklotz

So how do we proceed with this? I am fine with changing it, but will only start working once there is a commitment to a solution ;)

I have created a code that functions similarly to F-string. It's a simple prototype, so it's not perfect beyond the basic functionality, but based on test code, if there's a specific scenario that needs to be addressed, it could be refined into a more advanced form. If there is a situation that needs to be handled, it would be good to talk together. :) #355

youngmoneee avatar Apr 11 '24 15:04 youngmoneee

I agree that PromptTemplate should provide an API for users to use different delimiters based on their own scenarios. However, can we keep the default delimiter as { } to avoid unnecessary compatibility changes for existing users?

HzjNeverStop avatar Jul 30 '24 12:07 HzjNeverStop

I also agree that we should allow different delimiters, and I've also had requests for different template engines. I do not want to change the default { and } as it would bring too much pain to users. If I made bad choice there, apologies. Bumping this to RC1 - sorry, it keeps slipping. Will get to it eventually! Apologies.

markpollack avatar Aug 22 '24 15:08 markpollack