Created
August 8, 2025 21:55
-
-
Save pamelafox/06d367eef7a9991ccf21d7a6f43c13ef to your computer and use it in GitHub Desktop.
GPT-5 + Python + OpenAI SDK
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
| import os | |
| import azure.identity | |
| import openai | |
| # Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API | |
| API_HOST = os.getenv("API_HOST", "azure") | |
| if API_HOST == "azure": | |
| token_provider = azure.identity.get_bearer_token_provider( | |
| azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default" | |
| ) | |
| client = openai.AzureOpenAI( | |
| api_version="2024-03-01-preview", | |
| azure_endpoint="https://cog-icygqdubf4x6w-gpt5.openai.azure.com", | |
| azure_ad_token_provider=token_provider, | |
| ) | |
| MODEL_NAME = "gpt-5-mini" | |
| elif API_HOST == "ollama": | |
| client = openai.OpenAI(base_url=os.environ["OLLAMA_ENDPOINT"], api_key="nokeyneeded") | |
| MODEL_NAME = os.environ["OLLAMA_MODEL"] | |
| elif API_HOST == "github": | |
| client = openai.OpenAI(base_url="https://models.github.ai/inference", api_key=os.environ["GITHUB_TOKEN"]) | |
| MODEL_NAME = os.getenv("GITHUB_MODEL", "openai/gpt-5-mini") | |
| else: | |
| client = openai.OpenAI(api_key=os.environ["OPENAI_KEY"]) | |
| MODEL_NAME = os.environ["OPENAI_MODEL"] | |
| response = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| n=1, | |
| messages=[ | |
| {"role": "user", "content": "Write a Python program that converts a PNG file to base64."}, | |
| ], | |
| reasoning_effort="minimal", | |
| stream_options={"include_usage": True}, | |
| stream=True, | |
| verbosity="high" | |
| ) | |
| print(f"Response from {API_HOST}: \n") | |
| for event in response: | |
| if event.choices: | |
| content = event.choices[0].delta.content | |
| if content: | |
| print(content, end="", flush=True) | |
| if event.usage: | |
| print(f"\n\nUsage: {event.usage}", flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment