Skip to content

Commit 5a2077e

Browse files
committed
add python samples
1 parent 51810db commit 5a2077e

File tree

1 file changed

+111
-28
lines changed

1 file changed

+111
-28
lines changed

articles/ai-services/content-understanding/tutorial/RAG-tutorial.md

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ ms.custom: 2025-understanding-release
1515

1616
This tutorial provides a comprehensive guide to building a Retrieval Augmented Generation (RAG) solution using Azure AI Content Understanding. It explains the essential components required to design and implement a robust RAG system, highlights best practices for optimizing relevance and accuracy, and outlines the integration points with other Azure services. By the end of this tutorial, you will have a clear understanding of how to leverage Content Understanding to process multimodal data, enhance retrieval precision, and enable generative AI models to deliver contextually rich and accurate responses.
1717

18-
Sample code can be found in this [Python notebook]((https://github.com/Azure-Samples/azure-ai-search-with-content-understanding-python#samples)), but we recommend using this walkthrough for context, insights, and for exploring alternative approaches.
19-
2018
## Exercises Covered in This Tutorial
2119

2220
1. **Creating Analyzers:** Learn how to create reusable analyzers to extract structured content from multimodal data using content extraction.
@@ -38,12 +36,18 @@ To get started, you need **An active Azure subscription**. If you don't have an
3836
3937
:::image type="content" source="../media/overview/azure-multi-service-resource.png" alt-text="Screenshot of the multi-service resource page in the Azure portal.":::
4038

41-
* In this tutorial, we use the cURL command line tool. If it isn't installed, you can download a version for your dev environment:
39+
- **Azure AI Search Resource:** Set up an [Azure AI Search resource](https://github.com/tonyeiyalla/azure-ai-search-with-content-understanding-python/blob/tonye-cu-rag/docs/create_azure_ai_service.md) to enable indexing and retrieval of multimodal data.
40+
- **Azure OpenAI Resource:** Deploy an [Azure OpenAI resource](https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) with a chat model to enable conversational interactions.
41+
- **Embedding Model Deployment:** Ensure you have an embedding model deployed to generate vector representations for semantic search.
42+
- **API Version:** This tutorial uses the latest preview [API version](https://review.learn.microsoft.com/en-us/rest/api/contentunderstanding/operation-groups?view=rest-contentunderstanding-2024-12-01-preview&preserve-view=true): `2024-12-01-preview`.
43+
- **Python Environment:** Install [Python 3.11](https://www.python.org/downloads/) to execute the provided code samples and scripts.
44+
- This tutorial follows this sample code can be found in this [Python notebook]((https://github.com/Azure-Samples/azure-ai-search-with-content-understanding-python#samples)). Follow this [README]() to create essential resources, grant resources the right Access control(IAM) roles and install all packages needed for this tutorial.
4245

43-
* [Windows](https://curl.haxx.se/windows/)
44-
* [Mac or Linux](https://learn2torials.com/thread/how-to-install-curl-on-mac-or-linux-(ubuntu)-or-windows)
46+
- Additionally, this tutorial utilizes the cURL command-line tool for API interactions. If it is not already installed, you can download it for your development environment:
47+
- [Windows](https://curl.haxx.se/windows/)
48+
- [Mac or Linux](https://learn2torials.com/thread/how-to-install-curl-on-mac-or-linux-(ubuntu)-or-windows)
4549

46-
* The multimodal data used in this tutorial includes sample documents, including documents, images, audio and video designed to guide you through the process of building a robust RAG solution with Azure AI Content Understanding.
50+
* The [multimodal data]() used in this tutorial includes sample documents, including documents, images, audio and video designed to guide you through the process of building a robust RAG solution with Azure AI Content Understanding.
4751

4852
## Extracting Data with Content Understanding: Key Concepts
4953
Building a robust multimodal RAG solution begins with extracting and structuring data from diverse content types. Azure AI Content Understanding provides three key components to facilitate this process: **content extraction**, **field extraction**, and **analyzers**. Together, these components form the foundation for creating a unified, reusable, and enhanced data pipeline for RAG workflows.
@@ -197,39 +201,118 @@ First, create a JSON file named `request_body.json` with the following content:
197201

198202
---
199203

204+
Load all environment variables and libraries from Langchain
200205

201-
Before running the following `cURL` commands, make the following changes to the HTTP request:
202-
203-
1. Replace `{endpoint}` and `{key}` with the endpoint and key values from your Azure portal Azure AI Services instance.
204-
1. Replace `{analyzerId}` with the name of the new analyzer and create, such as `myInvoice`.
206+
``` python
205207

206-
### PUT Request
208+
import os
209+
from dotenv import load_dotenv
210+
load_dotenv()
211+
212+
# Load and validate Azure AI Services configs
213+
AZURE_AI_SERVICE_ENDPOINT = os.getenv("AZURE_AI_SERVICE_ENDPOINT")
214+
AZURE_AI_SERVICE_API_VERSION = os.getenv("AZURE_AI_SERVICE_API_VERSION") or "2024-12-01-preview"
215+
AZURE_DOCUMENT_INTELLIGENCE_API_VERSION = os.getenv("AZURE_DOCUMENT_INTELLIGENCE_API_VERSION") or "2024-11-30"
216+
217+
# Load and validate Azure OpenAI configs
218+
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
219+
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME")
220+
AZURE_OPENAI_CHAT_API_VERSION = os.getenv("AZURE_OPENAI_CHAT_API_VERSION") or "2024-08-01-preview"
221+
AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME")
222+
AZURE_OPENAI_EMBEDDING_API_VERSION = os.getenv("AZURE_OPENAI_EMBEDDING_API_VERSION") or "2023-05-15"
223+
224+
# Load and validate Azure Search Services configs
225+
AZURE_SEARCH_ENDPOINT = os.getenv("AZURE_SEARCH_ENDPOINT")
226+
AZURE_SEARCH_INDEX_NAME = os.getenv("AZURE_SEARCH_INDEX_NAME") or "sample-doc-index"
227+
228+
# Import libraries from Langchain
229+
from langchain import hub
230+
from langchain_openai import AzureChatOpenAI
231+
from langchain_openai import AzureOpenAIEmbeddings
232+
from langchain.schema import StrOutputParser
233+
from langchain.schema.runnable import RunnablePassthrough
234+
from langchain.text_splitter import MarkdownHeaderTextSplitter
235+
from langchain.vectorstores.azuresearch import AzureSearch
236+
from langchain_core.prompts import ChatPromptTemplate
237+
from langchain.schema import Document
238+
import requests
239+
import json
240+
import sys
241+
import uuid
242+
from pathlib import Path
243+
from dotenv import find_dotenv, load_dotenv
244+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
245+
246+
# Add the parent directory to the path to use shared modules
247+
parent_dir = Path(Path.cwd()).parent
248+
sys.path.append(str(parent_dir))
207249

208-
```bash
209-
curl -i -X PUT "{endpoint}/contentunderstanding/analyzers/{analyzerId}?api-version=2024-12-01-preview" \
210-
-H "Ocp-Apim-Subscription-Key: {key}" \
211-
-H "Content-Type: application/json" \
212-
-d @request_body.json
213250
```
251+
---
214252

215-
### PUT Response
216-
217-
The 201 (`Created`) response includes an `Operation-Location` header containing a URL that you can use to track the status of this asynchronous creation operation.
253+
Create analyzers using the schema definition from above
218254

219-
```
220-
201 Created
221-
Operation-Location: {endpoint}/contentunderstanding/analyzers/{analyzerId}/operations/{operationId}?api-version=2024-12-01-preview
222-
```
255+
``` python
256+
from pathlib import Path
257+
from python.content_understanding_client import AzureContentUnderstandingClient
258+
credential = DefaultAzureCredential()
259+
token_provider = get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
260+
261+
#set analyzer configs
262+
analyzer_configs = [
263+
{
264+
"id": "doc-analyzer" + str(uuid.uuid4()),
265+
"template_path": "../analyzer_templates/content_document.json",
266+
"location": Path("../data/sample_layout.pdf"),
267+
},
268+
{
269+
"id": "image-analyzer" + str(uuid.uuid4()),
270+
"template_path": "../analyzer_templates/image_chart_diagram_understanding.json",
271+
"location": Path("../data/sample_report.pdf"),
272+
},
273+
{
274+
"id": "audio-analyzer" + str(uuid.uuid4()),
275+
"template_path": "../analyzer_templates/call_recording_analytics.json",
276+
"location": Path("../data/callCenterRecording.mp3"),
277+
},
278+
{
279+
"id": "video-analyzer" + str(uuid.uuid4()),
280+
"template_path": "../analyzer_templates/video_content_understanding.json",
281+
"location": Path("../data/FlightSimulator.mp4"),
282+
},
283+
]
284+
285+
# Create Content Understanding client
286+
content_understanding_client = AzureContentUnderstandingClient(
287+
endpoint=AZURE_AI_SERVICE_ENDPOINT,
288+
api_version=AZURE_AI_SERVICE_API_VERSION,
289+
token_provider=token_provider,
290+
x_ms_useragent="azure-ai-content-understanding-python/content_extraction", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.
291+
)
223292

224-
Upon completion, performing an HTTP GET on the URL returns `"status": "succeeded"`.
293+
# Iterate through each config and create an analyzer
294+
for analyzer in analyzer_configs:
295+
analyzer_id = analyzer["id"]
296+
template_path = analyzer["template_path"]
297+
298+
try:
299+
300+
# Create the analyzer using the content understanding client
301+
response = content_understanding_client.begin_create_analyzer(
302+
analyzer_id=analyzer_id,
303+
analyzer_template_path=template_path
304+
)
305+
result = content_understanding_client.poll_result(response)
306+
print(f"Successfully created analyzer: {analyzer_id}")
307+
308+
except Exception as e:
309+
print(f"Failed to create analyzer: {analyzer_id}")
310+
print(f"Error: {e}")
225311

226-
```bash
227-
curl -i -X GET "{endpoint}/contentunderstanding/analyzers/{analyzerId}/operations/{operationId}?api-version=2024-12-01-preview" \
228-
-H "Ocp-Apim-Subscription-Key: {key}"
229312
```
230-
231313
---
232314

315+
233316
## Perform Content and Field Analysis
234317
**Content extraction** is the first step in the RAG implementation process. It transforms raw multimodal data—such as documents, images, audio, and video—into structured, searchable formats. This foundational step ensures that the content is organized and ready for indexing and retrieval. Content extraction provides the baseline for indexing and retrieval but may not fully address domain-specific needs or provide deeper contextual insights.
235318
[Learn more]() about content extraction capabilities for each modality.

0 commit comments

Comments
 (0)