fix: Optimizing AI agent creation and deleting garbage threads#446
Merged
Conversation
fix: Hide citation panel logic on click of hide chat
fix: Optimize Azure AI Agent Provisioning and Garbage Thread Cleanup for KM Generic Accelerator
Avijit-Microsoft
approved these changes
May 28, 2025
Contributor
|
🎉 This PR is included in version 3.3.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
This pull request introduces significant enhancements to the backend architecture, focusing on improving the management of Azure AI agents, optimizing caching, and enhancing logging practices. Key updates include the creation of a singleton factory for Azure AI agents, restructuring the FastAPI application lifecycle, and implementing a custom cache for managing agent threads.
Backend Architecture Enhancements:
Singleton Azure AI Agent Factory: Introduced
AgentFactoryinsrc/api/agents/agent_factory.pyto manage a singleton instance ofAzureAIAgent. This ensures centralized agent creation and deletion, with support for plugins and Azure AI configuration.FastAPI Lifespan Management: Refactored
src/api/app.pyto integrateAgentFactoryinto the FastAPI application lifecycle. Thelifespancontext manager initializes the agent on startup and cleans up resources during shutdown. [1] [2]Caching Improvements:
ExpCacheinsrc/api/services/chat_service.pyto handle thread eviction and expiration. This ensures Azure AI agent threads are properly deleted when no longer needed, reducing resource leakage.Logging and Error Handling:
src/api/services/chat_service.py,src/api/plugins/chat_with_data_plugin.py) by replacing f-strings with parameterized logging for better performance and security. [1] [2] [3] [4]Minor Updates:
ChatService: ModifiedChatServiceto accept aRequestobject, enabling access to the application state for agent management. [1] [2]src/api/plugins/chat_with_data_plugin.py.These changes collectively improve resource management, scalability, and maintainability of the backend system.
Does this introduce a breaking change?
Golden Path Validation
Deployment Validation
What to Check
A single shared agent will be used for handling requests.
Threads will be deleted automatically from the cache 1 hour after conversation expiration.