Skip to content

Commit 0930e9d

Browse files
authored
Merge pull request #32 from Azure-Samples/copilot/fix-31
Migrate AzureOpenAI constructors to OpenAI client
2 parents a081e95 + 396b7d7 commit 0930e9d

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,7 @@ A related option is VS Code Dev Containers, which will open the project in your
117117
azd env set AZURE_LOCATION "yournewlocationname"
118118
```
119119

120-
3. When `azd` has finished, you should have an OpenAI account you can use locally when logged into your Azure account. You can output the necessary environment variables into an `.env` file by running a script:
121-
122-
For Mac OS X / Linux:
123-
124-
```shell
125-
./write_dot_env.sh
126-
```
127-
128-
For Windows:
129-
130-
```shell
131-
pwsh ./write_dot_env.ps1
132-
```
120+
3. Once the resources are provisioned, you should now see a local `.env` file with all the environment variables needed to run the scripts.
133121

134122
4. Then you can proceed to [run the Python example](#running-the-python-example).
135123

@@ -155,7 +143,7 @@ A related option is VS Code Dev Containers, which will open the project in your
155143
156144
### Costs
157145
158-
This template creates only the Azure OpenAI resource, which is free to provision. However, you will be charged for the usage of the Azure OpenAI chat completions API. The pricing is based on the number of tokens used, with around 1-3 tokens used per word. You can find the pricing details for the OpenAI API on the [Azure Cognitive Services pricing page](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/).
146+
This template creates only the Azure OpenAI resource, which is free to provision. However, you will be charged for the usage of the Azure OpenAI chat completions API. The pricing is based on the number of tokens used, with around 1-3 tokens used per word. You can find the pricing details for the OpenAI API on the [Azure OpenAI pricing page](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/).
159147
160148
### Security guidelines
161149

azure.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,16 @@
22

33
name: azure-openai-keyless-python
44
metadata:
5-
template: azure-openai-keyless-python@0.0.4
5+
template: azure-openai-keyless-python@0.0.5
6+
hooks:
7+
postprovision:
8+
windows:
9+
shell: pwsh
10+
run: ./infra/write_dot_env.ps1
11+
interactive: false
12+
continueOnError: false
13+
posix:
14+
shell: sh
15+
run: ./infra/write_dot_env.sh
16+
interactive: false
17+
continueOnError: false

example.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
exit(1)
1616

1717

18-
credential = azure.identity.DefaultAzureCredential()
18+
# Change this to AzureCliCredential, DefaultAzureCredential, or another credential type if needed
19+
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.environ["AZURE_TENANT_ID"])
1920
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
2021

21-
client = openai.AzureOpenAI(
22-
api_version="2024-03-01-preview",
23-
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
24-
azure_ad_token_provider=token_provider,
22+
client = openai.OpenAI(
23+
base_url=f"https://{os.environ['AZURE_OPENAI_SERVICE']}.openai.azure.com/openai/v1",
24+
api_key=token_provider,
2525
)
2626

2727
response = client.chat.completions.create(
2828
# For Azure OpenAI, the model parameter must be set to the deployment name
29-
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
29+
model=os.environ["AZURE_OPENAI_GPT_DEPLOYMENT"],
3030
temperature=0.7,
31-
n=1,
3231
messages=[
3332
{"role": "system", "content": "You are a helpful assistant that makes lots of cat references and uses emojis."},
3433
{"role": "user", "content": "Write a haiku about a hungry cat who wants tuna"},

write_dot_env.ps1 renamed to infra/write_dot_env.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Set-Content -Path .env -Value ""
44
# Append new values to the .env file
55
$azureOpenAiDeployment = azd env get-value AZURE_OPENAI_GPT_DEPLOYMENT
66
$azureOpenAiService = azd env get-value AZURE_OPENAI_SERVICE
7+
$azureTenantId = azd env get-value AZURE_TENANT_ID
78

89
Add-Content -Path .env -Value "AZURE_OPENAI_GPT_DEPLOYMENT=$azureOpenAiDeployment"
910
Add-Content -Path .env -Value "AZURE_OPENAI_SERVICE=$azureOpenAiService"
11+
Add-Content -Path .env -Value "AZURE_TENANT_ID=$azureTenantId"

write_dot_env.sh renamed to infra/write_dot_env.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
# Append new values to the .env file
77
echo "AZURE_OPENAI_GPT_DEPLOYMENT=$(azd env get-value AZURE_OPENAI_GPT_DEPLOYMENT)" >> .env
88
echo "AZURE_OPENAI_SERVICE=$(azd env get-value AZURE_OPENAI_SERVICE)" >> .env
9+
echo "AZURE_TENANT_ID=$(azd env get-value AZURE_TENANT_ID)" >> .env

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
azure-identity
2-
openai
2+
openai>=1.108.1
33
python-dotenv

0 commit comments

Comments
 (0)