-
Notifications
You must be signed in to change notification settings - Fork 186
Improve semantic fact extraction prompt and add JSON enforcement #4282
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
Improve semantic fact extraction prompt and add JSON enforcement #4282
Conversation
c8d4f1b to
e79b701
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4282 +/- ##
=========================================
Coverage 80.15% 80.15%
Complexity 10167 10167
=========================================
Files 854 854
Lines 44181 44183 +2
Branches 5112 5112
=========================================
+ Hits 35413 35417 +4
+ Misses 6614 6613 -1
+ Partials 2154 2153 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
common/src/main/java/org/opensearch/ml/common/memorycontainer/MemoryContainerConstants.java
Show resolved
Hide resolved
common/src/main/java/org/opensearch/ml/common/memorycontainer/MemoryContainerConstants.java
Outdated
Show resolved
Hide resolved
- Replace XML-heavy prompt with universal semantic fact extraction agent - Add JSON_ENFORCEMENT_MESSAGE constant for reliable LLM output format - Update MemoryProcessingService to append enforcement message to all fact extraction requests - Add comprehensive unit tests to verify JSON enforcement functionality - Support multiple contexts: personal facts, technical investigations, and RCA - Improve fact extraction reliability and maintainability Signed-off-by: Dhrubo Saha <dhrubo@amazon.com>
- Convert SEMANTIC_FACTS_EXTRACTION_PROMPT to use Java text blocks - Convert JSON_ENFORCEMENT_MESSAGE to use text blocks - Improves code readability and makes prompts easier to edit and visualize - Addresses PR feedback about using triple quotes
Signed-off-by: Dhrubo Saha <dhrubo@amazon.com>
9d55e94 to
9cb73cd
Compare
|
@dhrubo-os should |
| MessageInput enforcementMessage = getMessageInput(JSON_ENFORCEMENT_MESSAGE); | ||
| enforcementMessage.toXContent(messagesBuilder, ToXContent.EMPTY_PARAMS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you find adding the enforcement message in the User prompt instead of the system prompt works better?
Because in my experience they seem to adhere better in System prompt.
If we add too many messages in user prompt, the LLM might fail to adhere to the output format. The length of system prompt is in our control but the length of user prompt varies, I am not sure if this would adversely impact the output format
The general guidance from OpenAI and others is to put output format instructions in System Prompt.
If we do not see any significant improvements, I suggest adding it in the system prompt, to keep the code clean and readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
even in the user prompt when i ask LLM to not adhere to this format, it responds by saying it cannot and has been explicitly requested to follow a certain format. This must be because system prompt is given higher priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed significant improvements with adding this JSON_ENFORCEMENT_MESSAGE. By using this I had 100% success to extract facts with our desired facts with 30+ iterations (different types of memories). So I'll insist to keep this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I got this suggestion from LLMs like claude & openai :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, that sounds good.
But recommend to try system_prompt as an iteration in the future, it should perform better! I had some issues with PER responses when it was in user_prompt, moving to system_prompt significantly helped. As you have tested, I don't want to block you! Thanks!
I'm still investigating what else to improve in the user preference. After performing my analysis I'll raise another separate PR focusing that. |
* Improve semantic fact extraction prompt and add JSON enforcement - Replace XML-heavy prompt with universal semantic fact extraction agent - Add JSON_ENFORCEMENT_MESSAGE constant for reliable LLM output format - Update MemoryProcessingService to append enforcement message to all fact extraction requests - Add comprehensive unit tests to verify JSON enforcement functionality - Support multiple contexts: personal facts, technical investigations, and RCA - Improve fact extraction reliability and maintainability Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> * Use triple quotes (text blocks) for better prompt readability - Convert SEMANTIC_FACTS_EXTRACTION_PROMPT to use Java text blocks - Convert JSON_ENFORCEMENT_MESSAGE to use text blocks - Improves code readability and makes prompts easier to edit and visualize - Addresses PR feedback about using triple quotes * updated prompt based on comment Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> --------- Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> (cherry picked from commit cac8ddb)
…) (#4286) * Improve semantic fact extraction prompt and add JSON enforcement - Replace XML-heavy prompt with universal semantic fact extraction agent - Add JSON_ENFORCEMENT_MESSAGE constant for reliable LLM output format - Update MemoryProcessingService to append enforcement message to all fact extraction requests - Add comprehensive unit tests to verify JSON enforcement functionality - Support multiple contexts: personal facts, technical investigations, and RCA - Improve fact extraction reliability and maintainability * Use triple quotes (text blocks) for better prompt readability - Convert SEMANTIC_FACTS_EXTRACTION_PROMPT to use Java text blocks - Convert JSON_ENFORCEMENT_MESSAGE to use text blocks - Improves code readability and makes prompts easier to edit and visualize - Addresses PR feedback about using triple quotes * updated prompt based on comment --------- (cherry picked from commit cac8ddb) Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> Co-authored-by: Dhrubo Saha <dhrubo@amazon.com>
…nsearch-project#4282) * Improve semantic fact extraction prompt and add JSON enforcement - Replace XML-heavy prompt with universal semantic fact extraction agent - Add JSON_ENFORCEMENT_MESSAGE constant for reliable LLM output format - Update MemoryProcessingService to append enforcement message to all fact extraction requests - Add comprehensive unit tests to verify JSON enforcement functionality - Support multiple contexts: personal facts, technical investigations, and RCA - Improve fact extraction reliability and maintainability Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> * Use triple quotes (text blocks) for better prompt readability - Convert SEMANTIC_FACTS_EXTRACTION_PROMPT to use Java text blocks - Convert JSON_ENFORCEMENT_MESSAGE to use text blocks - Improves code readability and makes prompts easier to edit and visualize - Addresses PR feedback about using triple quotes * updated prompt based on comment Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> --------- Signed-off-by: Dhrubo Saha <dhrubo@amazon.com> Signed-off-by: Anuj Soni <sonianuj287@gmail.com>
Summary
This PR improves the semantic fact extraction system in the agentic memory feature by replacing the verbose XML-heavy prompt with a universal semantic fact extraction agent and adding JSON enforcement mechanisms for reliable LLM output.
Changes Made
1. Updated Semantic Facts Extraction Prompt
common/src/main/java/org/opensearch/ml/common/memorycontainer/MemoryContainerConstants.java2. Added JSON Enforcement Mechanism
common/src/main/java/org/opensearch/ml/common/memorycontainer/MemoryContainerConstants.javaJSON_ENFORCEMENT_MESSAGEconstant to guarantee proper LLM output format3. Updated Service Logic
plugin/src/main/java/org/opensearch/ml/action/memorycontainer/memory/MemoryProcessingService.javaextractFactsFromConversationmethod to automatically append JSON enforcement message to all fact extraction requests4. Comprehensive Testing
plugin/src/test/java/org/opensearch/ml/action/memorycontainer/memory/MemoryProcessingServiceTests.javatestExtractFactsFromConversation_JsonEnforcementMessageAppendedto verify JSON enforcementBenefits
Testing
Backward Compatibility
This change is backward compatible as it only modifies the internal prompt structure and adds enforcement messaging. The API and external behavior remain unchanged.
Related Issues
This addresses improvements to the agentic memory feature's fact extraction reliability and prompt maintainability.