Skip to content

Sample application to demonstrate Azure OpenAI in Python

License

Notifications You must be signed in to change notification settings

guygregory/ChatToolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat Toolkit - Azure OpenAI sample GPT-4/GPT-3.5 Python application

The Azure OpenAI Service provides businesses and developers with high-performance AI models at production scale with industry-leading uptime. This is the same production service that Microsoft uses to power its own products, including GitHub Copilot, an AI pair programmer that helps developers write better code, Power BI, which leverages OpenAI-powered natural language to automatically generate formulae and expressions, and the recently-announced Microsoft Designer, which helps creators build stunning content with natural language prompts.

As a Partner Technololgy Strategist (PTS) at Microsoft, I wanted to create a simple app to demonstrate the simplicity of the new Azure OpenAI service to our partners and customers. I'm not an experienced developer, so I began with the sample code from Chat Playground in Azure OpenAI Studio and used both GitHub Copilot and the preview of GitHub Copilot Chat (part of GitHub Copilot X) to co-create the rest.

Here's the sample code from Chat Playground in Azure OpenAI Studio

Here's an example of how I used GitHub Copilot Chat to provide some simple error handling (using the VS Code extension).

Based on the comment and function declaration, GitHub Copilot wrote this whole template export function, which ran first-time.

The API Options allow the model parameters to be adjusted, and reflects the controls in Azure OpenAI Studio.

If zero-shot (system prompt-only) doesn't suffice, few-shot examples can be used to guide the model further

How to use - Prerequisites

  • Before deploying Azure OpenAI, please gain approval for your Azure subscription. Here's the form (currently business customers/managed partners/MSFT internal only)
  • GPT-4 (Optional) At time of writing, GPT-4 models are currently in preview. Once you've had Azure OpenAI approved for your subscription, you can apply for GPT-4 by filling out this form
  • GPT-4 (Optional) At time of writing, GPT-4 models are only available in East US and South Central US Azure regions. Check this page for new regions added in future.
  • Once you've had approval, you'll need to deploy the Azure OpenAI service into your subscription. Instructions are on the Microsoft Docs page.
  • Next, you'll need to deploy at least one model, either the standard GPT-3.5 Turbo model, or GPT-4/GPT-4-32k if you have access.

How to use - Downloading and running the application

  • The app requires that you have Python installed, which can be downloaded here (python.org) or here (Microsoft Store)
  • The app requires the OpenAI Python library (pip install openai)
  • Download/clone the script onto your local computer
  • Add the OPENAI_API_KEY and OPENAI_API_BASE environmental variables on your PC using the details from your own Azure OpenAI instance. Use the guide here to understand how to set environmental variables.
    For example, on Windows:
    setx OPENAI_API_BASE "https://.openai.azure.com/"
    setx OPENAI_API_KEY "1234567890abcdef1234567890abcdef"
  • Edit the model name, unless you named it 'gpt-4'
  • When running the .py script, a Tkinter form should appear as per the above screenshot

Please note

  • When building applications using Azure OpenAI, developers must adhere to the code of conduct and responsible AI principles.
  • This is not an official Microsoft code sample, and is intended as a conversation starter, not as a production-ready application.

Feedback