Skip to content

Commit cc840bc

Browse files
committed
added openai
1 parent 22154bd commit cc840bc

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

app/main.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import os
1616
import sys
1717
from datetime import datetime, timezone
18-
18+
from openai import AzureOpenAI
1919
from dotenv import load_dotenv
2020

2121
load_dotenv()
@@ -44,14 +44,12 @@ def _get_openai_client():
4444
"""Lazily initialize the Azure OpenAI client."""
4545
global _openai_client
4646
if _openai_client is None:
47-
# 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")
47+
from azure.openai import AzureOpenAI
48+
_openai_client = AzureOpenAI(
49+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
50+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
51+
api_version="2024-10-21",
52+
)
5553
return _openai_client
5654

5755

@@ -62,12 +60,12 @@ def _get_content_safety_client():
6260
# NOTE: The Content Safety SDK handles API versioning internally --
6361
# no api_version parameter is needed (unlike the OpenAI SDK).
6462
# 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-
# )
63+
from azure.ai.contentsafety import ContentSafetyClient
64+
from azure.core.credentials import AzureKeyCredential
65+
_content_safety_client = ContentSafetyClient(
66+
endpoint=os.environ["AZURE_CONTENT_SAFETY_ENDPOINT"],
67+
credential=AzureKeyCredential(os.environ["AZURE_CONTENT_SAFETY_KEY"]),
68+
)
7169
raise NotImplementedError("Configure the Content Safety client")
7270
return _content_safety_client
7371

@@ -79,12 +77,12 @@ def _get_language_client():
7977
# NOTE: The Language SDK handles API versioning internally --
8078
# no api_version parameter is needed (unlike the OpenAI SDK).
8179
# 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-
# )
80+
from azure.ai.textanalytics import TextAnalyticsClient
81+
from azure.core.credentials import AzureKeyCredential
82+
_language_client = TextAnalyticsClient(
83+
endpoint=os.environ["AZURE_AI_LANGUAGE_ENDPOINT"],
84+
credential=AzureKeyCredential(os.environ["AZURE_AI_LANGUAGE_KEY"]),
85+
)
8886
raise NotImplementedError("Configure the AI Language client")
8987
return _language_client
9088

@@ -103,11 +101,24 @@ def classify_311_request(request_text: str) -> dict:
103101
"""
104102
# TODO: Step 1.1 - Get the OpenAI client
105103
# TODO: Step 1.2 - Call client.chat.completions.create() with:
106-
# model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o")
107-
# A system message that classifies into: Pothole, Noise Complaint,
108-
# Trash/Litter, Street Light, Water/Sewer, Other
109-
# response_format={"type": "json_object"}, temperature=0
110-
# TODO: Step 1.3 - Parse the JSON response with json.loads()
104+
client = _get_openai_client()
105+
response = client.chat.completions.create(
106+
model=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o"),
107+
messages=[
108+
{
109+
"role": "system",
110+
"content": (
111+
"Classify the following 311 service request into one of these categories: "
112+
"Pothole, Noise Complaint, Trash/Litter, Street Light, Water/Sewer, Other."
113+
),
114+
},
115+
{"role": "user", "content": request_text},
116+
],
117+
response_format={"type": "json_object"},
118+
temperature=0,
119+
)
120+
# TODO: Step 1.3 - Parse the JSON response with
121+
json.loads()
111122
raise NotImplementedError("Implement classify_311_request in Step 1")
112123

113124

0 commit comments

Comments
 (0)