Last active
March 26, 2025 17:42
-
-
Save JGalego/069dda571fb4543f3a34cae4ec78b628 to your computer and use it in GitHub Desktop.
Quick 'n' dirty 'Ahoy!' demo with Azure OpenAI π΄ββ οΈπ¦
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.7,<=3.11" | |
| # dependencies = [ | |
| # "azure-identity >= 1.2.1", | |
| # "openai >= 1.65.2", | |
| # ] | |
| # /// | |
| """ | |
| Quick 'n' dirty 'Ahoy!' demo with Azure OpenAI | |
| Adapted from 'How to configure Azure OpenAI Service with Microsoft Entra ID authentication' | |
| https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/managed-identity | |
| """ | |
| # Standard imports | |
| import os | |
| # Library imports | |
| from azure.identity import DefaultAzureCredential, get_bearer_token_provider | |
| from openai import AzureOpenAI | |
| token_provider = get_bearer_token_provider( | |
| DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" | |
| ) | |
| client = AzureOpenAI( | |
| api_version="2024-07-01-preview", | |
| azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"), | |
| azure_ad_token_provider=token_provider | |
| ) | |
| response = client.chat.completions.create( | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": "You are a coding assistant that talks like a pirate.", | |
| }, | |
| { | |
| "role": "user", | |
| "content": "How do I check if a Python object is an instance of a class?", | |
| } | |
| ], | |
| max_tokens=4096, | |
| temperature=1.0, | |
| top_p=1.0, | |
| model="gpt-4o-mini" | |
| ) | |
| print(response.choices[0].message.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment