VOOZH about

URL: https://thenewstack.io/tutorial-build-a-rag-agent-with-azure-ai-agent-service-sdk/

⇱ Tutorial: Build a RAG Agent With Azure AI Agent Service SDK - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-02-11 09:00:25
Tutorial: Build a RAG Agent With Azure AI Agent Service SDK
tutorial,
AI Agents / Cloud Services / Software Development

Tutorial: Build a RAG Agent With Azure AI Agent Service SDK

Build an AI Agent on Azure using the Azure AI Python SDK. The agent will complete an action with file search and a user-defined function call.
Feb 11th, 2025 9:00am by Janakiram MSV
👁 Featued image for: Tutorial: Build a RAG Agent With Azure AI Agent Service SDK
Image via Unsplash+. 

This tutorial will help you build an agent using the Azure AI Python SDK. The agent will complete an action by leveraging the file search tool (knowledge tool) and a user-defined function call (Action tool).

The agent can answer questions about flight timings and baggage policies based on an existing PDF file and a real-time API. While the PDF is fictitious, the API is real-time and based on FlightAware API. Refer to my previous tutorial for details on this API and how to integrate it with function calls.

Prerequisites

This guide assumes that you have access to an active Azure subscription. You will require a hub and project in Azure AI Foundry, with the GPT-4o-mini model deployed and ready to use.

Your Model+Endpoints section should look like the following:

👁 Image

Ensure the token rate limit is set to max to avoid throttling.

👁 Image

Finally, get the project connection string from the project’s resource page.

👁 Image

Install the Python Modules

Configure the virtual environment:

python -m venv .venv
source .venv/bin/activate

Install Python modules:

pip install azure-ai-projects
pip install azure-identity

Initialize the Client

We will start by initializing the project client and passing the project connection string, which is stored as an environment variable.

import os
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import FileSearchTool, FilePurpose, FunctionTool, ToolSet
from azure.identity import DefaultAzureCredential
from user_functions import user_functions

# Initialize client
credential = DefaultAzureCredential()
project_client = AIProjectClient.from_connection_string(
 credential=credential, conn_str=os.environ["PROJECT_CONNECTION_STRING"]
)

Create the Knowledge Tool From the PDF

We will now upload the PDF to turn it into a vector store, which will become a knowledge tool for the agent.

👁 Image

# Upload and process baggage policy document
file = project_client.agents.upload_file_and_poll(file_path='./data/baggage.pdf', purpose=FilePurpose.AGENTS)
print(f"Uploaded file, file ID: {file.id}")

# Create vector store
vector_store = project_client.agents.create_vector_store_and_poll(file_ids=[file.id], name="baggage_vector_store")
print(f"Created vector store, vector store ID: {vector_store.id}")

Wrapping the FlightAware API Into a Tool

Create a Python file called user_functions.py with the following code:

import json
from datetime import datetime, timedelta
from typing import Any, Callable, Set, Dict, List, Optional
import requests
import pytz

def get_flight_status(flight):
 """Returns Flight Information"""
 AEROAPI_BASE_URL = "YOUR_API_URL"
 AEROAPI_KEY="YOUR_API_KEY"

 def get_api_session():
 session = requests.Session()
 session.headers.update({"x-apikey": AEROAPI_KEY})
 return session

 def fetch_flight_data(flight_id, session):
 if "flight_id=" in flight_id:
 flight_id = flight_id.split("flight_id=")[1] 
 
 start_date = datetime.now().date().strftime('%Y-%m-%d')
 end_date = (datetime.now().date() + timedelta(days=1)).strftime('%Y-%m-%d')
 api_resource = f"/flights/{flight_id}?start={start_date}&end={end_date}"
 response = session.get(f"{AEROAPI_BASE_URL}{api_resource}")
 response.raise_for_status()
 return response.json()['flights'][0]

 def utc_to_local(utc_date_str, local_timezone_str):
 utc_datetime = datetime.strptime(utc_date_str, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.utc)
 local_timezone = pytz.timezone(local_timezone_str)
 local_datetime = utc_datetime.astimezone(local_timezone)
 return local_datetime.strftime('%Y-%m-%d %H:%M:%S') 
 
 session = get_api_session()
 flight_data = fetch_flight_data(flight, session)
 
 dep_key = 'estimated_out' if 'estimated_out' in flight_data and flight_data['estimated_out'] else \
 'actual_out' if 'actual_out' in flight_data and flight_data['actual_out'] else \
 'scheduled_out'
 
 arr_key = 'estimated_in' if 'estimated_in' in flight_data and flight_data['estimated_in'] else \
 'actual_in' if 'actual_in' in flight_data and flight_data['actual_in'] else \
 'scheduled_in' 
 
 flight_details = {
 'flight':flight,
 'source': flight_data['origin']['city'],
 'destination': flight_data['destination']['city'],
 'depart_time': utc_to_local(flight_data[dep_key], flight_data['origin']['timezone']),
 'arrival_time': utc_to_local(flight_data[arr_key], flight_data['destination']['timezone']),
 'status': flight_data['status']
 }
 return json.dumps(flight_details)

user_functions: Set[Callable[..., Any]] = {
 get_flight_status,
} 

We already imported this in our agent script with the line from user_functions import user_functions. This becomes the action tool for our agent.

Creating a Toolkit for the Agent

We will register both the tools with the toolkit that becomes available to our agent.

# Set up both tools
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
functions = FunctionTool(user_functions)

# Combine tools in a toolset
toolset = ToolSet()
toolset.add(functions)
toolset.add(file_search_tool)

Creating an Azure AI Agent

Let’s define the agent with the instructions and a thread responsible for the final execution.

agent = project_client.agents.create_agent(
 model="gpt-4o-mini",
 name="unified-flight-agent",
 instructions="""You are a helpful agent that can:
 1. Search for information in the baggage policy document
 2. Provide flight status information
 Choose the appropriate tool based on the user's question.""",
 toolset=toolset
)
print(f"Created agent, agent ID: {agent.id}")

# Create a thread
thread = project_client.agents.create_thread()
print(f"Created thread, thread ID: {thread.id}")

Invoke the Agent

With the agent and thread objects in place, we must create the message with the actual task and run the agent. The helper function, process_user_query, does this task.

def process_user_query(query):
 message = project_client.agents.create_message(
 thread_id=thread.id,
 role="user",
 content=query
 )
 print(f"Created message, message ID: {message.id}")

 run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
 print(f"Run status: {run.status}")

 if run.status == "failed":
 print(f"Run failed: {run.last_error}")
 return

 messages = project_client.agents.list_messages(thread_id=thread.id)
 for message in messages['data']:
 content = message['content']
 for item in content:
 if item['type'] == 'text':
 print(f"Message content: {item['text']['value']}")

# Example usage
try: 
 # Test flight status query
 process_user_query("What is the status of flight EK524?")
 # Test baggage policy query
 process_user_query("What is the size of checked baggage?")

finally:
 # Cleanup
 project_client.agents.delete_vector_store(vector_store.id)
 print("Deleted vector store")
 
 project_client.agents.delete_agent(agent.id)
 print("Deleted agent")

Notice that we are sending queries that will be routed to the vector store or a function call. The LLM determines this at runtime.

When we execute, we see the below output.

👁 Image

I hope you found this tutorial useful in building your first AI Agent on Azure.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.