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

Contents

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

  • OpenAI Chat API, you can build your own applications with gpt-3.5-turbo and gpt-4
  • 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

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

Reference

1
pip install openai
 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

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

#Note: The openai-python library support for Azure OpenAI is in preview.
from typing import Dict, List, Text
from enum import Enum
from dataclasses import dataclass
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")
ENGINE_NAME = "gpt-35-turbo"
# Instruction tuning to refine the model behaivor
INSTRUCTIONS = [
"Classify text as neutral, negative, positive.",
"Only return neutral, negative or positive."
]
# Give the model some examples
FEW_SHOT_EXAMPLES = [
{"user": "I'm on the way to work", "assistant": "positive"},
{"user": "I don't want to work", "assistant": "negative"},
]
@dataclass
class FewShotExample:
user: Text
assistant: Text
def to_openai_message(self) -> List[Dict]:
return [
{
"role": "user",
"content": self.user,
},
{
"role": "assistant",
"content": self.assistant,
}
]
class Label(str, Enum):
POSITIVE = "positive"
NEGATIVE = "negative"
NEUTRAL = "neutral"
def multilingual_classifier(intput_text: Text) -> Label:
"""Multilingual classifier."""
label = Label.NEUTRAL
try:
few_shot_examples = [FewShotExample(**example) for example in FEW_SHOT_EXAMPLES]
messages = _get_chat_completion_messages(intput_text, INSTRUCTIONS, few_shot_examples)
response = openai.ChatCompletion.create(
engine="gpt-35-turbo", # engine = "deployment_name".
messages=messages
)
label = Label(response['choices'][0]['message']['content'])
except Exception as e:
print(e)
return label
def _get_chat_completion_messages(
query_text: Text,
instructions: List[Text],
few_shot_examples: List[FewShotExample]
) -> List[Dict]:
"""Get messages.
Official prompt engineering:
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
"""
messages = []
system_content = """Assistant is a multilingual classifier and just give me the output label without any reason.
Instructions:
"""
for instruction in instructions:
system_content += f"- {instruction}\n"
messages.append({"role": "system", "content": system_content})
for few_shot_example in few_shot_examples:
messages.extend(few_shot_example.to_openai_message())
messages.append({"role": "user", "content": query_text})
return messages
if __name__ == "__main__":
label = multilingual_classifier("今天有動力上班")
# Positive
print(label.value)
label = multilingual_classifier("I prefer stay in home.")
# Negative
print(label.value)

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

  • 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"
)