Skip to content

Commit b702232

Browse files
authored
Merge pull request #2 from Azure-Samples/streamchanges
Pre-stream changes
2 parents f2f8fcb + 60aafbc commit b702232

17 files changed

+125
-21
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ To run the samples, you'll either need to have already [deployed the Azure OpenA
155155
156156
| Script filename | Description |
157157
|---------------------------|-----------------------------------------------------------------------------|
158+
| `basic_azure.py` | A basic example that uses deployed Azure OpenAI resource to extract from string input. |
159+
| `basic_githubmodels.py` | A basic example that uses free gpt-4o from GitHub Models to extract from string input. |
158160
| `extract_github_issue.py` | Fetches a public issue using the GitHub API, and then extracts details. |
159161
| `extract_github_repo.py` | Fetches a public README using the GitHub API, and then extracts details. |
160162
| `extract_image_graph.py` | Parses a local image of a graph and extracts details like title, axis, legend. |

basic_azure.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import logging
2+
import os
3+
4+
import rich
5+
from azure.identity import AzureDeveloperCliCredential, get_bearer_token_provider
6+
from dotenv import load_dotenv
7+
from openai import AzureOpenAI
8+
from pydantic import BaseModel
9+
10+
logging.basicConfig(level=logging.WARNING)
11+
load_dotenv()
12+
13+
token_provider = get_bearer_token_provider(
14+
AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID")), "https://cognitiveservices.azure.com/.default"
15+
)
16+
17+
client = AzureOpenAI(
18+
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
19+
azure_ad_token_provider=token_provider,
20+
api_version="2024-10-21",
21+
)
22+
23+
24+
class CalendarEvent(BaseModel):
25+
name: str
26+
date: str
27+
participants: list[str]
28+
29+
30+
completion = client.beta.chat.completions.parse(
31+
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
32+
messages=[
33+
{"role": "system", "content": "Extract the event information."},
34+
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
35+
],
36+
response_format=CalendarEvent,
37+
)
38+
39+
output = completion.choices[0].message.parsed
40+
event = CalendarEvent.model_validate(output)
41+
42+
rich.print(event)

basic_githubmodels.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
3+
import rich
4+
from openai import OpenAI
5+
from pydantic import BaseModel
6+
7+
client = OpenAI(
8+
base_url="https://models.inference.ai.azure.com",
9+
api_key=os.environ["GITHUB_TOKEN"],
10+
# Specify the API version to use the Structured Outputs feature
11+
default_query={"api-version": "2024-08-01-preview"},
12+
)
13+
model_name = "gpt-4o"
14+
15+
16+
class CalendarEvent(BaseModel):
17+
name: str
18+
date: str
19+
participants: list[str]
20+
21+
22+
completion = client.beta.chat.completions.parse(
23+
model=os.getenv("AZURE_OPENAI_GPT_DEPLOYMENT"),
24+
messages=[
25+
{"role": "system", "content": "Extract the event information."},
26+
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
27+
],
28+
response_format=CalendarEvent,
29+
)
30+
31+
output = completion.choices[0].message.parsed
32+
event = CalendarEvent.model_validate(output)
33+
34+
rich.print(event)

extract_github_issue.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1919
exit(1)
2020
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
21-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
21+
token_provider = azure.identity.get_bearer_token_provider(
22+
credential, "https://cognitiveservices.azure.com/.default"
23+
)
2224
client = openai.AzureOpenAI(
2325
api_version="2024-08-01-preview",
2426
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -33,7 +35,8 @@
3335
base_url="https://models.inference.ai.azure.com",
3436
api_key=os.environ["GITHUB_TOKEN"],
3537
# Specify the API version to use the Structured Outputs feature
36-
default_query={"api-version": "2024-08-01-preview"})
38+
default_query={"api-version": "2024-08-01-preview"},
39+
)
3740
model_name = "gpt-4o"
3841

3942

extract_github_repo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1919
exit(1)
2020
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
21-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
21+
token_provider = azure.identity.get_bearer_token_provider(
22+
credential, "https://cognitiveservices.azure.com/.default"
23+
)
2224
client = openai.AzureOpenAI(
2325
api_version="2024-08-01-preview",
2426
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -33,7 +35,8 @@
3335
base_url="https://models.inference.ai.azure.com",
3436
api_key=os.environ["GITHUB_TOKEN"],
3537
# Specify the API version to use the Structured Outputs feature
36-
default_query={"api-version": "2024-08-01-preview"})
38+
default_query={"api-version": "2024-08-01-preview"},
39+
)
3740
model_name = "gpt-4o"
3841

3942

extract_image_graph.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1717
exit(1)
1818
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
19-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
19+
token_provider = azure.identity.get_bearer_token_provider(
20+
credential, "https://cognitiveservices.azure.com/.default"
21+
)
2022
client = openai.AzureOpenAI(
2123
api_version="2024-08-01-preview",
2224
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -31,7 +33,8 @@
3133
base_url="https://models.inference.ai.azure.com",
3234
api_key=os.environ["GITHUB_TOKEN"],
3335
# Specify the API version to use the Structured Outputs feature
34-
default_query={"api-version": "2024-08-01-preview"})
36+
default_query={"api-version": "2024-08-01-preview"},
37+
)
3538
model_name = "gpt-4o"
3639

3740

extract_image_table.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1717
exit(1)
1818
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
19-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
19+
token_provider = azure.identity.get_bearer_token_provider(
20+
credential, "https://cognitiveservices.azure.com/.default"
21+
)
2022
client = openai.AzureOpenAI(
2123
api_version="2024-08-01-preview",
2224
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -31,7 +33,8 @@
3133
base_url="https://models.inference.ai.azure.com",
3234
api_key=os.environ["GITHUB_TOKEN"],
3335
# Specify the API version to use the Structured Outputs feature
34-
default_query={"api-version": "2024-08-01-preview"})
36+
default_query={"api-version": "2024-08-01-preview"},
37+
)
3538
model_name = "gpt-4o"
3639

3740

extract_pdf_receipt.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1717
exit(1)
1818
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
19-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
19+
token_provider = azure.identity.get_bearer_token_provider(
20+
credential, "https://cognitiveservices.azure.com/.default"
21+
)
2022
client = openai.AzureOpenAI(
2123
api_version="2024-08-01-preview",
2224
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -31,7 +33,8 @@
3133
base_url="https://models.inference.ai.azure.com",
3234
api_key=os.environ["GITHUB_TOKEN"],
3335
# Specify the API version to use the Structured Outputs feature
34-
default_query={"api-version": "2024-08-01-preview"})
36+
default_query={"api-version": "2024-08-01-preview"},
37+
)
3538
model_name = "gpt-4o"
3639

3740

extract_webpage.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
logging.warning("AZURE_OPENAI_SERVICE and AZURE_OPENAI_GPT_DEPLOYMENT env variables are empty. See README.")
1818
exit(1)
1919
credential = azure.identity.AzureDeveloperCliCredential(tenant_id=os.getenv("AZURE_TENANT_ID"))
20-
token_provider = azure.identity.get_bearer_token_provider(credential, "https://cognitiveservices.azure.com/.default")
20+
token_provider = azure.identity.get_bearer_token_provider(
21+
credential, "https://cognitiveservices.azure.com/.default"
22+
)
2123
client = openai.AzureOpenAI(
2224
api_version="2024-08-01-preview",
2325
azure_endpoint=f"https://{os.getenv('AZURE_OPENAI_SERVICE')}.openai.azure.com",
@@ -32,9 +34,11 @@
3234
base_url="https://models.inference.ai.azure.com",
3335
api_key=os.environ["GITHUB_TOKEN"],
3436
# Specify the API version to use the Structured Outputs feature
35-
default_query={"api-version": "2024-08-01-preview"})
37+
default_query={"api-version": "2024-08-01-preview"},
38+
)
3639
model_name = "gpt-4o"
3740

41+
3842
# Define models for Structured Outputs
3943
class BlogPost(BaseModel):
4044
title: str

http/.env.azure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
AZURE_OPENAI_SERVICE=
22
AZURE_OPENAI_GPT_DEPLOYMENT=
3-
AZURE_OPENAI_TOKEN=
3+
AZURE_OPENAI_TOKEN=

0 commit comments

Comments
 (0)