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

Contents
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
andgpt-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
|
|
Create python script
|
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
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
|
|
- Q: Can I specify the api information in the create method?
- A: Yes
|
|