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


<!--more-->
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
  - [Prompt Engineering 入门](https://zhuanlan.zhihu.com/p/610743229)
  - [Introduction to prompt engineering](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/prompt-engineering)

## Pricing

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

Reference
  - https://azure.microsoft.com/zh-tw/pricing/details/cognitive-services/openai-service/

## Quick Start with ChatGPT

### Installation

```bash
pip install openai
```

### Create python script

```python
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](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


{{< gist ycc789741ycc 09db30e8d8f1752b96a554b2866ffde5 >}}

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](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
```python
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
```python
response = openai.ChatCompletion.create(
    engine=OPENAI_ENGINE_NAME,  # engine = "deployment_name".
    messages=messages,
    api_version="2023-03-15-preview"
)
```

