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

[BUG] Unable to create custom PromptTemplate #1075

Open
jayshetti opened this issue May 9, 2024 · 10 comments
Open

[BUG] Unable to create custom PromptTemplate #1075

jayshetti opened this issue May 9, 2024 · 10 comments
Labels
bug Something isn't working P3 Medium priority

Comments

@jayshetti
Copy link

jayshetti commented May 9, 2024

Describe the bug
I am trying langchain4J in Android and Upon creation new PromptTemplate as below, I see exception java.lang.ExceptionInInitializerError

 PromptTemplate prompt = new PromptTemplate(
                "Read and understand the conversation between the User and the AI. " +
                        "Then, analyze the new query from the User. " +
                        "Identify all relevant details, terms, and context from both the conversation and the new query. " +
                        "Reformulate this query into a clear, concise, and self-contained format suitable for information retrieval.\n" +
                        "\n" +
                        "Conversation:\n" +
                        "{{chatMemory}}\n" +
                        "\n" +
                        "User query: {{query}}\n" +
                        "\n" +
                        "It is very important that you provide only reformulated query and nothing else! " +
                        "Do not prepend a query with anything!"
        );

Log and Stack trace
java.lang.ExceptionInInitializerError
at dev.langchain4j.model.input.DefaultPromptTemplateFactory.create(DefaultPromptTemplateFactory.java:18)
at dev.langchain4j.model.input.DefaultPromptTemplateFactory.create(DefaultPromptTemplateFactory.java:14)
at dev.langchain4j.model.input.PromptTemplate.(PromptTemplate.java:62)
at dev.langchain4j.model.input.PromptTemplate.(PromptTemplate.java:51)

Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 10
{{(.+?)}}
^
To Reproduce
Just try to create the PromptTemplate

Expected behavior
PromptTemplate should be created.

Please complete the following information:

  • LangChain4j version: 0.30
  • LLM(s) used: Azure openAI
  • Java version: Android Q
@jayshetti jayshetti added the bug Something isn't working label May 9, 2024
Copy link

dosubot bot commented May 9, 2024

The java.lang.ExceptionInInitializerError you're encountering seems to be related to the static initialization phase of the PromptTemplate class in LangChain4j, specifically when it tries to initialize a PromptTemplateFactory. This error can occur due to several reasons, such as service loading failure, exceptions in factory implementations, or compatibility issues with Android.

To address this issue, you should:

  1. Verify Service Provider Configuration: Ensure any custom PromptTemplateFactory implementations are correctly specified in META-INF/services. The service provider configuration file should be named dev.langchain4j.spi.prompt.PromptTemplateFactory and contain the fully qualified name of your implementation class.

  2. Review Factory Implementations: Make sure that any custom implementations of PromptTemplateFactory do not throw exceptions during instantiation and are compatible with Android.

  3. Check Android Compatibility: Ensure all Java features used in your PromptTemplateFactory implementations and the loadFactories method are compatible with the Android version you are targeting.

  4. Debug Static Initialization: Consider adding logging or try-catch blocks around the static initialization code to identify the exact cause of the error.

If these steps do not resolve the issue, you might need to provide a custom initialization path for Android that avoids problematic static initialization or uses alternatives compatible with Android.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@jayshetti
Copy link
Author

The java.lang.ExceptionInInitializerError you're encountering seems to be related to the static initialization phase of the PromptTemplate class in LangChain4j, specifically when it tries to initialize a PromptTemplateFactory. This error can occur due to several reasons, such as service loading failure, exceptions in factory implementations, or compatibility issues with Android.

To address this issue, you should:

  1. Verify Service Provider Configuration: Ensure any custom PromptTemplateFactory implementations are correctly specified in META-INF/services. The service provider configuration file should be named dev.langchain4j.spi.prompt.PromptTemplateFactory and contain the fully qualified name of your implementation class.
  2. Review Factory Implementations: Make sure that any custom implementations of PromptTemplateFactory do not throw exceptions during instantiation and are compatible with Android.
  3. Check Android Compatibility: Ensure all Java features used in your PromptTemplateFactory implementations and the loadFactories method are compatible with the Android version you are targeting.
  4. Debug Static Initialization: Consider adding logging or try-catch blocks around the static initialization code to identify the exact cause of the error.

If these steps do not resolve the issue, you might need to provide a custom initialization path for Android that avoids problematic static initialization or uses alternatives compatible with Android.

Details

To continue the conversation, mention @dosu.

The root cause seems to be due to

private static final Pattern VARIABLE_PATTERN = Pattern.compile("\{\{(.+?)}}");

Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 10 {{(.+?)}} ^ this is happening can u see why

Copy link

dosubot bot commented May 9, 2024

The PatternSyntaxException you're encountering with the pattern \\{\\{(.+?)}} in the PromptTemplate class is puzzling since the pattern itself is correctly defined for matching text enclosed in double curly braces {{...}}. The correct Java syntax for this pattern is indeed "\\{\\{(.+?)\\}\\}", and your provided pattern \\{\\{(.+?)}} matches this requirement, suggesting that the syntax of the regular expression itself is not the source of the error.

Given that the regular expression is correctly defined, the issue might not be with the pattern's syntax. It could be related to other factors such as the execution environment, how the Java code is being run, or interactions with other parts of the code not shown in the snippet provided.

To further diagnose the issue, consider isolating the regular expression in a minimal Java program to verify its behavior outside of the Android and LangChain4j context. This step can help determine if the problem lies with the regular expression handling in your specific environment or if it's an issue with the LangChain4j library or its integration with Android Q.

If the regular expression works as expected in isolation, the next steps would involve a closer examination of the LangChain4j library's usage, especially around how PromptTemplate and PromptTemplateFactory are initialized and used within the Android environment. Ensure there are no environmental or compatibility issues that might affect the execution of regular expressions on Android Q.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@langchain4j
Copy link
Owner

Hi @jayshetti, thanks a lot for reporting! I tried the PromptTemplate you provided in plain Java and it works fine. Assuming that this is an AndroidQ-specific issue, could you please provide steps to reproduce it in Androind Q environment? Could be also a github project, thanks!

@jayshetti
Copy link
Author

Hi @jayshetti, thanks a lot for reporting! I tried the PromptTemplate you provided in plain Java and it works fine. Assuming that this is an AndroidQ-specific issue, could you please provide steps to reproduce it in Androind Q environment? Could be also a github project, thanks!

Hi @langchain4j ,

Still the same issue after lot of debugging i can see that apk which i am building does not the have service PromptTemplateFactory under META-INF/services/.
Can this be an issue? I tried adding it manually also but no luck for me on this.

@langchain4j
Copy link
Owner

If you want to use the DefaultPromptTemplateFactory, there is no need to have anything under META-INF/services/.

Can you provide code to reproduce this problem? I will be able to debug it then.

@jayshetti
Copy link
Author

jayshetti commented May 13, 2024

            @langchain4j 
            
            Please find the below code snippet, which is causing crash while creating the new  PromptTemplate itself.
			
	```
	ChatLanguageModel chatLanguageModel =  AzureOpenAiChatModel.builder()
            .apiKey("API_KEY")
            .endpoint("END_POINT")
            .deploymentName("MODEL_NAME")
            .temperature(0.3)
            .logRequestsAndResponses(true)
            .build();
			
			PromptTemplate prompt = new PromptTemplate(
            "Read and understand the conversation between the User and the AI. " +
                    "Then, analyze the new query from the User. " +
                    "Identify all relevant details, terms, and context from both the conversation and the new query. " +
                    "Reformulate this query into a clear, concise, and self-contained format suitable for information retrieval.\n" +
                    "\n" +
                    "Conversation:\n" +
                    "{{chatMemory}}\n" +
                    "\n" +
                    "User query: {{query}}\n" +
                    "\n" +
                    "It is very important that you provide only reformulated query and nothing else! " +
                    "Do not prepend a query with anything!"
            );
			
			QueryTransformer queryTransformer = new CompressingQueryTransformer(chatLanguageModel, prompt);
If i don't pass prompt for  CompressingQueryTransformer its same issue, the crash is observed , below is the back trace.
  
                              at dev.langchain4j.model.input.DefaultPromptTemplateFactory.create(DefaultPromptTemplateFactory.java:18)
                                                                                                    	at dev.langchain4j.model.input.DefaultPromptTemplateFactory.create(DefaultPromptTemplateFactory.java:14)
                                                                                                    	at dev.langchain4j.model.input.PromptTemplate.<init>(PromptTemplate.java:62)
                                                                                                    	at dev.langchain4j.model.input.PromptTemplate.<init>(PromptTemplate.java:51)
                                                                                                    	at dev.langchain4j.model.input.PromptTemplate.from(PromptTemplate.java:126)
                                                                                                    	at dev.langchain4j.rag.query.transformer.CompressingQueryTransformer.<clinit>(CompressingQueryTransformer.java:36)
				
				

@langchain4j langchain4j added the P3 Medium priority label May 13, 2024
@langchain4j
Copy link
Owner

Does this happen only in Android-Q? Can yo ureproduce it outside of Android? In plain Java

@jayshetti
Copy link
Author

jayshetti commented May 15, 2024

Does this happen only in Android-Q? Can yo ureproduce it outside of Android? In plain Java

@langchain4j If i use the regex present in the DefaultTemplate then I am encountering the issue, for Android regex should be "\\{\\{(.+?)\\}\\}"

This issue will not be there in the Java due to https://stackoverflow.com/a/45075252, and the current Prompttemplate will not work in Android. Do we have any work around for this?

Would be really helpful if I can get any work around or fix for this, I have tried to create diff PromptTemplateFactory to by pass this issue but adding META-INF/Services is difficult task in Android.

@langchain4j
Copy link
Owner

Well, if I understand it correctly, we can just add (unnecessary for java) escapes to both "}" in the regex. Feel free to do this change and open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P3 Medium priority
Projects
None yet
Development

No branches or pull requests

2 participants