azure-openai.mdx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ---
  2. title: "Azure OpenAI"
  3. description: "Integrate Azure OpenAI with Agency Swarm to ensure secure data processing and enhanced privacy."
  4. icon: "microsoft"
  5. ---
  6. Many organizations prioritize data privacy and are cautious about sharing their data with any third-parties. By leveraging Azure OpenAI, you can ensure that your data is processed only within your own secure Azure environment, and not even shared with OpenAI itself.
  7. <Info>
  8. Running OpenAI models on Azure is the same as deploying your own open source model on any other cloud provider.
  9. </Info>
  10. ## Prerequisites
  11. Before you begin, ensure you have the following:
  12. 1. Create an Azure Account with an active subscription. [Create an account here](https://azure.microsoft.com/en-us/free/).
  13. 2. Get approved access to the OpenAI Service on Azure.
  14. 3. Create an Azure OpenAI resource in [one of the available regions](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models#assistants-preview) and deploy a model to it.
  15. 4. Obtain the endpoint URL and API key for the OpenAI resource.
  16. ## Setting Up Azure OpenAI with Agency Swarm
  17. <Steps>
  18. <Step title="Configure the Azure OpenAI Client">
  19. To use Azure OpenAI, you need to configure the client to connect to your Azure OpenAI resource as follows:
  20. ```python
  21. import os
  22. from openai import AsyncAzureOpenAI
  23. # Create a custom AsyncOpenAI client for Azure
  24. azure_agent = AsyncAzureOpenAI(
  25. api_key=os.getenv("AZURE_OPENAI_KEY"),
  26. azure_endpoint=os.getenv("AZURE_ENDPOINT"),
  27. api_version=os.getenv("AZURE_API_VERSION"),
  28. )
  29. ```
  30. </Step>
  31. <Step title="Update Agent Model Parameters">
  32. Set the `model` parameter inside each agent to use your custom client and model:
  33. ```python
  34. from agency_swarm import Agent, OpenAIChatCompletionsModel
  35. # Define your agent and pass the custom client into the model
  36. azure_agent = Agent(
  37. name="AzureAgent",
  38. instructions="You are a helpful assistant",
  39. model=OpenAIChatCompletionsModel(
  40. model="gpt-5.4-mini",
  41. openai_client=azure_agent,
  42. ),
  43. )
  44. ```
  45. <Note>
  46. Model deployment name might be different from the standard OpenAI model names. It is set by you when you deploy a model to Azure.
  47. </Note>
  48. </Step>
  49. <Step title="Run Your Agency">
  50. After configuring the client and updating the agents, you can run your agency as usual:
  51. ```python
  52. import asyncio
  53. from agency_swarm import Agency
  54. agency=Agency(azure_agent)
  55. agency.tui()
  56. ```
  57. The first terminal run downloads the matching terminal app automatically.
  58. </Step>
  59. </Steps>
  60. For other model providers (Anthropic, Google, AWS Bedrock, etc.), see [Third-Party Models](/additional-features/third-party-models).