Skip to content

Commit 8a30aa8

Browse files
author
Saurabh Badenkal
committed
Add pro-dev quick start example, switch LLM to azure-ai-inference
1 parent 976ef58 commit 8a30aa8

2 files changed

Lines changed: 458 additions & 20 deletions

File tree

examples/advanced/datascience_risk_assessment.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
2727
Additional libraries (optional -- used for visualization and LLM; not part
2828
of the SDK and must be installed separately):
29-
pip install matplotlib # for charts / visualization
30-
pip install openai # for LLM summarization (Azure OpenAI or OpenAI)
29+
pip install matplotlib # for charts / visualization
30+
pip install azure-ai-inference # for LLM summarization (Azure AI Foundry / Azure OpenAI)
3131
"""
3232

3333
import sys
@@ -50,11 +50,12 @@
5050
HAS_MATPLOTLIB = False
5151

5252
try:
53-
from openai import AzureOpenAI
53+
from azure.ai.inference import ChatCompletionsClient
54+
from azure.core.credentials import AzureKeyCredential
5455

55-
HAS_OPENAI = True
56+
HAS_AZURE_AI = True
5657
except ImportError:
57-
HAS_OPENAI = False
58+
HAS_AZURE_AI = False
5859

5960

6061
# ================================================================
@@ -282,7 +283,7 @@ def classify_risk(score):
282283
# ================================================================
283284

284285

285-
def step3_summarize(risk_df, azure_openai_endpoint=None, azure_openai_key=None):
286+
def step3_summarize(risk_df, azure_ai_endpoint=None, azure_ai_key=None):
286287
"""Generate per-account risk summaries using LLM or template fallback."""
287288
print("\n" + "=" * 60)
288289
print("STEP 3: Generate risk summaries")
@@ -292,11 +293,11 @@ def step3_summarize(risk_df, azure_openai_endpoint=None, azure_openai_key=None):
292293
flagged = risk_df[risk_df["risk_tier"].isin(["High", "Medium"])].copy()
293294
print(f"[INFO] Generating summaries for {len(flagged)} flagged accounts")
294295

295-
if HAS_OPENAI and azure_openai_endpoint:
296-
print("[INFO] Using Azure OpenAI for LLM summarization")
297-
summaries = _summarize_with_llm(flagged, azure_openai_endpoint, azure_openai_key)
296+
if HAS_AZURE_AI and azure_ai_endpoint:
297+
print("[INFO] Using Azure AI Inference for LLM summarization")
298+
summaries = _summarize_with_llm(flagged, azure_ai_endpoint, azure_ai_key)
298299
else:
299-
print("[INFO] Using template-based summarization (install openai for LLM)")
300+
print("[INFO] Using template-based summarization (install azure-ai-inference for LLM)")
300301
summaries = _summarize_with_template(flagged)
301302

302303
flagged["risk_summary"] = summaries
@@ -316,11 +317,10 @@ def step3_summarize(risk_df, azure_openai_endpoint=None, azure_openai_key=None):
316317

317318

318319
def _summarize_with_llm(flagged_df, endpoint, api_key):
319-
"""Use Azure OpenAI to generate risk narratives."""
320-
client = AzureOpenAI(
321-
azure_endpoint=endpoint,
322-
api_key=api_key,
323-
api_version="2024-02-01",
320+
"""Use Azure AI Inference (Azure OpenAI / Azure AI Foundry) for risk narratives."""
321+
client = ChatCompletionsClient(
322+
endpoint=endpoint,
323+
credential=AzureKeyCredential(api_key),
324324
)
325325

326326
summaries = []
@@ -337,11 +337,12 @@ def _summarize_with_llm(flagged_df, endpoint, api_key):
337337
Avg Close Probability: {row["avg_close_probability"]:.0f}%
338338
""")
339339

340-
response = client.chat.completions.create(
341-
model="gpt-4o",
340+
from azure.ai.inference.models import SystemMessage, UserMessage
341+
342+
response = client.complete(
342343
messages=[
343-
{"role": "system", "content": "You are a risk analyst. Be concise and actionable."},
344-
{"role": "user", "content": prompt},
344+
SystemMessage(content="You are a risk analyst. Be concise and actionable."),
345+
UserMessage(content=prompt),
345346
],
346347
max_tokens=150,
347348
temperature=0.3,
@@ -517,7 +518,7 @@ def run_risk_pipeline(client):
517518
risk_df = step2_analyze(accounts, cases, opportunities)
518519

519520
# Step 3: LLM-powered risk summarization
520-
# To use Azure OpenAI, set these values:
521+
# To use Azure AI Inference (Azure OpenAI / AI Foundry), set these values:
521522
azure_endpoint = None # e.g. "https://your-resource.openai.azure.com/"
522523
azure_key = None # Your API key
523524
risk_df = step3_summarize(risk_df, azure_endpoint, azure_key)

0 commit comments

Comments
 (0)