Azure OpenAI with Microsoft 365 Copilot
Quick answer
Use the
AzureOpenAI client from the openai Python package configured with your Azure endpoint and API key to interact with Microsoft 365 Copilot. Microsoft 365 Copilot leverages Azure OpenAI models like gpt-4o for enterprise productivity, enabling AI-powered assistance within Office apps.PREREQUISITES
Python 3.8+Azure OpenAI API keyAzure OpenAI endpoint URLpip install openai>=1.0
Setup
Install the openai Python package and set environment variables for your Azure OpenAI API key and endpoint. Use the AzureOpenAI client to authenticate.
pip install openai>=1.0 output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to call Azure OpenAI with the gpt-4o model, which powers Microsoft 365 Copilot features. Replace environment variables with your Azure OpenAI resource details.
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"], # e.g., "gpt-4o"
messages=[{"role": "user", "content": "Summarize the latest Microsoft 365 Copilot features."}]
)
print("Response:", response.choices[0].message.content) output
Response: Microsoft 365 Copilot integrates AI-powered assistance directly into Office apps, enabling users to generate content, automate tasks, and analyze data efficiently.
Common variations
- Use async calls with
asynciofor non-blocking integration. - Switch models by changing the deployment name in
modelparameter. - Enable streaming responses by adding
stream=Truetochat.completions.create.
import asyncio
from openai import AzureOpenAI
async def main():
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
stream = await client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Explain Microsoft 365 Copilot."}],
stream=True
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
asyncio.run(main()) output
Microsoft 365 Copilot is an AI-powered assistant integrated into Office apps that helps users create, edit, and analyze content more efficiently.
Troubleshooting
- If you get authentication errors, verify your
AZURE_OPENAI_API_KEYandAZURE_OPENAI_ENDPOINTenvironment variables are correct. - Ensure your Azure OpenAI deployment name matches the
modelparameter exactly. - For network issues, check firewall rules and Azure resource permissions.
Key Takeaways
- Use the
AzureOpenAIclient with your Azure endpoint and API key to access Microsoft 365 Copilot models. - Set the deployment name in
modelto target specific Azure OpenAI models likegpt-4o. - Async and streaming calls improve responsiveness in interactive applications.
- Verify environment variables and deployment names to avoid common authentication and configuration errors.