-
Notifications
You must be signed in to change notification settings - Fork 186
Introduce hook and context management to OpenSearch Agents #4388
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
base: main
Are you sure you want to change the base?
Introduce hook and context management to OpenSearch Agents #4388
Conversation
caa6d57 to
4eaeb94
Compare
| * @param <T> The type of hook event | ||
| */ | ||
| public <T extends HookEvent> void addCallback(Class<T> eventType, HookCallback<T> callback) { | ||
| callbacks.computeIfAbsent(eventType, k -> new ArrayList<>()).add(callback); |
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.
callbacks.computeIfAbsent --> this is not thread-safe. Do you see any concern?
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.
@Zhangxunmt can you explain your intention please?
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 don't see any new Threads added in this PR. So iirc there's no multiple threads in agents registration which is the only trigger to add hooks. If this addCallback is never used in the agent execution we should be good. Please double check @mingshl .
common/src/main/java/org/opensearch/ml/common/contextmanager/ActivationRuleFactory.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Xun Zhang <xunzh@amazon.com>
…project#4345) * initiate context management api with hook implementation Signed-off-by: Mingshi Liu <mingshl@amazon.com> * apply spotless Signed-off-by: Mingshi Liu <mingshl@amazon.com> --------- Signed-off-by: Mingshi Liu <mingshl@amazon.com>
* add pre_llm hook to per agent Signed-off-by: Mingshi Liu <mingshl@amazon.com> change context management passing from query parameters to payload Signed-off-by: Mingshi Liu <mingshl@amazon.com> pass hook registery into PER Signed-off-by: Mingshi Liu <mingshl@amazon.com> apply spotless Signed-off-by: Mingshi Liu <mingshl@amazon.com> initiate context management api with hook implementation Signed-off-by: Mingshi Liu <mingshl@amazon.com> * add comment Signed-off-by: Mingshi Liu <mingshl@amazon.com> * format Signed-off-by: Mingshi Liu <mingshl@amazon.com> * add validation Signed-off-by: Mingshi Liu <mingshl@amazon.com> --------- Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
…ing in index (opensearch-project#4403) * allow inline create context management without storing in agent register Signed-off-by: Mingshi Liu <mingshl@amazon.com> * make ML_COMMONS_MULTI_TENANCY_ENABLED default is false Signed-off-by: Mingshi Liu <mingshl@amazon.com> --------- Signed-off-by: Mingshi Liu <mingshl@amazon.com>
…roject#4408) * Fix POST_TOOL hook interaction updates and add tenant ID support Signed-off-by: Mingshi Liu <mingshl@amazon.com> - Fix POST_TOOL hook to return full ContextManagerContext like PRE_LLM hook - Update MLChatAgentRunner to properly handle interaction updates from POST_TOOL hook - Ensure interactions list and tmpParameters.INTERACTIONS stay synchronized - Add tenant ID support to MLPredictionTaskRequest in ModelGuardrail and SummarizationManager Signed-off-by: Mingshi Liu <mingshl@amazon.com> * fix error message escaping Signed-off-by: Mingshi Liu <mingshl@amazon.com> * consolicate post_hook logic Signed-off-by: Mingshi Liu <mingshl@amazon.com> --------- Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
4f376f3 to
8bb6138
Compare
Signed-off-by: Mingshi Liu <mingshl@amazon.com>
Description
Add Complete Context Management Framework with Memory-Integrated Hook Events to ML Agents
This PR introduces a comprehensive context management system for ML agents, featuring memory-integrated hook
events, complete hook registry with fault-tolerant execution, enhanced context integration, and intelligent
resource management for enterprise-grade deployments.
Major Features:
Complete Hook Registry System
• Fault-Tolerant Event Emission: emitEvent() with exception isolation and comprehensive logging
• Thread-Safe Operations: ConcurrentHashMap ensuring concurrent callback registration and execution
• Callback Management: Type-safe callback registration with monitoring capabilities
• Production-Grade Error Handling: Individual callback failures don't affect system operation
Comprehensive Hook Event Architecture
• HookEvent Base Class: Abstract base with invocation state for all lifecycle events
• PreLLMEvent: pre llm event with context integration and retrieved re-act interactions, including tool used result/ completed steps
• EnhancedPostToolEvent: Tool event with context integration
• Event Hierarchy: Well-designed inheritance from HookEvent to specialized events
Complete CRUD REST API Implementation
• Create Operations: MLCreateContextManagementTemplateAction, MLCreateContextManagementTemplateRequest,
MLCreateContextManagementTemplateResponse
• Read Operations: MLGetContextManagementTemplateAction, MLGetContextManagementTemplateRequest,
MLGetContextManagementTemplateResponse
• Delete Operations: MLDeleteContextManagementTemplateAction, MLDeleteContextManagementTemplateRequest,
MLDeleteContextManagementTemplateResponse
• Full CRUD Lifecycle: Complete template management with create, read, and delete operations
Context Management Integration
• ML Agent Integration: contextManagementName and contextManagement fields with validation
• Template System: ContextManagementTemplate with hooks, validation, and serialization
• Hook Provider Integration: ContextManagerHookProvider registers callbacks for all event types
Rich Context State Management
• ContextManagerContext: Comprehensive context with prompts, history, tools, interactions, and parameters
• Utility Methods: getEstimatedTokenCount(), getMessageCount(), addParameter(), addToolInteraction()
• Memory Integration: Context managers can access both current context and retrieved memory
• Dynamic Modification: Runtime context updates with memory and invocation state tracking
Event-Driven Execution Pipeline
• Tool Event Processing: handlePostTool() processes enhanced tool events with context
• Pre-LLM Processing: handlePreLLM() processes context before LLM execution
• Memory Event Processing: handlePostMemory() processes memory events with context and history
• Automatic Execution: All events processed automatically through hook registry
Complete Activation Rules System
• TokensExceedRule: Context-aware activation using comprehensive token estimation
• MessageCountExceedRule: Conversation length management with memory integration
• CompositeActivationRule: Complex conditions considering memory and context state
Token Counting Architecture
• TokenCounter Interface: Standardized interface with multiple truncation strategies
• Context Integration: Token counters integrated with activation rules and memory state
• Pluggable Design: Interface supports model-specific implementations
Pluggable Context Manager Architecture
• ContextManagerConfig: Configuration with type, activation, and manager settings
• Template Management: Complete template system with memory-integrated hooks
• Registry Integration: Managers registered for memory, tool, and LLM events
Technical Highlights:
• Comprehensive Event Model: Complete lifecycle coverage with memory, tool, and LLM events
• Fault-Tolerant Processing: Exception isolation ensures system stability across all event types
• Rich Context Access: Context managers have access to full context and memory
Architecture Benefits:
• Complete Lifecycle Coverage: Events cover all major phases of ML agent execution
• Integrated Memory Management: Memory retrieval and context management work together seamlessly
• Enterprise Reliability: Fault-tolerant design with comprehensive error handling
Use Cases:
• Comprehensive Resource Management: Manage tokens across current context and memory history
Impact:
• Comprehensive Agent Lifecycle: Full coverage of memory, tool, and LLM execution phases
• Advanced AI Orchestration: Sophisticated context management with memory integration
Check List
--signoff.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.