Skip to content
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

Qute global values should probably be lazy #39832

Closed
FroMage opened this issue Apr 2, 2024 · 4 comments · Fixed by #39959
Closed

Qute global values should probably be lazy #39832

FroMage opened this issue Apr 2, 2024 · 4 comments · Fixed by #39959
Labels
area/qute The template engine kind/enhancement New feature or request
Milestone

Comments

@FroMage
Copy link
Member

FroMage commented Apr 2, 2024

Describe the bug

While filing a LangChain4J issue related to rendering a prompt using Qute, which is probably using the wrong classloader, which invoked global values, I noticed that global values were not lazy: quarkiverse/quarkus-langchain4j#436

So, there are two Qute issues here:

  • Perhaps global values should be lazy, after all they're method calls?
  • Probably global values should only be passed for certain targets? I mean, those were intended for HTML/Web templates, and while I can imagine them being useful even for email templates, I was surprised to see them evaluated for AI prompts. Perhaps we should add something of a target/scope to globals, like Web, Email, AI…?
@FroMage FroMage added the kind/enhancement New feature or request label Apr 2, 2024
@quarkus-bot quarkus-bot bot added the area/qute The template engine label Apr 2, 2024
Copy link

quarkus-bot bot commented Apr 2, 2024

/cc @mkouba (qute)

@mkouba
Copy link
Contributor

mkouba commented Apr 2, 2024

Perhaps global values should be lazy,

Globals variables are implemented as TemplateInstance.Initializers, see https://quarkus.io/guides/qute-reference#global_variables; i.e. they're added to the data map during initialization of a template instance. Which is exactly what I see in the stack trace.

after all they're method calls?

...and fields ;-)

Probably global values should only be passed for certain targets? I mean, those were intended for HTML/Web templates, and while I can imagine them being useful even for email templates, I was surprised to see them evaluated for AI prompts. Perhaps we should add something of a target/scope to globals, like Web, Email, AI…?

These variables are "global" by definition. If you only need something in a certain template, then don't use "global variable" but a custom TemplateInstance.Initializer instead 🤷.

In theory, we could add something like a set of targets where the global is accessible. But this would require the creator of the TemplateInstance to add a special attribute with TemplateInstance#setAttribute(), i.e. something like:

@TemplateGlobal(targets = ["AI","web"])
public class Globals {
    static int age = 40;
}

and then when creating the template instance something like:

template.instance().setAttribute("target", "AI").render();

so that we could compare TemplateGlobal#targets with TemplateInstance#getAttribute("target").

And target is ofc not a good name at all.

@FroMage
Copy link
Member Author

FroMage commented Apr 2, 2024

they're added to the data map during initialization of a template instance.

Well, they could be added to the map as a "lazy cacheable" value.

target is not great, but we can find a better name later. Intuitively, I expect that we could default that target on TemplateInstance for anything declared in controllers as web? And mail for MailTemplateInstance?

I mean, both solutions would probably solve the issue of unwanted globals showing up:

  • lazy globals, because if you don't use them, they are not initialised
  • segmented values depending on target, because AI prompts wouldn't see them

I'm not sure which is better, or if there's another even better approach? Both options do seem valuable for other use-cases, though, generally, no?

@mkouba
Copy link
Contributor

mkouba commented Apr 8, 2024

I'm not sure which is better, or if there's another even better approach? Both options do seem valuable for other use-cases, though, generally, no?

Yes, I think that we should make it possible to compute the data lazily and use it for global variables.

mkouba added a commit to mkouba/quarkus that referenced this issue Apr 9, 2024
- computed data are evaluated lazily when needed
- resolves quarkusio#39832
@quarkus-bot quarkus-bot bot added this to the 3.10 - main milestone Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/qute The template engine kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants