spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.25k stars 827 forks source link

StringTemplate should not be the only prompt template mechanism #1687

Open johnsonr opened 1 day ago

johnsonr commented 1 day ago

StringTemplate should not be the only prompt template mechanism used in Spring AI. Nor should it be the default.

StringTemplate is presently deeply baked into Spring AI. Consider AdvisedRequest.toPrompt and PromptTemplate. ChatClient.defaultSystem will automatically use ST.

The template engine should be pluggable, and a more familiar default such as Mustache should be used, ideally aligning as far as possible with existing Spring support. Jinjava provides solid JVM support for Jinja and it should also be an option because of its pervasive use in the Gen AI space.

Reasons:

While variable interpolation is fine, more complex things are quirky with StringTemplate. For example, looping is a common requirement in prompt templates. Consider how it looks in various popular template languages:

Mustache (familiar to many developers on all stacks):

{{#items}}
    {{.}}
{{/items}}

Jinja (the norm in Python and probably the most widely used in Gen AI):

{% for item in items %}
    {{ item }}
{% endfor %}

Both of these are easy for any developer to grasp, even if they haven't seen that particular syntax.

Now take StringTemplate:

list(items) ::= 
<items:{item | <item>}; separator="\n">
>>

This looks more like a BNF grammar than a typical template.

ilayaperumalg commented 17 hours ago

There is some related discussion around this topic here