Skip to content

PromptTemplate validation prevents the use of StringTemplate built-in functions #2456

Closed
@asw12

Description

@asw12

Bug description
PromptTemplate has a validate method that compares input variables used in the template (pulled straight from the underlying Antlr TokenStream) to the parameters provided in an attempt to verify that there are no unbound variables. However, StringTemplate has some built-in functions, and this validation is preventing their use.

Environment
Spring AI 1.0.0-M6
Java 17

Steps to reproduce
I was attempting to make it so that the prompt added by VectorStoreChatMemoryAdvisor would be added conditionally on whether there was actually data pulled from the chat history. Something like:

   String chatMemoryPrompt = """
                {if(strlen(long_term_memory))}

                Use the long term conversation memory from the LONG_TERM_MEMORY section to provide accurate answers.

                ---------------------
                LONG_TERM_MEMORY:
                {long_term_memory}
                ---------------------
                {endif}
                """;

    VectorStoreChatMemoryAdvisor.builder(chatHistoryStore)
                    .systemTextAdvise(chatMemoryPrompt)
                    .build();

But the validation considers strlen to be a variable that is unbound, and fails.

Expected behavior
I think the same conclusion from this can be reached as #1428 - make the validation optional.
But if we wanted to continue the validation "whack-a-mole", there exists a org.stringtemplate.v4.compiler.Compiler#funcs static map whose keys could be used to find the names of these built-in functions, and omit these in validation.
https://github.com/antlr/stringtemplate4/blob/master/src/org/stringtemplate/v4/compiler/Compiler.java#L81

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions