
Integrating OpenAI with Azure might sound intimidating, especially if you’re new to cloud platforms or APIs. Don’t worry! This guide walks you through every step, from setting up your Azure account to writing code that uses your API key to interact with OpenAI. By the end, you’ll understand not just how to do it, but also why each step matters.
What Is an API Key and Why Do You Need One?
An API key is like a password for your application. It tells Azure that your app is allowed to use a particular service (OpenAI in this case). You’ll use this key in your code to prove your identity and access OpenAI models.
Key points:
- Secure authentication for your applications
- Access OpenAI and other Azure services without exposing sensitive credentials
- Ability to monitor usage and manage permissions
Setting Up Your Azure Account
If you don’t have an Azure account yet:
- Go to https://azure.com/free and sign up.
- You’ll need an email and phone number.
- Azure provides free credits for new users.
注意: Some Azure subscriptions or regions may have restricted access to OpenAI services. Make sure your subscription supports OpenAI.
Creating an Azure OpenAI Resource
This is where you’ll host your OpenAI models.
Step-by-Step Instructions
- Log in to the Azure Portal.
- Search for Azure OpenAI in the search bar.
- 点击Azure OpenAI in the results.
- 点击+ Create to create a new resource.
- Fill in the required fields:
- Subscription: Select your subscription.
- Resource Group: Create a new one (例如, openai-demo-rg).
- Region: Choose a nearby region (例如, East US).
- Name: Give your resource a unique name.
- 点击Review + Create, then Create.
- Wait for deployment, then click Go to resource.
Tip: Azure may take a few minutes to deploy your resource.
Deploying a Model
Before using OpenAI, you need to deploy a model.
- In your Azure OpenAI resource, find Model Deployments or Deployments.
- 点击+ Create or + Add.
- Select a model (例如,
gpt-35-turbo,gpt-4). - Name your deployment (例如,
gpt35-demo). - 点击Create.
Why this matters: Deploying a model makes it available for API calls. You’ll reference the deployment name in your code.
注意: Model names may change as Azure updates available models. Always check the official list when deploying.
Getting Your API Key and Endpoint
- In the resource, go to Keys and Endpoint.
- Copy one of the keys (KEY 1 is fine).
- Copy the Endpoint URL (
https://your-resource-name.openai.azure.com/).
Tip: Keep your API key secret. Never share it publicly.
Writing Code to Use Your API Key
Prerequisites
- Python installed from python.org
- Requests library:
pip install requests
Sample Python Code
import requests
# Replace these with your actual values
endpoint = "https://YOUR_RESOURCE_NAME.openai.azure.com/"
api_key = "YOUR_API_KEY"
deployment_name = "YOUR_DEPLOYMENT_NAME"
headers = {
"api-key": api_key,
"Content-Type": "application/json"
}
data = {
"prompt": "Write a short story about a robot learning to paint.",
"max_tokens": 100
}
# Construct the full URL for the API call
url = f"{endpoint}openai/deployments/{deployment_name}/completions?api-version=2024-02-15-preview"
response = requests.post(url, headers=headers, json=data)
# Print the response from the API
print(response.json())Notes:
endpoint: Base URL for your Azure OpenAI resourceapi_key: Your secret API keydeployment_name: The model deployment nameapi-version: Check Azure documentation for the latest version
Running Your Code
- Save as
openai_test.py. - Run in terminal:
python openai_test.pyYou should see a JSON response from the model.
Best Practices and Troubleshooting
- Keep API keys safe; don’t commit to public repositories.
- Rotate keys regularly.
- For production, use environment variables or Azure Key Vault.
Common errors:
401 Unauthorized: Invalid key429 Too Many Requests: Rate limit exceeded
开桥: A Multi-Model AI Integration Alternative
While Azure API keys allow access to OpenAI, managing multiple AI services can be complex. 开桥 provides:
- Multi-Model Integration: One API to access multiple AI models
- Reliability: Automated failover and output quality safeguards
- Simplified Development: Single integration for multiple providers
