Skip to content

Commit e0deec0

Browse files
Complete Activity 1
1 parent b0f93b2 commit e0deec0

File tree

1 file changed

+68
-26
lines changed

1 file changed

+68
-26
lines changed

app/main.py

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def _get_openai_client():
4545
global _openai_client
4646
if _openai_client is None:
4747
# TODO: Uncomment and configure
48-
# from openai import AzureOpenAI
49-
# _openai_client = AzureOpenAI(
50-
# azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
51-
# api_key=os.environ["AZURE_OPENAI_API_KEY"],
52-
# api_version="2024-10-21",
53-
# )
54-
raise NotImplementedError("Configure the Azure OpenAI client")
48+
from openai import AzureOpenAI
49+
_openai_client = AzureOpenAI(
50+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
51+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
52+
api_version="2024-10-21",
53+
)
54+
5555
return _openai_client
5656

5757

@@ -62,13 +62,13 @@ def _get_content_safety_client():
6262
# NOTE: The Content Safety SDK handles API versioning internally --
6363
# no api_version parameter is needed (unlike the OpenAI SDK).
6464
# TODO: Uncomment and configure
65-
# from azure.ai.contentsafety import ContentSafetyClient
66-
# from azure.core.credentials import AzureKeyCredential
67-
# _content_safety_client = ContentSafetyClient(
68-
# endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
69-
# credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
70-
# )
71-
raise NotImplementedError("Configure the Content Safety client")
65+
from azure.ai.contentsafety import ContentSafetyClient
66+
from azure.core.credentials import AzureKeyCredential
67+
_content_safety_client = ContentSafetyClient(
68+
endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
69+
credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
70+
)
71+
7272
return _content_safety_client
7373

7474

@@ -79,13 +79,13 @@ def _get_language_client():
7979
# NOTE: The Language SDK handles API versioning internally --
8080
# no api_version parameter is needed (unlike the OpenAI SDK).
8181
# TODO: Uncomment and configure
82-
# from azure.ai.textanalytics import TextAnalyticsClient
83-
# from azure.core.credentials import AzureKeyCredential
84-
# _language_client = TextAnalyticsClient(
85-
# endpoint=os.environ["AZURE_AI_LANGUAGE_ENDPOINT"],
86-
# credential=AzureKeyCredential(os.environ["AZURE_AI_LANGUAGE_KEY"]),
87-
# )
88-
raise NotImplementedError("Configure the AI Language client")
82+
from azure.ai.textanalytics import TextAnalyticsClient
83+
from azure.core.credentials import AzureKeyCredential
84+
_language_client = TextAnalyticsClient(
85+
endpoint=os.environ["AZURE_AI_LANGUAGE_ENDPOINT"],
86+
credential=AzureKeyCredential(os.environ["AZURE_AI_LANGUAGE_KEY"]),
87+
)
88+
8989
return _language_client
9090

9191

@@ -103,12 +103,41 @@ def classify_311_request(request_text: str) -> dict:
103103
"""
104104
# TODO: Step 1.1 - Get the OpenAI client
105105
# TODO: Step 1.2 - Call client.chat.completions.create() with:
106-
# model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o")
106+
107107
# A system message that classifies into: Pothole, Noise Complaint,
108108
# Trash/Litter, Street Light, Water/Sewer, Other
109-
# response_format={"type": "json_object"}, temperature=0
109+
model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o"),
110+
response = client.chat.completions.create(
111+
112+
messages=[
113+
{
114+
"role": "system",
115+
"content": (
116+
"You are a local city complaint classifier. Given a complaint from a resident, "
117+
"classify it into exactly one of the following categories:\n"
118+
"- Pothole\n"
119+
"- Noise Complaint\n"
120+
"- Trash/Litter\n"
121+
"- Street Light\n"
122+
"- Water/Sewer\n"
123+
"- Other\n\n"
124+
"Respond with a JSON object containing exactly these fields:\n"
125+
" - \"category\": one of the six categories listed above (string)\n"
126+
" - \"confidence\": your confidence score between 0.0 and 1.0 (number, not a percentage)\n"
127+
" - \"reasoning\": a short explanation of why you chose this category (string)"
128+
)
129+
},
130+
{
131+
"role": "user",
132+
"content": request_text
133+
}
134+
],
135+
response_format={"type": "json_object"}, temperature=0
136+
)
110137
# TODO: Step 1.3 - Parse the JSON response with json.loads()
111-
raise NotImplementedError("Implement classify_311_request in Step 1")
138+
result = json.loads(response.choices[0].message.content)
139+
return result
140+
112141

113142

114143
# ---------------------------------------------------------------------------
@@ -125,8 +154,17 @@ def check_content_safety(text: str) -> dict:
125154
"""
126155
# TODO: Step 2.1 - Get the Content Safety client
127156
# TODO: Step 2.2 - Call client.analyze_text() with AnalyzeTextOptions
157+
from azure.ai.contentsafety.models import AnalyzeTextOptions
158+
159+
def analyze_text(text: str):
160+
result = client.analyze_text(AnalyzeTextOptions(text=text))
161+
162+
return result
128163
# TODO: Step 2.3 - Return safety results
129-
raise NotImplementedError("Implement check_content_safety in Step 2")
164+
categories = {item.category: item.severity for item in result.categories_analysis}
165+
safe = all(severity == 0 for severity in categories.values())
166+
167+
return {"safe": True, "categories": categories}
130168

131169

132170
# ---------------------------------------------------------------------------
@@ -143,8 +181,12 @@ def extract_key_phrases(text: str) -> list[str]:
143181
"""
144182
# TODO: Step 3.1 - Get the Language client
145183
# TODO: Step 3.2 - Call client.extract_key_phrases([text])
184+
response = client.extract_key_phrases([text])
146185
# TODO: Step 3.3 - Return the list of key phrases
147-
raise NotImplementedError("Implement extract_key_phrases in Step 3")
186+
if response[0].is_error:
187+
return []
188+
189+
return response[0].key_phrases
148190

149191

150192
def main():

0 commit comments

Comments
 (0)