langchain4j / langchain4j

Java version of LangChain
https://docs.langchain4j.dev
Apache License 2.0
3.91k stars 742 forks source link

[FEATURE] PromptTemplate supports multiple templating engines. #149

Open jiangsier-xyz opened 10 months ago

jiangsier-xyz commented 10 months ago

I have noticed that PromptTemplate currently uses Mustache directly to generate the final string from a template string. However, in the real world, Python f-strings and Jinja2 are widely used to describe prompt templates. I suggest abstracting an interface for the template engine and integrating it into PromptTemplate to handle different styles of template strings (of course we can have a default handler).

For Mustache, we can use the following maven package:

<dependency>
    <groupId>com.github.spullara.mustache.java</groupId>
    <artifactId>compiler</artifactId>
</dependency>

For Python f-strings, we can use the following maven package:

<dependency>
    <groupId>com.github.squidfunk</groupId>
    <artifactId>python-string-format</artifactId>
</dependency>

For Jinja2/Jinjava, we can use the following maven package:

<dependency>
    <groupId>com.hubspot.jinjava</groupId>
    <artifactId>jinjava</artifactId>
</dependency>

(Since it uses {{...}} style placeholders like Mustache, maybe we don't need to integrate it.)

jiangsier-xyz commented 10 months ago

Oops...ChatGPT told me about com.github.squidfunk:python-string-format, but it doesn't exist... Now I'm using regex ways to resolve f-string templates.