Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/tLTGCA4G)
---
title: "Activity 1 - Hello, Azure AI"
type: lab
Expand Down
110 changes: 81 additions & 29 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ def _get_openai_client():
global _openai_client
if _openai_client is None:
# TODO: Uncomment and configure
# from openai import AzureOpenAI
# _openai_client = AzureOpenAI(
# azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
# api_key=os.environ["AZURE_OPENAI_API_KEY"],
# api_version="2024-10-21",
# )
raise NotImplementedError("Configure the Azure OpenAI client")
from openai import AzureOpenAI
_openai_client = AzureOpenAI(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version="2024-10-21",
)
return _openai_client


def _get_content_safety_client():
"""Lazily initialize the Azure Content Safety client."""
Expand All @@ -62,13 +61,12 @@ def _get_content_safety_client():
# NOTE: The Content Safety SDK handles API versioning internally --
# no api_version parameter is needed (unlike the OpenAI SDK).
# TODO: Uncomment and configure
# from azure.ai.contentsafety import ContentSafetyClient
# from azure.core.credentials import AzureKeyCredential
# _content_safety_client = ContentSafetyClient(
# endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
# credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
# )
raise NotImplementedError("Configure the Content Safety client")
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
_content_safety_client = ContentSafetyClient(
endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
)
return _content_safety_client


Expand All @@ -79,13 +77,12 @@ def _get_language_client():
# NOTE: The Language SDK handles API versioning internally --
# no api_version parameter is needed (unlike the OpenAI SDK).
# TODO: Uncomment and configure
# from azure.ai.textanalytics import TextAnalyticsClient
# from azure.core.credentials import AzureKeyCredential
# _language_client = TextAnalyticsClient(
# endpoint=os.environ["AZURE_AI_LANGUAGE_ENDPOINT"],
# credential=AzureKeyCredential(os.environ["AZURE_AI_LANGUAGE_KEY"]),
# )
raise NotImplementedError("Configure the AI Language client")
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
_language_client = TextAnalyticsClient(
endpoint=os.environ["AZURE_AI_LANGUAGE_ENDPOINT"],
credential=AzureKeyCredential(os.environ["AZURE_AI_LANGUAGE_KEY"]),
)
return _language_client


Expand All @@ -102,14 +99,39 @@ def classify_311_request(request_text: str) -> dict:
dict with keys: category, confidence, reasoning
"""
# TODO: Step 1.1 - Get the OpenAI client
AiClient = _get_openai_client()
# TODO: Step 1.2 - Call client.chat.completions.create() with:
# model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o")
# A system message that classifies into: Pothole, Noise Complaint,
# Trash/Litter, Street Light, Water/Sewer, Other
# response_format={"type": "json_object"}, temperature=0
X = AiClient.chat.completions.create(
model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o"),
# A system message that classifies into: Pothole, Noise Complaint,
# Trash/Litter, Street Light, Water/Sewer, Other
messages = [
{
"role": "system",
"content": (
"You are a complaint classification system. "
"Classify the user's complaint text into one of these six categories: "
"Pothole, Noise Complaint, Trash/Litter, Street Light, Water/Sewer, Other. "
"Return a response as a JSON object with three fields: "
"'category', 'confidence', and 'reasoning'."
),
},
{
"role": "user",
"content": request_text,
},
],
response_format={"type": "json_object"},
temperature=0,
)

# TODO: Step 1.3 - Parse the JSON response with json.loads()
raise NotImplementedError("Implement classify_311_request in Step 1")

Y = json.loads(X.choices[0].message.content)

return Y

raise NotImplementedError("Implement classify_311_request in Step 1")

# ---------------------------------------------------------------------------
# TODO: Step 2 - Check content safety
Expand All @@ -124,10 +146,26 @@ def check_content_safety(text: str) -> dict:
dict with keys: safe (bool), categories (dict of category: severity)
"""
# TODO: Step 2.1 - Get the Content Safety client

Sclient = _get_content_safety_client()

# TODO: Step 2.2 - Call client.analyze_text() with AnalyzeTextOptions

from azure.ai.contentsafety.models import AnalyzeTextOptions

Z = Sclient.analyze_text(AnalyzeTextOptions(text=text))


# TODO: Step 2.3 - Return safety results
raise NotImplementedError("Implement check_content_safety in Step 2")

categories = {c.category: c.severity for c in Z.categories_analysis}
safe = all(severity == 0 for severity in categories.values())

# Return final structured response
return {
"safe": safe,
"categories": categories
}

# ---------------------------------------------------------------------------
# TODO: Step 3 - Extract key phrases
Expand All @@ -142,9 +180,23 @@ def extract_key_phrases(text: str) -> list[str]:
List of key phrase strings.
"""
# TODO: Step 3.1 - Get the Language client

LClient = _get_language_client()

# TODO: Step 3.2 - Call client.extract_key_phrases([text])

response = LClient.extract_key_phrases([text])

# TODO: Step 3.3 - Return the list of key phrases
raise NotImplementedError("Implement extract_key_phrases in Step 3")

key_phrases = [
phrase
for item in response
if not item.is_error
for phrase in item.key_phrases
]

return key_phrases


def main():
Expand Down
Loading