Making API Calls to Azure AI Foundry Agents


Overview
Azure AI Foundry provides a powerful platform for creating and deploying AI agents. This guide will walk you through how to make API calls to your deployed agents using JavaScript in a NestJS application.
Prerequisites
An Azure AI Foundry account
A deployed agent
Node.js environment
Azure CLI installed and configured
Environment variables set up for sensitive information
Azure CLI Setup
Before you can use the DefaultAzureCredential authentication, you need to:
Install Azure CLI from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
Log in to Azure using:
az login
Set your default subscription:
az account set --subscription "your-subscription-id"
Step-by-Step Guide
1. Accessing the Agent Code
Log in to Azure AI Foundry
Navigate to the "Agents" section
Find your agent and click "Try in Playground"
Click "View Code" to access the implementation details
2. Setting Up Environment Variables
Create a .env
file with the following variables:
AZURE_AI_FOUNDRY_ENDPOINT=your_endpoint_here
AZURE_AI_FOUNDRY_API_KEY=your_api_key_here
AZURE_AI_FOUNDRY_AGENT_ID=your_agent_id_here
AZURE_AGENT_CONNECTION_STRING=your_connection_string
3. NestJS Service Implementation
Here's the exact implementation of the Azure service in a NestJS application:
// azure.service.ts
import { Injectable } from '@nestjs/common';
import { DefaultAzureCredential } from '@azure/identity';
import { AzureAIFoundryClient } from '@azure/ai-foundry';
@Injectable()
export class AzureService {
private client: AzureAIFoundryClient;
constructor() {
const credential = new DefaultAzureCredential();
this.client = new AzureAIFoundryClient({
endpoint: process.env.AZURE_AI_FOUNDRY_ENDPOINT,
credential: credential,
});
}
async runAgentConversation(userInput: string): Promise<string> {
try {
async runAgentConversation(prompt: string) {
const client = AIProjectsClient.fromConnectionString(
process.env.AZURE_AGENT_CONNECTION_STRING,
new DefaultAzureCredential({}),
);
const agent = await client.agents.getAgent(process.env.AZURE_AI_FOUNDRY_AGENT_ID);
console.log(`Retrieved agent: ${agent.name}`);
const thread = await client.agents.createThread();
console.log(`Retrieved thread, thread ID: ${thread.id}`);
const message = await client.agents.createMessage(thread.id, {
role: 'user',
content: userInput,
});
console.log(`Created message, message ID: ${message.id}`);
// Create run
let run = await client.agents.createRun(thread.id, agent.id);
// Poll until the run reaches a terminal status
while (run.status === 'queued' || run.status === 'in_progress') {
// Wait for a second
await new Promise((resolve) => setTimeout(resolve, 1000));
run = await client.agents.getRun(thread.id, run.id);
}
console.log(`Run completed with status: ${run.status}`);
// Retrieve messages
const messages = await client.agents.listMessages(thread.id);
// // Display messages
// for (const dataPoint of messages.data.reverse()) {
// console.log(`${dataPoint.createdAt} - ${dataPoint.role}:`);
// for (const contentItem of dataPoint.content) {
// if (contentItem.type === 'text') {
// console.log(contentItem.type);
// }
// }
// }
const content = messages.data[0].content as Array<MessageTextContentOutput>;
return content[0].text.value;
}
} catch (error) {
console.error('Error running agent conversation:', error);
throw error;
}
}
}
4. Using the Service in a Controller
// azure.controller.ts
import { Controller, Post, Body } from '@nestjs/common';
import { AzureService } from './azure.service';
@Controller('azure')
export class AzureController {
constructor(private readonly azureService: AzureService) {}
@Post('conversation')
async runConversation(@Body('input') userInput: string) {
try {
const response = await this.azureService.runAgentConversation(userInput);
return { response };
} catch (error) {
return { error: error.message };
}
}
}
5. Module Setup
// azure.module.ts
import { Module } from '@nestjs/common';
import { AzureService } from './azure.service';
import { AzureController } from './azure.controller';
@Module({
controllers: [AzureController],
providers: [AzureService],
exports: [AzureService],
})
export class AzureModule {}
Key Components Explained
DefaultAzureCredential
The DefaultAzureCredential
is used for authentication and will try multiple authentication methods in this order:
Environment variables
Managed Identity
Visual Studio Code
Azure CLI
Interactive browser login
AzureAIFoundryClient
The AzureAIFoundryClient
is the main class for interacting with Azure AI Foundry. It handles:
Authentication
API endpoint management
Request/response handling
runAgent Method
The runAgent
method takes two main parameters:
agentId
: The unique identifier for your agentinput
: The user's input to process
Response Structure
The response from the agent typically includes:
output
: The agent's response textmetadata
: Additional information about the interactionstatus
: The status of the request
Error Handling
Common errors you might encounter:
Authentication errors (invalid credentials)
Agent not found (invalid agent ID)
Rate limiting
Network issues
Example Use Cases
Customer Support: Automated responses to common queries
Data Analysis: Processing and analyzing user input
Content Generation: Creating content based on user prompts
Task Automation: Automating repetitive tasks
Conclusion
Making API calls to Azure AI Foundry Agents in a NestJS application is straightforward once you have the proper setup. By following this guide and implementing the provided code, you can integrate AI agent functionality into your applications.
Remember to:
Keep your Azure CLI session active
Implement proper error handling
Monitor your API usage
Stay updated with the latest Azure AI Foundry features
For more complex implementations, consider:
Adding retry logic for failed requests
Implementing caching for frequent queries
Creating a wrapper service for better abstraction
Subscribe to my newsletter
Read articles from Ojangole Jordan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
