Microsoft Agent Framework는 Azure OpenAI 응답 서비스를 사용하는 에이전트 만들기를 지원합니다.
시작하기
필요한 NuGet 패키지를 프로젝트에 추가합니다.
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
Azure OpenAI 응답 에이전트 만들기
첫 번째 단계로 Azure OpenAI 서비스에 연결할 클라이언트를 만들어야 합니다.
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using OpenAI;
AzureOpenAIClient client = new AzureOpenAIClient(
new Uri("https://<myresource>.openai.azure.com/"),
new AzureCliCredential());
Azure OpenAI는 모델 호출 기능을 모두 제공하는 여러 서비스를 지원합니다. 응답 서비스를 선택하여 응답 기반 에이전트를 만듭니다.
#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
var responseClient = client.GetOpenAIResponseClient("gpt-4o-mini");
#pragma warning restore OPENAI001
마지막으로 CreateAIAgent에 ResponseClient 확장 메서드를 사용하여 에이전트를 만듭니다.
AIAgent agent = responseClient.CreateAIAgent(
instructions: "You are good at telling jokes.",
name: "Joker");
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
에이전트 사용
에이전트는 표준 AIAgent 이며 모든 표준 AIAgent 작업을 지원합니다.
에이전트를 실행하고 상호 작용하는 방법에 대한 자세한 내용은 에이전트 시작 자습서를 참조하세요.
구성 / 설정
환경 변수
Azure OpenAI 응답 에이전트를 사용하기 전에 다음 환경 변수를 설정해야 합니다.
export AZURE_OPENAI_ENDPOINT="https://<myresource>.openai.azure.com"
export AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME="gpt-4o-mini"
필요에 따라 다음을 설정할 수도 있습니다.
export AZURE_OPENAI_API_VERSION="preview" # Required for Responses API
export AZURE_OPENAI_API_KEY="<your-api-key>" # If not using Azure CLI authentication
설치
프로젝트에 Agent Framework 패키지를 추가합니다.
pip install agent-framework --pre
시작하기
Authentication
Azure OpenAI 응답 에이전트는 인증에 Azure 자격 증명을 사용합니다. 가장 간단한 방법은 AzureCliCredential을 실행한 후 az login을 사용하는 것입니다.
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
Azure OpenAI 응답 에이전트 만들기
기본 에이전트 만들기
에이전트를 만드는 가장 간단한 방법은 환경 변수를 AzureOpenAIResponsesClient 사용하는 것입니다.
import asyncio
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())
명시적 구성
환경 변수를 사용하는 대신 명시적으로 구성을 제공할 수도 있습니다.
import asyncio
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIResponsesClient(
endpoint="https://<myresource>.openai.azure.com",
deployment_name="gpt-4o-mini",
api_version="preview",
credential=AzureCliCredential()
).create_agent(
instructions="You are good at telling jokes.",
name="Joker"
)
result = await agent.run("Tell me a joke about a pirate.")
print(result.text)
asyncio.run(main())
에이전트 기능
추론 모델
Azure OpenAI 응답 에이전트는 복잡한 문제 해결을 위해 o1과 같은 고급 추론 모델을 지원합니다.
import asyncio
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIResponsesClient(
deployment_name="o1-preview", # Use reasoning model
credential=AzureCliCredential()
).create_agent(
instructions="You are a helpful assistant that excels at complex reasoning.",
name="ReasoningAgent"
)
result = await agent.run("Solve this logic puzzle: If A > B, B > C, and C > D, and we know D = 5, B = 10, what can we determine about A?")
print(result.text)
asyncio.run(main())
구조적 출력
Azure OpenAI 응답 에이전트에서 구조적 응답을 가져옵니다.
import asyncio
from typing import Annotated
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
from pydantic import BaseModel, Field
class WeatherForecast(BaseModel):
location: Annotated[str, Field(description="The location")]
temperature: Annotated[int, Field(description="Temperature in Celsius")]
condition: Annotated[str, Field(description="Weather condition")]
humidity: Annotated[int, Field(description="Humidity percentage")]
async def main():
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
instructions="You are a weather assistant that provides structured forecasts.",
response_format=WeatherForecast
)
result = await agent.run("What's the weather like in Paris today?")
weather_data = result.value
print(f"Location: {weather_data.location}")
print(f"Temperature: {weather_data.temperature}°C")
print(f"Condition: {weather_data.condition}")
print(f"Humidity: {weather_data.humidity}%")
asyncio.run(main())
함수 도구
Azure OpenAI 응답 에이전트에 사용자 지정 함수 도구를 제공할 수 있습니다.
import asyncio
from typing import Annotated
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
from pydantic import Field
def get_weather(
location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
"""Get the weather for a given location."""
return f"The weather in {location} is sunny with a high of 25°C."
async def main():
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful weather assistant.",
tools=get_weather
)
result = await agent.run("What's the weather like in Seattle?")
print(result.text)
asyncio.run(main())
코드 인터프리터
Azure OpenAI 응답 에이전트는 호스트된 코드 인터프리터를 통해 코드 실행을 지원합니다.
import asyncio
from agent_framework import ChatAgent, HostedCodeInterpreterTool
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
async with ChatAgent(
chat_client=AzureOpenAIResponsesClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant that can write and execute Python code.",
tools=HostedCodeInterpreterTool()
) as agent:
result = await agent.run("Calculate the factorial of 20 using Python code.")
print(result.text)
asyncio.run(main())
파일 업로드와 코드 인터프리터
데이터 분석 작업의 경우 파일을 업로드하고 코드를 사용하여 분석할 수 있습니다.
import asyncio
import os
import tempfile
from agent_framework import ChatAgent, HostedCodeInterpreterTool
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
from openai import AsyncAzureOpenAI
async def create_sample_file_and_upload(openai_client: AsyncAzureOpenAI) -> tuple[str, str]:
"""Create a sample CSV file and upload it to Azure OpenAI."""
csv_data = """name,department,salary,years_experience
Alice Johnson,Engineering,95000,5
Bob Smith,Sales,75000,3
Carol Williams,Engineering,105000,8
David Brown,Marketing,68000,2
Emma Davis,Sales,82000,4
Frank Wilson,Engineering,88000,6
"""
# Create temporary CSV file
with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as temp_file:
temp_file.write(csv_data)
temp_file_path = temp_file.name
# Upload file to Azure OpenAI
print("Uploading file to Azure OpenAI...")
with open(temp_file_path, "rb") as file:
uploaded_file = await openai_client.files.create(
file=file,
purpose="assistants", # Required for code interpreter
)
print(f"File uploaded with ID: {uploaded_file.id}")
return temp_file_path, uploaded_file.id
async def cleanup_files(openai_client: AsyncAzureOpenAI, 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)
print(f"Cleaned up uploaded file: {file_id}")
# Clean up temporary local file
os.unlink(temp_file_path)
print(f"Cleaned up temporary file: {temp_file_path}")
async def main():
print("=== Azure OpenAI Code Interpreter with File Upload ===")
# Initialize Azure 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",
)
temp_file_path, file_id = await create_sample_file_and_upload(openai_client)
# Create agent using Azure OpenAI Responses client
async with ChatAgent(
chat_client=AzureOpenAIResponsesClient(credential=credential),
instructions="You are a helpful assistant that can analyze data files using Python code.",
tools=HostedCodeInterpreterTool(inputs=[{"file_id": file_id}]),
) as agent:
# 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)
asyncio.run(main())
파일 검색
에이전트가 업로드된 문서 및 파일을 검색할 수 있도록 설정합니다.
import asyncio
from agent_framework import ChatAgent, HostedFileSearchTool, HostedVectorStoreContent
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def create_vector_store(client: AzureOpenAIResponsesClient) -> tuple[str, HostedVectorStoreContent]:
"""Create a vector store with sample documents."""
file = await client.client.files.create(
file=("todays_weather.txt", b"The weather today is sunny with a high of 75F."),
purpose="assistants"
)
vector_store = await client.client.vector_stores.create(
name="knowledge_base",
expires_after={"anchor": "last_active_at", "days": 1},
)
result = await client.client.vector_stores.files.create_and_poll(
vector_store_id=vector_store.id,
file_id=file.id
)
if result.last_error is not None:
raise Exception(f"Vector store file processing failed with status: {result.last_error.message}")
return file.id, HostedVectorStoreContent(vector_store_id=vector_store.id)
async def delete_vector_store(client: AzureOpenAIResponsesClient, file_id: str, vector_store_id: str) -> None:
"""Delete the vector store after using it."""
await client.client.vector_stores.delete(vector_store_id=vector_store_id)
await client.client.files.delete(file_id=file_id)
async def main():
print("=== Azure OpenAI Responses Client with File Search Example ===\n")
# Initialize Responses client
client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
file_id, vector_store = await create_vector_store(client)
async with ChatAgent(
chat_client=client,
instructions="You are a helpful assistant that can search through files to find information.",
tools=[HostedFileSearchTool(inputs=vector_store)],
) as agent:
query = "What is the weather today? Do a file search to find the answer."
print(f"User: {query}")
result = await agent.run(query)
print(f"Agent: {result}\n")
await delete_vector_store(client, file_id, vector_store.vector_store_id)
asyncio.run(main())
MCP(모델 컨텍스트 프로토콜) 도구
로컬 MCP 도구
확장 기능을 위해 로컬 MCP 서버에 연결합니다.
import asyncio
from agent_framework import ChatAgent, MCPStreamableHTTPTool
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
"""Example showing local MCP tools for Azure OpenAI Responses Agent."""
# Create Azure OpenAI Responses client
responses_client = AzureOpenAIResponsesClient(credential=AzureCliCredential())
# Create agent
agent = responses_client.create_agent(
name="DocsAgent",
instructions="You are a helpful assistant that can help with Microsoft documentation questions.",
)
# Connect to the MCP server (Streamable HTTP)
async with MCPStreamableHTTPTool(
name="Microsoft Learn MCP",
url="https://learn.microsoft.com/api/mcp",
) as mcp_tool:
# First query — expect the agent to use the MCP tool if it helps
first_query = "How to create an Azure storage account using az cli?"
first_result = await agent.run(first_query, tools=mcp_tool)
print("\n=== Answer 1 ===\n", first_result.text)
# Follow-up query (connection is reused)
second_query = "What is Microsoft Agent Framework?"
second_result = await agent.run(second_query, tools=mcp_tool)
print("\n=== Answer 2 ===\n", second_result.text)
asyncio.run(main())
호스트된 MCP 도구
승인 워크플로에서 호스트된 MCP 도구를 사용합니다.
import asyncio
from agent_framework import ChatAgent, HostedMCPTool
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
"""Example showing hosted MCP tools without approvals."""
credential = AzureCliCredential()
async with ChatAgent(
chat_client=AzureOpenAIResponsesClient(credential=credential),
name="DocsAgent",
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
tools=HostedMCPTool(
name="Microsoft Learn MCP",
url="https://learn.microsoft.com/api/mcp",
# Auto-approve all function calls for seamless experience
approval_mode="never_require",
),
) as agent:
# First query
first_query = "How to create an Azure storage account using az cli?"
print(f"User: {first_query}")
first_result = await agent.run(first_query)
print(f"Agent: {first_result.text}\n")
print("\n=======================================\n")
# Second query
second_query = "What is Microsoft Agent Framework?"
print(f"User: {second_query}")
second_result = await agent.run(second_query)
print(f"Agent: {second_result.text}\n")
asyncio.run(main())
이미지 분석
Azure OpenAI 응답 에이전트는 이미지 분석을 포함한 다중 모드 상호 작용을 지원합니다.
import asyncio
from agent_framework import ChatMessage, TextContent, UriContent
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
print("=== Azure Responses Agent with Image Analysis ===")
# Create an Azure Responses agent with vision capabilities
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
name="VisionAgent",
instructions="You are a helpful agent that can analyze images.",
)
# Create a message with both text and image content
user_message = ChatMessage(
role="user",
contents=[
TextContent(text="What do you see in this image?"),
UriContent(
uri="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
media_type="image/jpeg",
),
],
)
# Get the agent's response
print("User: What do you see in this image? [Image provided]")
result = await agent.run(user_message)
print(f"Agent: {result.text}")
asyncio.run(main())
컨텍스트 관리에 스레드 사용
여러 상호 작용 간에 대화 컨텍스트를 유지 관리합니다.
import asyncio
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful programming assistant."
)
# Create a new thread for conversation context
thread = agent.get_new_thread()
# First interaction
result1 = await agent.run("I'm working on a Python web application.", thread=thread, store=True)
print(f"Assistant: {result1.text}")
# Second interaction - context is preserved
result2 = await agent.run("What framework should I use?", thread=thread, store=True)
print(f"Assistant: {result2.text}")
asyncio.run(main())
스트리밍 응답
스트리밍을 사용하여 생성되는 응답을 가져옵니다.
import asyncio
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
async def main():
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful assistant."
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Tell me a short story about a robot"):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
asyncio.run(main())
에이전트 사용
에이전트는 표준 BaseAgent 이며 모든 표준 에이전트 작업을 지원합니다.
에이전트를 실행하고 상호 작용하는 방법에 대한 자세한 내용은 에이전트 시작 자습서를 참조하세요.