Python: feat(Azure AI Search Context Provider): Add Azure AI Search Context Provider with toggle for "semantic" mode and "agentic" mode in azure ai#1546
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a Python Azure AI Search Context Provider with support for two retrieval modes: semantic (fast hybrid search) and agentic (slower multi-hop reasoning with Knowledge Bases).
- Implements AzureAISearchContextProvider with dual-mode functionality for different use cases
- Adds comprehensive sample demonstrating both semantic and agentic retrieval modes
- Integrates azure-search-documents dependency for search capabilities
Reviewed Changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
python/packages/azure-ai/agent_framework_azure_ai/_search_provider.py |
Core implementation of AzureAISearchContextProvider with semantic and agentic modes |
python/samples/getting_started/agents/azure_ai/azure_ai_with_search_context.py |
Sample demonstrating usage of the search context provider with both modes |
python/packages/azure-ai/pyproject.toml |
Adds azure-search-documents dependency |
python/packages/azure-ai/agent_framework_azure_ai/__init__.py |
Exports the new AzureAISearchContextProvider class |
python/samples/getting_started/agents/azure_ai/README.md |
Documents the new sample and required environment variables |
python/packages/azure-ai/agent_framework_azure_ai/_search_provider.py
Outdated
Show resolved
Hide resolved
python/packages/azure-ai/agent_framework_azure_ai/_search_provider.py
Outdated
Show resolved
Hide resolved
python/packages/azure-ai/agent_framework_azure_ai/_search_provider.py
Outdated
Show resolved
Hide resolved
python/packages/azure-ai/agent_framework_azure_ai/_search_provider.py
Outdated
Show resolved
Hide resolved
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
@farzad528 there are also several pyright typing issues we'll need to address: https://github.com/microsoft/agent-framework/actions/runs/18756209717/job/53508713618?pr=1546#step:5:269 You'll want to make sure you have the pre-commit checks install so they run before committing code: https://github.com/microsoft/agent-framework/blob/main/python/DEV_SETUP.md#after-installing-uv |
dmytrostruk
left a comment
There was a problem hiding this comment.
Looks good in general, just need to resolve CI errors and it would be great to add some unit tests.
This still needs to be handled, as well. Pre-commit checks are failing. |
1d770e3 to
a9a1b1e
Compare
- Fixes mypy error: Cannot assign to final name 'DEFAULT_CONTEXT_PROMPT' - Base class ContextProvider already defines this as Final - Instance variable self.context_prompt correctly set in __init__ (Line 197) Related to microsoft#1546
- Fixes mypy error: Cannot assign to final name 'DEFAULT_CONTEXT_PROMPT' - Base class ContextProvider already defines this as Final - Instance variable self.context_prompt correctly set in __init__ (Line 197) Related to microsoft#1546
8de2673 to
45dba6b
Compare
- Add type annotation to DEFAULT_CONTEXT_PROMPT - Add type annotation to vectorizable_fields - Add union type annotation to vector_queries
- Rename DEFAULT_CONTEXT_PROMPT to _DEFAULT_SEARCH_CONTEXT_PROMPT to avoid conflict with base class Final variable - Update test to use new constant name - All core package tests passing (1123 passed)
…rag' into feature/azure-ai-search-agentic-rag
| "openai>=1.99.0", | ||
| "azure-identity>=1,<2", | ||
| "mcp[ws]>=1.13", | ||
| "azure-search-documents==11.7.0b2", |
There was a problem hiding this comment.
given this dependency (beta status) all this code needs to be moved into a seperate package, while still importing through the .azure module. So we need to create a new package called agent-framework-azureaisearch or agent-framework-aisearch or something similar, that exports the same files, but uses lazy loading in the core/agent_framework/azure/__init__.py file.
| self, | ||
| endpoint: str, | ||
| index_name: str, | ||
| credential: AzureKeyCredential | AsyncTokenCredential, |
There was a problem hiding this comment.
let's also allow folks to pass in a key directly, and then we create the AzureKeyCredential in code.
|
|
||
| def __init__( | ||
| self, | ||
| endpoint: str, |
There was a problem hiding this comment.
I would like a settings class to be used for this, index_name and the key, and potentially also for azure_ai_project_endpoint, model, or azure openai config.
| top_k: int = 5, | ||
| semantic_configuration_name: str | None = None, | ||
| vector_field_name: str | None = None, | ||
| embedding_function: Any | None = None, |
There was a problem hiding this comment.
this should also be typed, otherwise I have no clue what to pass in here!
| """ | ||
| # Convert to list and filter to USER/ASSISTANT messages with text only | ||
| messages_list = [messages] if isinstance(messages, ChatMessage) else list(messages) | ||
| from agent_framework import Role |
There was a problem hiding this comment.
this can be a top-level import
…ge) (#2328) * Python: Fix pyright errors and move search provider to core (#1546) * address pablo coments * update azure ai search pypi version to latest prev * init update * Fix MyPy type annotation errors in search provider - Add type annotation to DEFAULT_CONTEXT_PROMPT - Add type annotation to vectorizable_fields - Add union type annotation to vector_queries * Fix DEFAULT_CONTEXT_PROMPT MyPy error and update test - Rename DEFAULT_CONTEXT_PROMPT to _DEFAULT_SEARCH_CONTEXT_PROMPT to avoid conflict with base class Final variable - Update test to use new constant name - All core package tests passing (1123 passed) * Python: Move Azure AI Search to separate package per PR feedback Addresses reviewer feedback from PR #1546 by isolating the beta dependency (azure-search-documents==11.7.0b2) into a new agent-framework-aisearch package. Changes: - Created new agent-framework-aisearch package with complete structure - Moved AzureAISearchContextProvider from core to aisearch package - Added AzureAISearchSettings class for environment variable auto-loading - Added support for direct API key string (auto-converts to AzureKeyCredential) - Added azure_openai_api_key parameter for Knowledge Base authentication - Updated embedding_function type to Callable[[str], Awaitable[list[float]]] - Moved Role import to top-level imports - Maintained lazy loading through agent_framework.azure module - Removed beta dependency from core package - Updated all tests to use new package location - All quality checks pass: ruff format/lint, pyright, mypy (0 errors) - All 21 unit tests pass with 59% coverage Semantic search mode verified working with both API key and managed identity authentication. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Python: Clarify top_k parameter only applies to semantic mode Updated documentation to clarify that the top_k parameter only affects semantic search mode. In agentic mode, the server-side Knowledge Base determines retrieval based on query complexity and reasoning effort. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Python: Add Knowledge Base output mode and retrieval reasoning effort parameters Added support for configurable Knowledge Base behavior in agentic mode: - knowledge_base_output_mode: "extractive_data" (default) or "answer_synthesis" Some knowledge sources require answer_synthesis mode for proper functionality. - retrieval_reasoning_effort: "minimal" (default), "medium", or "low" Controls query planning complexity and multi-hop reasoning depth. These parameters give users fine-grained control over Knowledge Base behavior and enable support for knowledge sources that require answer synthesis. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * effort and outputmode query params * Address PR review feedback for Azure AI Search context provider * comments eduward * ed latest comments --------- Co-authored-by: Farzad Sunavala <farzad.sunavala.enovate.ai> Co-authored-by: farzad528 <farzad528@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
…ge) (microsoft#2328) * Python: Fix pyright errors and move search provider to core (microsoft#1546) * address pablo coments * update azure ai search pypi version to latest prev * init update * Fix MyPy type annotation errors in search provider - Add type annotation to DEFAULT_CONTEXT_PROMPT - Add type annotation to vectorizable_fields - Add union type annotation to vector_queries * Fix DEFAULT_CONTEXT_PROMPT MyPy error and update test - Rename DEFAULT_CONTEXT_PROMPT to _DEFAULT_SEARCH_CONTEXT_PROMPT to avoid conflict with base class Final variable - Update test to use new constant name - All core package tests passing (1123 passed) * Python: Move Azure AI Search to separate package per PR feedback Addresses reviewer feedback from PR microsoft#1546 by isolating the beta dependency (azure-search-documents==11.7.0b2) into a new agent-framework-aisearch package. Changes: - Created new agent-framework-aisearch package with complete structure - Moved AzureAISearchContextProvider from core to aisearch package - Added AzureAISearchSettings class for environment variable auto-loading - Added support for direct API key string (auto-converts to AzureKeyCredential) - Added azure_openai_api_key parameter for Knowledge Base authentication - Updated embedding_function type to Callable[[str], Awaitable[list[float]]] - Moved Role import to top-level imports - Maintained lazy loading through agent_framework.azure module - Removed beta dependency from core package - Updated all tests to use new package location - All quality checks pass: ruff format/lint, pyright, mypy (0 errors) - All 21 unit tests pass with 59% coverage Semantic search mode verified working with both API key and managed identity authentication. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Python: Clarify top_k parameter only applies to semantic mode Updated documentation to clarify that the top_k parameter only affects semantic search mode. In agentic mode, the server-side Knowledge Base determines retrieval based on query complexity and reasoning effort. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Python: Add Knowledge Base output mode and retrieval reasoning effort parameters Added support for configurable Knowledge Base behavior in agentic mode: - knowledge_base_output_mode: "extractive_data" (default) or "answer_synthesis" Some knowledge sources require answer_synthesis mode for proper functionality. - retrieval_reasoning_effort: "minimal" (default), "medium", or "low" Controls query planning complexity and multi-hop reasoning depth. These parameters give users fine-grained control over Knowledge Base behavior and enable support for knowledge sources that require answer synthesis. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * effort and outputmode query params * Address PR review feedback for Azure AI Search context provider * comments eduward * ed latest comments --------- Co-authored-by: Farzad Sunavala <farzad.sunavala.enovate.ai> Co-authored-by: farzad528 <farzad528@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
Motivation and Context
This PR adds a first-class Azure AI Search context provider to the core framework, enabling agents to ground responses using both:
azure-search-documents==11.7.0b2).The goal is to support knowledge-grounded reasoning across enterprise content, using either lightweight semantic retrieval or multi-hop, citation-capable agentic grounding. This also centralizes Search support in the core package, making it available to all agents without extra Azure-specific packages.
Description
Key changes:
AzureAISearchContextProviderincore/agent_framework/azure/_search_provider.pyand exported via:from agent_framework.azure import AzureAISearchContextProviderUpgraded to
azure-search-documents==11.7.0b2and fully migrated from deprecated KnowledgeAgent types to the new KnowledgeBase API:KnowledgeAgent→KnowledgeBaseKnowledgeAgentMessage→KnowledgeBaseMessageazure.search.documents.agent→azure.search.documents.knowledgebasesAdded dual authentication support for Knowledge Bases:
Added
azure_ai_with_search_context.pysample demonstrating:Updated
README.mdwith correct sample references and usage guidance.Features:
Semantic mode
k_nearest_neighbors=50→ reranked totop_k(default 5)Agentic mode
Implementation improvements
KnowledgeBaseRetrievalClientin__aenter__Contribution Checklist
ruff,pyright,mypy)