Fixing samples with minor changes. 1. Adding model details for Anthropic, 2. Moving to environment variables for endpoint and deployment details

This commit is contained in:
Chetan Toshniwal
2026-03-30 21:04:07 -07:00
committed by Eduard van Valkenburg
Unverified
parent 95fd5ec658
commit 30920e1f9d
8 changed files with 52 additions and 37 deletions
@@ -1,10 +1,14 @@
# 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
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Connect to a pre-configured agent in Microsoft Foundry
@@ -21,9 +25,9 @@ Environment variables:
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["FOUNDRY_AGENT_VERSION"],
credential=AzureCliCredential(),
)
@@ -1,10 +1,15 @@
# 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
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Custom client configuration
@@ -25,9 +30,9 @@ Environment variables:
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["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["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["FOUNDRY_AGENT_VERSION"],
credential=AzureCliCredential(),
)
agent_composed = Agent(client=client)
@@ -1,9 +1,14 @@
# 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
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent — Connect to a HostedAgent (no version needed)
@@ -20,8 +25,8 @@ Environment variables:
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,7 +5,10 @@ import os
from agent_framework.foundry import FoundryAgent
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent with Environment Variables
@@ -23,7 +26,7 @@ async def main() -> None:
agent = FoundryAgent(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
agent_name=os.environ["FOUNDRY_AGENT_NAME"],
agent_version=os.environ.get("FOUNDRY_AGENT_VERSION"),
agent_version=os.environ["FOUNDRY_AGENT_VERSION"],
credential=AzureCliCredential(),
)
@@ -1,13 +1,18 @@
# 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
# Load environment variables from .env file
load_dotenv()
"""
Foundry Agent with Local Function Tools
@@ -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,7 +8,7 @@ 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
from openai import AsyncOpenAI
# Load environment variables from .env file
load_dotenv()
@@ -23,7 +23,7 @@ for Python code execution and data analysis with uploaded files.
# 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 +51,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)
@@ -67,21 +67,14 @@ async def main() -> None:
# Initialize the underlying OpenAI client for file operations
credential = AzureCliCredential()
project_endpoint = os.environ.get("FOUNDRY_PROJECT_ENDPOINT")
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=project_endpoint)
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)
# Create code interpreter tool with file access
code_interpreter_tool = client.get_code_interpreter_tool(file_ids=[file_id])