Contents

Quick Start with building multilingual classifier by power of OpenAI in 5 minutes

image: https://blogs.microsoft.com/blog/2023/01/23/microsoftandopenaiextendpartnership/

What is Openai ChatCompletion

  • OpenAI Chat API, you can build your own applications with gpt-3.5-turbo and gpt-4

Prompt Engineering Overview

  • Completion
    • Given the unfinished text, complete the text by the GPT model
  • Zero shot
    • Ask the model to generate the text without any prompt example
  • One shot
    • Give one sample before giving the assignment
  • Few shot
    • Give a few samples before giving the assignment
  • Chain of thought
    • Require model to generate the process with inference steps
  • Role prompting / Instruction Tuning
    • Bring in the role to awake model’s functionality

Reference

Pricing

  • One token is approximately 0.75 word
  • 0.002 USD per 1000 tokens

Reference

Quick Start with ChatGPT

Installation

1
pip install openai

Create python script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import os
import openai
openai.api_type = "azure"
openai.api_version = "2023-03-15-preview" 
openai.api_base = os.getenv("OPENAI_API_BASE")  # Your Azure OpenAI resource's endpoint value.
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
    engine="gpt-35-turbo", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
    messages=[
        {"role": "system", "content": "Assistant is a large language model trained by OpenAI."},
        {"role": "user", "content": "What's the difference between garbanzo beans and chickpeas?"},
    ]
)

print(response)

print(response['choices'][0]['message']['content'])

Reference: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#working-with-the-chatgpt-and-gpt-4-models-preview

Making the classifier with few-shot learning and instruction tuning

In this example, I will use the gpt-35-turbo model to build a sentiment classifier to classify the text into positive, neutral or negative to inference the person is going to work or not.

I use the following concept:

  • Few-shot learning

  • Instruction tuning

Reference: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#few-shot-learning-with-chat-completion

FAQ

  • Q: What’s the difference between openai and azure-openai
  • A: Azure-openai provide the service monitoring for model usage but they both use openai client library

  • Q: How to set the request timeout in openai client library?
  • A: You can set it in the create function
1
2
3
4
5
response = openai.ChatCompletion.create(
    engine=OPENAI_ENGINE_NAME,  # engine = "deployment_name".
    messages=messages,
    request_timeout=3.,
)

  • Q: Can I specify the api information in the create method?
  • A: Yes
1
2
3
4
5
response = openai.ChatCompletion.create(
    engine=OPENAI_ENGINE_NAME,  # engine = "deployment_name".
    messages=messages,
    api_version="2023-03-15-preview"
)