Calling Azure OpenAI Programmatically: A Step-by-Step Guide -Part 1

alpa buddhabhatti
3 min readFeb 20, 2024

--

How to Call Azure OpenAI Programmatically

Whether you’re an experienced developer or embarking on your coding journey, this blog will guide you through the process of setting up and utilizing Azure OpenAI programmatically. While we’ll be using Python, the concepts can be applied to other languages such as C# or Java.

Prerequisites:

  1. An Azure Portal Account with access to Azure OpenAI services.
  2. Visual Studio Code or Visual Studio installed on your system.

Steps to Create an Azure OpenAI Instance:

  1. Sign in to the Azure Portal: Log in to your Azure account using the Azure Portal.
  2. Create an Azure OpenAI Resource: In the Azure Portal, click on Create a resource.
  • Search for “OpenAI” and select the Azure OpenAI Service.
  • Configure the settings, such as region, pricing tier, and resource group.
  • Click Create to provision your Azure OpenAI instance.

3. Deploy a Model:

  • Once your Azure OpenAI instance is created, navigate to it.
  • Under the Models section, click Deploy Model.
  • Choose a model (e.g., gpt-35-turbo-16k) and provide a suitable name for your model.
  • Follow the prompts to deploy the model.

4. Access Your Model Programmatically:

  • Retrieve the necessary credentials (API keys or tokens) from your Azure OpenAI instance.
  • Use these credentials in your code (whether in Visual Studio Code or Visual Studio) to make API calls to your deployed model.

You need following information which you can find From Azure Portal.

  1. Azure OpenAI end points
  2. Key
  3. Body message

For that you need to Click on “View code” in Playground as below

You will see sample code as below. Change Drop-down for Python message type. You will notice, Message body, Endpoints and key as highlighted below. This is a sample code which calling in main.py file in below.

Calling Azure OpenAI in Visual Studio Code

Machine Setup:

  1. Install the Required Tools:
  • Make sure you have either Visual Studio or Visual Studio Code installed on your machine.
  • Additionally, ensure that you have Python installed.

2. Install the OpenAI Python Package:

  • Open the terminal in Visual Studio Code.
  • Execute the following command to install the OpenAI package (version 0.28.1):

pip install openai==0.28.1

Create Your Python File (main.py):

  1. Create a new file named main.py in your Visual Studio Code project
  2. Copy the Python code snippet from above step 4 (sample code) into your main.py file.
  3. Set Up Your API Key:
  • Retrieve the API key from your Azure OpenAI instance.
  • Replace the placeholder in your Python code with the actual API key.

Testing:

Run main.py file. you will be below result.

Note that based on what you have installed for openAI above syntax slightly different . Above syntax for openai==0.28.1 (pip install openai==0.28.1). if openai version >= 1, in that case above syntax will not work.

--

--

alpa buddhabhatti

Microsoft Data MVP | Lead Data Engineer| MCT| DP-201 | DP-202|AI-100| AZ-204 | MB2-716 | AZ-900 | Masters in Computer Science |International Speaker