Compare commits

...
8 changed files with 78 additions and 53 deletions
@@ -20,7 +20,7 @@ This sample demonstrates using Anthropic with:
async def main() -> None:
"""Example of streaming response (get results as they are generated)."""
client = AnthropicClient[AnthropicChatOptions]()
client = AnthropicClient[AnthropicChatOptions](model_id="claude-sonnet-4-5-20250929")
# Create MCP tool configuration using instance method
mcp_tool = client.get_mcp_tool(
@@ -76,19 +76,19 @@ async def example_with_session_persistence_in_memory() -> None:
# First conversation
query1 = "What's the weather like in Tokyo?"
print(f"User: {query1}")
result1 = await agent.run(query1, session=session, store=False)
result1 = await agent.run(query1, session=session, options={"store": False})
print(f"Agent: {result1.text}")
# Second conversation using the same session - maintains context
query2 = "How about London?"
print(f"\nUser: {query2}")
result2 = await agent.run(query2, session=session, store=False)
result2 = await agent.run(query2, session=session, options={"store": False})
print(f"Agent: {result2.text}")
# Third conversation - agent should remember both previous cities
query3 = "Which of the cities I asked about has better weather?"
print(f"\nUser: {query3}")
result3 = await agent.run(query3, session=session, store=False)
result3 = await agent.run(query3, session=session, options={"store": False})
print(f"Agent: {result3.text}")
print("Note: The agent remembers context from previous messages in the same session.\n")
@@ -1,9 +1,11 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import os
from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
"""
Foundry Agent — Connect to a pre-configured agent in Microsoft Foundry
@@ -15,15 +17,18 @@ tools are all configured on the service — you just connect and run.
Environment variables:
FOUNDRY_PROJECT_ENDPOINT — Azure AI Foundry project endpoint
FOUNDRY_AGENT_NAME — Name of the agent in Foundry
FOUNDRY_AGENT_VERSION — Version of the agent (for PromptAgents)
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
agent = FoundryAgent(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-prompt-agent",
agent_version="1.0",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ.get("FOUNDRY_AGENT_VERSION"),
credential=AzureCliCredential(),
)
@@ -1,10 +1,12 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import os
from agent_framework import Agent
from agent_framework.foundry import FoundryAgent, RawFoundryAgentChatClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
"""
Foundry Agent — Custom client configuration
@@ -18,16 +20,19 @@ This sample demonstrates three ways to customize the FoundryAgent client layer:
Environment variables:
FOUNDRY_PROJECT_ENDPOINT — Azure AI Foundry project endpoint
FOUNDRY_AGENT_NAME — Name of the agent in Foundry
FOUNDRY_AGENT_VERSION — Version of the agent
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
# Option 1: Default — full middleware on both agent and client
agent = FoundryAgent(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-agent",
agent_version="1.0",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ.get("FOUNDRY_AGENT_VERSION"),
credential=AzureCliCredential(),
)
result = await agent.run("Hello from the default setup!")
@@ -35,9 +40,9 @@ async def main() -> None:
# Option 2: Raw client — no client-level middleware (agent middleware still active)
agent_raw_client = FoundryAgent(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-agent",
agent_version="1.0",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ.get("FOUNDRY_AGENT_VERSION"),
credential=AzureCliCredential(),
client_type=RawFoundryAgentChatClient,
)
@@ -47,9 +52,9 @@ async def main() -> None:
# Option 3: Composition — use Agent(client=...) directly
# this will not run the checks that the `FoundryAgent` does on things like tools.
client = RawFoundryAgentChatClient(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-agent",
agent_version="1.0",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ.get("FOUNDRY_AGENT_VERSION"),
credential=AzureCliCredential(),
)
agent_composed = Agent(client=client)
@@ -1,9 +1,11 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import os
from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
"""
Foundry Agent — Connect to a HostedAgent (no version needed)
@@ -16,12 +18,15 @@ Environment variables:
FOUNDRY_AGENT_NAME — Name of the hosted agent
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
# HostedAgents don't need agent_version
agent = FoundryAgent(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-hosted-agent",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
credential=AzureCliCredential(),
)
@@ -5,6 +5,7 @@ import os
from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
"""
Foundry Agent with Environment Variables
@@ -18,6 +19,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent (optional, for PromptAgents)
"""
# Load environment variables from .env file
load_dotenv()
async def main() -> None:
agent = FoundryAgent(
@@ -1,11 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import os
from typing import Annotated
from agent_framework import tool
from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from pydantic import Field
"""
@@ -24,6 +26,9 @@ Environment variables:
FOUNDRY_AGENT_VERSION — Version of the agent
"""
# Load environment variables from .env file
load_dotenv()
@tool(approval_mode="never_require")
def get_weather(
@@ -35,9 +40,9 @@ def get_weather(
async def main() -> None:
agent = FoundryAgent(
project_endpoint="https://your-project.services.ai.azure.com",
agent_name="my-weather-agent",
agent_version="1.0",
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ["FOUNDRY_AGENT_VERSION"],
credential=AzureCliCredential(),
tools=[get_weather],
)
@@ -8,22 +8,26 @@ from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
from openai import AsyncAzureOpenAI
# Load environment variables from .env file
load_dotenv()
from openai import AsyncOpenAI
"""
Foundry Chat Client with Code Interpreter and Files Example
This sample demonstrates using get_code_interpreter_tool() with Responses on Foundry
for Python code execution and data analysis with uploaded files.
Environment variables:
FOUNDRY_PROJECT_ENDPOINT — Azure AI Foundry project endpoint
FOUNDRY_MODEL — Model deployment name (e.g. "gpt-4o")
"""
# Load environment variables from .env file
load_dotenv()
# Helper functions
async def create_sample_file_and_upload(openai_client: AsyncAzureOpenAI) -> tuple[str, str]:
async def create_sample_file_and_upload(openai_client: AsyncOpenAI) -> tuple[str, str]:
"""Create a sample CSV file and upload it for Foundry code interpreter use."""
csv_data = """name,department,salary,years_experience
Alice Johnson,Engineering,95000,5
@@ -51,7 +55,7 @@ Frank Wilson,Engineering,88000,6
return temp_file_path, uploaded_file.id
async def cleanup_files(openai_client: AsyncAzureOpenAI, temp_file_path: str, file_id: str) -> None:
async def cleanup_files(openai_client: AsyncOpenAI, temp_file_path: str, file_id: str) -> None:
"""Clean up both local temporary file and uploaded file."""
# Clean up: delete the uploaded file
await openai_client.files.delete(file_id)
@@ -68,36 +72,33 @@ async def main() -> None:
# Initialize the underlying OpenAI client for file operations
credential = AzureCliCredential()
async def get_token():
token = credential.get_token("https://cognitiveservices.azure.com/.default")
return token.token
openai_client = AsyncAzureOpenAI(
azure_ad_token_provider=get_token,
api_version="2024-05-01-preview",
# Create FoundryChatClient first, then reuse its project client for file operations
client = FoundryChatClient(
credential=credential,
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["FOUNDRY_MODEL"],
)
openai_client = client.project_client.get_openai_client()
temp_file_path, file_id = await create_sample_file_and_upload(openai_client)
# Create agent using FoundryChatClient
client = FoundryChatClient(credential=credential)
try:
# Create code interpreter tool with file access
code_interpreter_tool = client.get_code_interpreter_tool(file_ids=[file_id])
# Create code interpreter tool with file access
code_interpreter_tool = client.get_code_interpreter_tool(file_ids=[file_id])
agent = Agent(
client=client,
instructions="You are a helpful assistant that can analyze data files using Python code.",
tools=[code_interpreter_tool],
)
agent = Agent(
client=client,
instructions="You are a helpful assistant that can analyze data files using Python code.",
tools=[code_interpreter_tool],
)
# Test the code interpreter with the uploaded file
query = "Analyze the employee data in the uploaded CSV file. Calculate average salary by department."
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result.text}")
await cleanup_files(openai_client, temp_file_path, file_id)
# Test the code interpreter with the uploaded file
query = "Analyze the employee data in the uploaded CSV file. Calculate average salary by department."
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result.text}")
finally:
await cleanup_files(openai_client, temp_file_path, file_id)
if __name__ == "__main__":