Skip to content

Commit ef0ed81

Browse files
committed
test 3
1 parent 26bf695 commit ef0ed81

File tree

1 file changed

+7
-43
lines changed

1 file changed

+7
-43
lines changed

app/main.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def _get_openai_client():
5151
api_key=os.environ["AZURE_OPENAI_API_KEY"],
5252
api_version="2024-10-21",
5353
)
54-
#raise NotImplementedError("Configure the Azure OpenAI client")
5554
return _openai_client
5655

5756

@@ -68,17 +67,13 @@ def _get_content_safety_client():
6867
endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
6968
credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
7069
)
71-
#raise NotImplementedError("Configure the Content Safety client")
7270
return _content_safety_client
7371

7472

7573
def _get_language_client():
7674
"""Lazily initialize the Azure AI Language client."""
7775
global _language_client
7876
if _language_client is None:
79-
# NOTE: The Language SDK handles API versioning internally --
80-
# no api_version parameter is needed (unlike the OpenAI SDK).
81-
# TODO: Uncomment and configure
8277
from azure.ai.textanalytics import TextAnalyticsClient
8378
from azure.core.credentials import AzureKeyCredential
8479
_language_client = TextAnalyticsClient(
@@ -96,7 +91,7 @@ def classify_311_request(request_text: str) -> dict:
9691
response = _get_openai_client().chat.completions.create(
9792
model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o"),
9893
messages=[
99-
{"role": "system", "content": "Classify this Memphis 311 request into one of: Pothole, Noise Complaint, Trash/Litter, Street Light, Water/Sewer, Other. Return JSON with keys: category, confidence, reasoning."},
94+
{"role": "system", "content": "Classify this Memphis 311 request into one of: Pothole, Noise Complaint, Trash/Litter, Street Light, Water/Sewer, Other. Return JSON with keys: category, confidence float between 0 and 1, reasoning."},
10095
{"role": "user", "content": request_text},
10196
],
10297
response_format={"type": "json_object"},
@@ -106,59 +101,28 @@ def classify_311_request(request_text: str) -> dict:
106101
return result
107102

108103

109-
# raise NotImplementedError("Implement classify_311_request in Step 1")
110-
111-
112104
# ---------------------------------------------------------------------------
113105
# TODO: Step 2 - Check content safety
114106
# ---------------------------------------------------------------------------
115107
def check_content_safety(text: str) -> dict:
116108
from azure.ai.contentsafety.models import AnalyzeTextOptions
117-
client = _get_content_safety_client()
118-
response = client.analyze_text(AnalyzeTextOptions(text=text))
119-
109+
result = _get_content_safety_client().analyze_text(AnalyzeTextOptions(text=text))
120110
categories = {
121111
cat.category: cat.severity
122-
for cat in response.categories_analysis
112+
for cat in result.categories_analysis
123113
}
124114
safe = all(severity == 0 for severity in categories.values())
125115
return {"safe": safe, "categories": categories}
126116

127-
"""Check text for harmful content using Azure Content Safety.
128-
129-
Args:
130-
text: Text to analyze.
131-
132-
Returns:
133-
dict with keys: safe (bool), categories (dict of category: severity)
134-
"""
135-
# TODO: Step 2.1 - Get the Content Safety client
136-
# TODO: Step 2.2 - Call client.analyze_text() with AnalyzeTextOptions
137-
# TODO: Step 2.3 - Return safety results
138-
139-
140-
141-
142117

143118
# ---------------------------------------------------------------------------
144119
# TODO: Step 3 - Extract key phrases
145120
# ---------------------------------------------------------------------------
146121
def extract_key_phrases(text: str) -> list[str]:
147-
148-
149-
"""Extract key phrases from text using Azure AI Language.
150-
151-
Args:
152-
text: Text to analyze.
153-
154-
Returns:
155-
List of key phrase strings.
156-
"""
157-
# TODO: Step 3.1 - Get the Language client
158-
# TODO: Step 3.2 - Call client.extract_key_phrases([text])
159-
# TODO: Step 3.3 - Return the list of key phrases
160-
raise NotImplementedError("Implement extract_key_phrases in Step 3")
161-
122+
response = _get_language_client().extract_key_phrases([text])
123+
if response[0].is_error:
124+
raise ValueError(f"Error extracting key phrases: {response[0].error}")
125+
return list(response[0].key_phrases)
162126

163127
def main():
164128
"""Main function -- call all three Azure AI services."""

0 commit comments

Comments
 (0)