Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,718 changes: 8,718 additions & 0 deletions ai.azure.com.har

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions hugging-face-tool-agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.11-slim

WORKDIR /app

COPY . user_agent/
WORKDIR /app/user_agent

RUN if [ -f requirements.txt ]; then \
pip install -r requirements.txt; \
else \
echo "No requirements.txt found"; \
fi

EXPOSE 8088

CMD ["python", "main.py"]
25 changes: 25 additions & 0 deletions hugging-face-tool-agent/agent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: HuggingFace-Agent
description: Hugging Face agent
metadata:
example:
- role: user
content: |-
What are the trending models in the OpenLLM Leaderboard?
tags:
- Microsoft Agent Framework
template:
name: HuggingFace-Agent
kind: hosted
environment_variables:
- name: AZURE_OPENAI_ENDPOINT
value: ${AZURE_OPENAI_ENDPOINT}
- name: OPENAI_API_VERSION
value: 2025-03-01-preview
- name: AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
value: "{{chat}}"
- name: AZURE_AI_PROJECT_TOOL_CONNECTION_ID
value: "HuggingFaceMCPServer"
resources:
- kind: model
id: gpt-5
name: chat
Binary file not shown.
Binary file not shown.
115 changes: 115 additions & 0 deletions hugging-face-tool-agent/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright (c) Microsoft. All rights reserved.
"""Example showing how to use an agent factory function with ToolClient.

This sample demonstrates how to pass a factory function to from_agent_framework
that receives a ToolClient and returns an AgentProtocol. This pattern allows
the agent to be created dynamically with access to tools from Azure AI Tool
Client at runtime.
"""

import asyncio
import os
from typing import List
from dotenv import load_dotenv
from agent_framework import AIFunction
from agent_framework.azure import AzureOpenAIChatClient

from azure.ai.agentserver.agentframework import from_agent_framework
from azure.identity.aio import DefaultAzureCredential, get_bearer_token_provider

load_dotenv()


def create_agent_factory():
"""Create a factory function that builds an agent with ToolClient.

This function returns a factory that takes a ToolClient and returns
an AgentProtocol. The agent is created at runtime for every request,
allowing it to access the latest tool configuration dynamically.
"""

async def agent_factory(tools: List[AIFunction]) -> AzureOpenAIChatClient:
"""Factory function that creates an agent using the provided tools.

:param tools: The list of AIFunction tools available to the agent.
:type tools: List[AIFunction]
:return: An Agent Framework ChatAgent instance.
:rtype: ChatAgent
"""
# List all available tools from the ToolClient
print("Fetching tools from Azure AI Tool Client via factory...")
print(f"Found {len(tools)} tools:")
for tool in tools:
print(f" - tool: {tool.name}, description: {tool.description}")

if not tools:
print("\nNo tools found!")
print("Make sure your Azure AI project has tools configured.")
raise ValueError("No tools available to create agent")

azure_credential = DefaultAzureCredential()
token_provider = get_bearer_token_provider(azure_credential, "https://cognitiveservices.azure.com/.default")
# Create the Agent Framework agent with the tools
print("\nCreating Agent Framework agent with tools from factory...")
agent = AzureOpenAIChatClient(ad_token_provider=token_provider).create_agent(
name="ToolClientAgent",
instructions="You are a helpful assistant with access to various tools.",
tools=tools,
)

print("Agent created successfully!")
return agent

return agent_factory


async def quickstart():
"""Build and return an AgentFrameworkCBAgent using an agent factory function."""

# Get configuration from environment
project_endpoint = os.getenv("AZURE_AI_PROJECT_ENDPOINT")

if not project_endpoint:
raise ValueError(
"AZURE_AI_PROJECT_ENDPOINT environment variable is required. "
"Set it to your Azure AI project endpoint, e.g., "
"https://<your-account>.services.ai.azure.com/api/projects/<your-project>"
)

# Create Azure credentials
credential = DefaultAzureCredential()

# Create a factory function that will build the agent at runtime
# The factory will receive a ToolClient when the agent first runs
agent_factory = create_agent_factory()

tool_connection_id = os.getenv("AZURE_AI_PROJECT_TOOL_CONNECTION_ID")
# Pass the factory function to from_agent_framework instead of a compiled agent
# The agent will be created on every agent run with access to ToolClient
print("Creating Agent Framework adapter with factory function...")
adapter = from_agent_framework(
agent_factory,
credentials=credential,
tools=[
{"type": "mcp", "project_connection_id": tool_connection_id}

]
)

print("Adapter created! Agent will be built on every request.")
return adapter


async def main(): # pragma: no cover - sample entrypoint
"""Main function to run the agent."""
adapter = await quickstart()

if adapter:
print("\nStarting agent server...")
print("The agent factory will be called for every request that arrives.")
await adapter.run_async()


if __name__ == "__main__":
asyncio.run(main())

4 changes: 4 additions & 0 deletions hugging-face-tool-agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
azure_ai_agentserver_core-1.0.0b7-py3-none-any.whl
azure_ai_agentserver_agentframework-1.0.0b7-py3-none-any.whl
pytest==8.4.2
python-dotenv==1.1.1
Loading