Skip to content

Commit 7dbe45e

Browse files
committed
Refactored initialization code. Fixed readme bug.
Signed-off-by: Paul Yuknewicz <[email protected]>
1 parent 4ea178b commit 7dbe45e

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Take note of the value of `AZURE_OPENAI_ENDPOINT` which can be found in `./.azur
3737
AZURE_OPENAI_ENDPOINT="https://cog-<unique string>.openai.azure.com/"
3838
```
3939

40-
5) Add this `local.settings.json` file to the root of the repo folder to simplify local development. Replace `AZURE_OPENAI_ENDPOINT` with your value from step 4. Optionally you can choose a different model deployment in `CHAT_MODEL_DEPLOYMENT_NAME`. This file will be gitignored to protect secrets from committing to your repo, however by default the sample uses Entra identity (user identity and mananaged identity) so it is secretless.
40+
5) Add this `local.settings.json` file to the root of the repo folder to simplify local development. Replace `AZURE_OPENAI_ENDPOINT` with your value from step 4. Optionally you can choose a different model deployment in `AZURE_OPENAI_CHATGPT_DEPLOYMENT`. This file will be gitignored to protect secrets from committing to your repo, however by default the sample uses Entra identity (user identity and mananaged identity) so it is secretless.
4141
```json
4242
{
4343
"IsEncrypted": false,

function_app.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@
99
app = func.FunctionApp()
1010

1111

12-
# Use the Entra Id DefaultAzureCredential to get the token
13-
credential = DefaultAzureCredential()
14-
# Set the API type to `azure_ad`
15-
os.environ["OPENAI_API_TYPE"] = "azure_ad"
16-
# Set the API_KEY to the token from the Azure credential
17-
os.environ["OPENAI_API_KEY"] = credential.get_token(
18-
"https://cognitiveservices.azure.com/.default"
19-
).token
12+
# Initializes Azure OpenAI environment
13+
def init():
14+
global credential
15+
global AZURE_OPENAI_ENDPOINT
16+
global AZURE_OPENAI_KEY
17+
global AZURE_OPENAI_CHATGPT_DEPLOYMENT
18+
global OPENAI_API_VERSION
2019

20+
# Use the Entra Id DefaultAzureCredential to get the token
21+
credential = DefaultAzureCredential()
22+
# Set the API type to `azure_ad`
23+
os.environ["OPENAI_API_TYPE"] = "azure_ad"
24+
# Set the API_KEY to the token from the Azure credential
25+
os.environ["OPENAI_API_KEY"] = credential.get_token(
26+
"https://cognitiveservices.azure.com/.default"
27+
).token
2128

22-
@app.function_name(name="ask")
23-
@app.route(route="ask", auth_level="function", methods=["POST"])
24-
def main(req):
25-
26-
try:
27-
req_body = req.get_json()
28-
prompt = req_body.get("prompt")
29-
except ValueError:
30-
raise RuntimeError("prompt data must be set in POST.")
31-
else:
32-
if not prompt:
33-
raise RuntimeError("prompt data must be set in POST.")
34-
35-
# Init OpenAI: configure these using Env Variables
29+
# Initialize Azure OpenAI environment
3630
AZURE_OPENAI_ENDPOINT = os.environ.get("AZURE_OPENAI_ENDPOINT")
3731
AZURE_OPENAI_KEY = credential.get_token(
3832
"https://cognitiveservices.azure.com/.default"
@@ -42,14 +36,32 @@ def main(req):
4236
OPENAI_API_VERSION = os.environ.get(
4337
"OPENAI_API_VERSION") or "2023-05-15"
4438

45-
# configure azure openai for langchain and/or llm
39+
# Configure base OpenAI framework for LangChain and/or llm
4640
openai.api_key = AZURE_OPENAI_KEY
4741
openai.api_base = AZURE_OPENAI_ENDPOINT
4842
openai.api_type = "azure"
49-
50-
# this may change in the future
5143
openai.api_version = OPENAI_API_VERSION
5244

45+
46+
# Initialize Azure OpenAI environment
47+
init()
48+
49+
50+
# Function App entry point route for /api/ask
51+
@app.function_name(name="ask")
52+
@app.route(route="ask", auth_level="function", methods=["POST"])
53+
def main(req):
54+
55+
try:
56+
req_body = req.get_json()
57+
prompt = req_body.get("prompt")
58+
except ValueError:
59+
raise RuntimeError("prompt data must be set in POST.")
60+
else:
61+
if not prompt:
62+
raise RuntimeError("prompt data must be set in POST.")
63+
64+
# LangChain user code goes here
5365
llm = AzureChatOpenAI(
5466
deployment_name=AZURE_OPENAI_CHATGPT_DEPLOYMENT,
5567
temperature=0.3,

0 commit comments

Comments
 (0)