Azure AI Foundry Agents
Azure AI Foundry Agents
Azure AI Foundry Agents를 OpenAI Request/Response 포맷으로 호출하는 방법을 알려드릴게요. LiteLLM을 쓰면 에이전트를 표준 completion 인터페이스로 사용할 수 있어요.
출처: 문서
본문
인증
옵션 1: Service Principal (프로덕션에 권장)
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-client-id"
export AZURE_CLIENT_SECRET="your-client-secret"
옵션 2: Azure AD 토큰 (수동)
# Get token via Azure CLI
az account get-access-token --resource "https://ai.azure.com" --query accessToken -o tsv
필요한 Azure 역할
az role assignment create \
--assignee-object-id "<service-principal-object-id>" \
--assignee-principal-type "ServicePrincipal" \
--role "Azure AI Developer" \
--scope "/subscriptions/<sub>/resourceGroups/<rg>/providers/Microsoft.CognitiveServices/accounts/<resource>"
빠른 시작
LiteLLM 모델 포맷
azure_ai/agents/{AGENT_ID}
azure_ai/agents/asst_abc123
LiteLLM Python SDK
import litellm
# Make a completion request to your Azure AI Foundry Agent
# Uses AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET env vars for auth
response = litellm.completion(
model="azure_ai/agents/asst_abc123",
messages=[
{
"role": "user",
"content": "Explain machine learning in simple terms"
}
],
api_base="https://your-resource.services.ai.azure.com/api/projects/your-project",
)
print(response.choices[0].message.content)
print(f"Usage: {response.usage}")
import litellm
# Stream responses from your Azure AI Foundry Agent
response = await litellm.acompletion(
model="azure_ai/agents/asst_abc123",
messages=[
{
"role": "user",
"content": "What are the key principles of software architecture?"
}
],
api_base="https://your-resource.services.ai.azure.com/api/projects/your-project",
stream=True,
)
async for chunk in response:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
LiteLLM Proxy
1. config.yaml에 모델 설정
model_list:
- model_name: azure-agent-1
litellm_params:
model: azure_ai/agents/asst_abc123
api_base: https://your-resource.services.ai.azure.com/api/projects/your-project
# Service Principal auth (recommended)
tenant_id: os.environ/AZURE_TENANT_ID
client_id: os.environ/AZURE_CLIENT_ID
client_secret: os.environ/AZURE_CLIENT_SECRET
- model_name: azure-agent-math-tutor
litellm_params:
model: azure_ai/agents/asst_def456
api_base: https://your-resource.services.ai.azure.com/api/projects/your-project
# Or pass Azure AD token directly
api_key: os.environ/AZURE_AD_TOKEN
2. LiteLLM Proxy 시작
litellm --config config.yaml
3. Azure AI Foundry Agents로 요청 보내기
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "azure-agent-1",
"messages": [
{
"role": "user",
"content": "Summarize the main benefits of cloud computing"
}
]
}'
curl http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "azure-agent-math-tutor",
"messages": [
{
"role": "user",
"content": "What is 25 * 4?"
}
],
"stream": true
}'
from openai import OpenAI
# Initialize client with your LiteLLM proxy URL
client = OpenAI(
base_url="http://localhost:4000",
api_key="your-litellm-api-key"
)
# Make a completion request to your Azure AI Foundry Agent
response = client.chat.completions.create(
model="azure-agent-1",
messages=[
{
"role": "user",
"content": "What are best practices for API design?"
}
]
)
print(response.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:4000",
api_key="your-litellm-api-key"
)
# Stream Agent responses
stream = client.chat.completions.create(
model="azure-agent-math-tutor",
messages=[
{
"role": "user",
"content": "Explain the Pythagorean theorem"
}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
환경 변수
export AZURE_TENANT_ID="your-tenant-id"
export AZURE_CLIENT_ID="your-client-id"
export AZURE_CLIENT_SECRET="your-client-secret"
대화 연속성 (스레드 관리)
import litellm
# First message creates a new thread
response1 = await litellm.acompletion(
model="azure_ai/agents/asst_abc123",
messages=[{"role": "user", "content": "My name is Alice"}],
api_base="https://your-resource.services.ai.azure.com/api/projects/your-project",
)
# Get the thread_id from the response
thread_id = response1._hidden_params.get("thread_id")
# Continue the conversation using the same thread
response2 = await litellm.acompletion(
model="azure_ai/agents/asst_abc123",
messages=[{"role": "user", "content": "What's my name?"}],
api_base="https://your-resource.services.ai.azure.com/api/projects/your-project",
thread_id=thread_id, # Pass the thread_id to continue conversation
)
print(response2.choices[0].message.content) # Should mention "Alice"
공급업체별 파라미터
from litellm import completion
response = litellm.completion(
model="azure_ai/agents/asst_abc123",
messages=[
{
"role": "user",
"content": "Analyze this data and provide insights",
}
],
api_base="https://your-resource.services.ai.azure.com/api/projects/your-project",
thread_id="thread_abc123", # Optional: Continue existing conversation
instructions="Be concise and focus on key insights", # Optional: Override agent instructions
)
model_list:
- model_name: azure-agent-analyst
litellm_params:
model: azure_ai/agents/asst_abc123
api_base: https://your-resource.services.ai.azure.com/api/projects/your-project
tenant_id: os.environ/AZURE_TENANT_ID
client_id: os.environ/AZURE_CLIENT_ID
client_secret: os.environ/AZURE_CLIENT_SECRET
instructions: "Be concise and focus on key insights"
사용 가능한 파라미터
thread_id와 instructions를 선택적으로 전달할 수 있어요.
LiteLLM A2A Gateway
1. Agents로 이동하기
2. Azure AI Foundry Agent 유형 선택하기
3. 에이전트 설정하기
에이전트 이름 (Agent Name)
에이전트 ID (Agent ID)
- https://ai.azure.com/ 에 접속해 "Agents"를 클릭해요
- 추가하려는 에이전트의 "ID"를 복사해요 (예:
asst_hbnoK9BOCcHhC3lC4MDroVGG) - LiteLLM에 Agent ID를 붙여 넣어요 - LiteLLM이 Azure Foundry에서 어떤 에이전트를 호출할지 알게 돼요
Azure AI API Base
- https://ai.azure.com/ 에 접속해 "Overview"를 클릭해요
- 라이브러리에서 Microsoft Foundry를 선택해요
- 엔드포인트를 얻으면
https://<domain>.services.ai.azure.com/api/projects/<project-name>형태예요 - LiteLLM에 URL을 붙여 넣어요
인증
- Azure Tenant ID
- Azure Client ID
- Azure Client Secret
4. Playground에서 테스트하기
5. 에이전트를 선택하고 메시지 보내기
추가 자료
- Azure AI Foundry Agents Documentation
- Create Thread and Run API Reference
- A2A Agent Gateway
- A2A Cost Tracking
A2A를 통한 Foundry 에이전트
agents:
- agent_name: foundry-agent
agent_card_params:
name: "Foundry Agent"
url: "https://<account>.services.ai.azure.com/api/projects/<project>/agents/<agent>/endpoint/protocols/a2a"
protocolVersion: "1.0"
capabilities:
streaming: false
litellm_params:
agent_card_path: agentCard/v1.0
tenant_id: os.environ/AZURE_TENANT_ID
client_id: os.environ/AZURE_CLIENT_ID
client_secret: os.environ/AZURE_CLIENT_SECRET
curl http://localhost:4000/a2a/foundry-agent \
-H "Authorization: Bearer $LITEL..._KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": "1",
"method": "message/send",
"params": {
"message": {
"kind": "message",
"role": "user",
"messageId": "m1",
"parts": [{"kind": "text", "text": "What is 2 + 2?"}]
},
"configuration": {"blocking": true}
}
}'
curl http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $LITEL..._KEY" \
-H "Content-Type: application/json" \
-d '{"model": "a2a/foundry-agent", "messages": [{"role": "user", "content": "What is 2 + 2?"}]}'