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