Try Azure OpenAI with ChatGPT and Python

Jun Kudo
3 min readMar 11, 2023

--

Introduction

Now that Azure OpenAI’s ChatGPT is available, I would like to give it a try.

ChatGPT is now available in Azure OpenAI Service
https://azure.microsoft.com/en-us/blog/chatgpt-is-now-available-in-azure-openai-service/

I play with quick start but it doesn’t work as it is, so I supplemented it.

Try ChatGPT Playground

This is quite simple.
I deploy ChatGPT and use it as is.

Try it in python

This is done in a WSL environment.

pip install openai

If you get any errors, please update pip.

pip install --upgrade pip

Define environment variables. Check them from the portal.

echo export OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment

echo export OPENAI_API_BASE="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment

where REPLACE_WITH_YOUR_ENDPOINT_HERE is the form including https.

The Python code is as follows

The engine is the name of the deployment at the time of creation, not the model name. It is not the model name.

#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
engine="ChatGPT",
#engine="gpt-35-turbo",
prompt="<|im_start|>user\nHello\n<|im_end|>\n<|im_start|>assistant",
temperature=1,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=["<|im_end|>"])

print(response['choices'][0]['text'])

You are typing “Hello.”

I think this will get you an answer.

If you run it several times, you will get different answers.

Note that the format is slightly different from the original OpenAI.

Allow it to be entered as a variable.

#Note: The openai-python library support for Azure OpenAI is in preview.
import sys
import os
import openai

args = sys.argv

openai.api_type = "azure"
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")
print("-----You-----\n"+args[1])
response = openai.Completion.create(
engine="ChatGPT",
#engine="gpt-35-turbo",
prompt="<|im_start|>user\n" + args[1] + "\n<|im_end|>\n<|im_start|>assistant",
temperature=1,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=["<|im_end|>"])

print("-----ChatGPT-----"+response['choices'][0]['text'])

It seems like a conversation in progress.

Conclusion

I played with ChatGPT in Azure OpenAI. This is interesting and can be applied in many ways.

Originally published at https://level69.net on March 11, 2023.

--

--

Jun Kudo
Jun Kudo

Written by Jun Kudo

I’m Japanese, Microsoft MVP for Azure. Alibaba Cloud MVP. . https://twitter.com/jkudo

No responses yet