-
Notifications
You must be signed in to change notification settings - Fork 3
Add local document chat agent using Microsoft Agent Framework + Azure AI Search #101
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
Draft
Copilot
wants to merge
14
commits into
development
Choose a base branch
from
copilot/create-local-agent-utilizing-index
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3a4a8c6
Initial plan
Copilot 8e83006
Add document chat agent using Microsoft Agent Framework with Azure AI…
Copilot 85570b9
Refactor agent: auto-resolve connection ID and index name from config
Copilot 8b0f8ac
Remove --stage/--model CLI args; create AI Search connection if missing
Copilot 50945bb
Fix pydocstyle D205/D400 in ensure_ai_search_connection_id docstring
Copilot 0184368
Add agent dependencies to root requirements.txt to fix test import error
Copilot 7411ee2
Add src/__init__.py to make src a Python package, fixing ModuleNotFou…
Copilot 8563f0f
Add [tool:pytest] pythonpath=. to setup.cfg to fix ModuleNotFoundErro…
Copilot 8f3cd5a
Fix 471 pytest warnings: lazy-import azure-ai-ml and filter marshmall…
Copilot 4675a7c
Revert 8f3cd5a: restore lazy imports, test patches and filterwarnings…
Copilot e91ac6d
Add azure-search-documents version print to buildcontainer Dockerfile
Copilot b1f0077
Fix AI Search connection creation fallback to hub workspace
Copilot 5299a8f
Revert hub-based workspace fallback for AI Search connection
Copilot 89a59f3
Fix workspace name resolution: use Foundry hostname instead of projec…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,6 @@ max-complexity = 10 | |
| max-line-length = 120 | ||
| count = True | ||
| statistics = True | ||
|
|
||
| [tool:pytest] | ||
| pythonpath = . | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Source package for AI Search MLOps components.""" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| """Agent module for chatting with indexed documents using Azure AI Search.""" | ||
|
|
||
| from src.agent import agent # noqa: F401 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| """Agent for chatting with documents indexed in Azure AI Search.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential | ||
| from azure.identity.aio import DefaultAzureCredential | ||
| from azure.ai.projects import AIProjectClient as SyncAIProjectClient | ||
| from azure.ai.projects.models import ConnectionType | ||
| from azure.ai.agents.models import AzureAISearchTool, AzureAISearchQueryType | ||
| from azure.ai.ml import MLClient | ||
| from azure.ai.ml.entities import AzureAISearchConnection | ||
| from semantic_kernel.agents import AzureAIAgent | ||
| from semantic_kernel.agents import AzureAIAgentThread | ||
|
|
||
| from mlops.common.config_utils import MLOpsConfig | ||
| from mlops.common.naming_utils import generate_index_name | ||
|
|
||
|
|
||
| AGENT_NAME = "DocumentChatAgent" | ||
| AGENT_INSTRUCTIONS = ( | ||
| "You are a helpful assistant that answers questions about documents " | ||
| "stored in an Azure AI Search index. Use the search tool to find relevant " | ||
| "information and provide accurate, concise answers based on the indexed content. " | ||
| "If the answer is not found in the indexed documents, say so clearly." | ||
| ) | ||
|
|
||
|
|
||
| def _extract_workspace_name(endpoint: str) -> str: | ||
| """Extract the AI Foundry workspace name from the project endpoint URL. | ||
|
|
||
| In Azure AI Foundry the ARM workspace resource is the Foundry resource | ||
| whose name appears as the hostname prefix (before | ||
| ``.services.ai.azure.com``), **not** the project name in the URL path. | ||
|
|
||
| Args: | ||
| endpoint (str): URL in the form | ||
| ``https://<foundry>.services.ai.azure.com/api/projects/<project>``. | ||
|
|
||
| Returns: | ||
| str: The workspace / Foundry resource name. | ||
| """ | ||
| from urllib.parse import urlparse | ||
|
|
||
| hostname = urlparse(endpoint).hostname or "" | ||
| return hostname.split(".")[0] | ||
|
|
||
|
|
||
| def ensure_ai_search_connection_id( | ||
| endpoint: str, | ||
| acs_service_name: str, | ||
| subscription_id: str, | ||
| resource_group_name: str, | ||
| ) -> str: | ||
| """Return the AI Foundry connection ID for the given Azure AI Search service. | ||
|
|
||
| First checks whether a connection whose target URL contains | ||
| ``acs_service_name`` is already registered in the project. If no such | ||
| connection exists, one is created via the Azure AI ML management SDK using | ||
| AAD/managed-identity authentication (no API key required). | ||
|
|
||
| Args: | ||
| endpoint (str): The Azure AI Foundry project endpoint. | ||
| acs_service_name (str): The Azure AI Search service name (e.g. 'my-search'). | ||
| subscription_id (str): Azure subscription ID. | ||
| resource_group_name (str): Azure resource group name. | ||
|
|
||
| Returns: | ||
| str: The connection ID to use with the AI Search tool. | ||
| """ | ||
| credential = SyncDefaultAzureCredential() | ||
| client = SyncAIProjectClient(endpoint=endpoint, credential=credential) | ||
| connections = list(client.connections.list(connection_type=ConnectionType.AZURE_AI_SEARCH)) | ||
|
|
||
| # Return the connection whose target URL matches the configured service name | ||
| matched = next( | ||
| (c for c in connections if acs_service_name.lower() in c.target.lower()), | ||
| None, | ||
| ) | ||
| if matched: | ||
| return matched.id | ||
|
|
||
| # No matching connection found — create one using the management SDK | ||
| print( | ||
| f"No AI Search connection found for '{acs_service_name}'. " | ||
| "Creating connection in AI Foundry..." | ||
| ) | ||
| workspace_name = _extract_workspace_name(endpoint) | ||
| ml_client = MLClient( | ||
| credential=credential, | ||
| subscription_id=subscription_id, | ||
| resource_group_name=resource_group_name, | ||
| workspace_name=workspace_name, | ||
| ) | ||
| new_connection = AzureAISearchConnection( | ||
| name=acs_service_name, | ||
| endpoint=f"https://{acs_service_name}.search.windows.net", | ||
| ) | ||
| created = ml_client.connections.create_or_update(new_connection) | ||
| return created.id | ||
|
|
||
|
|
||
| async def create_agent( | ||
| ai_search_connection_id: str, | ||
| ai_search_index_name: str, | ||
| model_deployment_name: str, | ||
| endpoint: str, | ||
| ) -> AzureAIAgent: | ||
| """ | ||
| Create an AzureAIAgent configured with an Azure AI Search tool. | ||
|
|
||
| Args: | ||
| ai_search_connection_id (str): The AI Foundry connection ID for Azure AI Search. | ||
| ai_search_index_name (str): The name of the Azure AI Search index to query. | ||
| model_deployment_name (str): The model deployment name to use for the agent. | ||
| endpoint (str): The Azure AI Foundry project endpoint. | ||
|
|
||
| Returns: | ||
| AzureAIAgent: Configured agent instance. | ||
| """ | ||
| ai_search_tool = AzureAISearchTool( | ||
| index_connection_id=ai_search_connection_id, | ||
| index_name=ai_search_index_name, | ||
| query_type=AzureAISearchQueryType.VECTOR_SEMANTIC_HYBRID, | ||
| top_k=5, | ||
| ) | ||
|
|
||
| credential = DefaultAzureCredential() | ||
| client = AzureAIAgent.create_client(credential=credential, endpoint=endpoint) | ||
| agent_definition = await client.agents.create_agent( | ||
| model=model_deployment_name, | ||
| name=AGENT_NAME, | ||
| instructions=AGENT_INSTRUCTIONS, | ||
| tools=ai_search_tool.definitions, | ||
| tool_resources=ai_search_tool.resources, | ||
| ) | ||
|
|
||
| return AzureAIAgent(client=client, definition=agent_definition) | ||
|
|
||
|
|
||
| async def run_agent_conversation(agent: AzureAIAgent, user_message: str) -> str: | ||
| """ | ||
| Send a single message to the agent and return the response. | ||
|
|
||
| Args: | ||
| agent (AzureAIAgent): The configured agent instance. | ||
| user_message (str): The user's query message. | ||
|
|
||
| Returns: | ||
| str: The agent's response text. | ||
| """ | ||
| thread: AzureAIAgentThread | None = None | ||
| try: | ||
| thread = AzureAIAgentThread(client=agent.client) | ||
| response_parts = [] | ||
| async for response in agent.invoke( | ||
| messages=user_message, | ||
| thread=thread, | ||
| ): | ||
| response_parts.append(str(response.content)) | ||
| return "".join(response_parts) | ||
| finally: | ||
| if thread is not None: | ||
| await thread.delete() | ||
|
|
||
|
|
||
| async def run_local_chat( | ||
| ai_search_connection_id: str, | ||
| ai_search_index_name: str, | ||
| model_deployment_name: str, | ||
| endpoint: str, | ||
| ) -> None: | ||
| """ | ||
| Run an interactive local chat session with the document agent. | ||
|
|
||
| Args: | ||
| ai_search_connection_id (str): The AI Foundry connection ID for Azure AI Search. | ||
| ai_search_index_name (str): The name of the Azure AI Search index to query. | ||
| model_deployment_name (str): The model deployment name to use for the agent. | ||
| endpoint (str): The Azure AI Foundry project endpoint. | ||
| """ | ||
| print("Initializing Document Chat Agent...") | ||
| agent = await create_agent( | ||
| ai_search_connection_id=ai_search_connection_id, | ||
| ai_search_index_name=ai_search_index_name, | ||
| model_deployment_name=model_deployment_name, | ||
| endpoint=endpoint, | ||
| ) | ||
| print(f"Agent '{AGENT_NAME}' is ready. Type 'exit' or 'quit' to stop.\n") | ||
|
|
||
| try: | ||
| while True: | ||
| user_input = input("You: ").strip() | ||
| if not user_input: | ||
| continue | ||
| if user_input.lower() in {"exit", "quit"}: | ||
| print("Goodbye!") | ||
| break | ||
| response = await run_agent_conversation(agent, user_input) | ||
| print(f"Agent: {response}\n") | ||
| finally: | ||
| await agent.client.agents.delete_agent(agent.id) | ||
|
|
||
|
|
||
| def main(): | ||
| """Run the document chat agent locally using configuration from config.yaml.""" | ||
| config = MLOpsConfig() | ||
| agent_config = config.agent_config | ||
| acs_config = config.acs_config | ||
| sub_config = config.sub_config | ||
|
|
||
| endpoint = agent_config["agent_endpoint"] | ||
| model = agent_config["agent_model_deployment"] | ||
| acs_service_name = acs_config["acs_service_name"] | ||
| subscription_id = sub_config["subscription_id"] | ||
| resource_group_name = sub_config["resource_group_name"] | ||
| index_name = generate_index_name() | ||
|
|
||
| print(f"Looking up AI Search connection for service '{acs_service_name}'...") | ||
| connection_id = ensure_ai_search_connection_id( | ||
| endpoint=endpoint, | ||
| acs_service_name=acs_service_name, | ||
| subscription_id=subscription_id, | ||
| resource_group_name=resource_group_name, | ||
| ) | ||
|
|
||
| asyncio.run( | ||
| run_local_chat( | ||
| ai_search_connection_id=connection_id, | ||
| ai_search_index_name=index_name, | ||
| model_deployment_name=model, | ||
| endpoint=endpoint, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| semantic-kernel>=1.0.0 | ||
| azure-identity>=1.16.1 | ||
| azure-ai-projects>=1.0.0 | ||
| azure-ai-agents>=1.0.0 | ||
| azure-ai-ml>=1.0.0 | ||
| python-dotenv>=0.10.3 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Agent tests package.""" |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
@copilot can we create a connection if it doesn't exist?
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.
Yes — implemented in commit
8b0f8ac. The functionensure_ai_search_connection_idnow:CognitiveSearchconnections in the AI Foundry projectACS_SERVICE_NAMEMLClient.connections.create_or_update(AzureAISearchConnection(...))usingsubscription_idandresource_group_namefromsub_config— the project name is extracted from the endpoint URL. AAD/managed-identity auth is used (no API key needed).