Food Order Understanding in Korean

This is a LoRA adapter as a result of fine-tuning the pre-trained model 'EleutherAI/polyglot-ko-12.8b'. It is designed with the expectation of understanding Korean food ordering sentences, and analyzing food menus, option names, and quantities.

Usage

Here is an example of loading the model. Note the pretrained model is EleutherAI/polyglot-ko-12.8b.

peft_model_id = "jangmin/qlora-polyglot-ko-12.8b-food-order-understanding-32K"

config = PeftConfig.from_pretrained(peft_model_id)

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, quantization_config=bnb_config, cache_dir=cache_dir, device_map={"":0})
model = PeftModel.from_pretrained(model, peft_model_id)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path, cache_dir=cache_dir)

model.eval()

Inferece can be done as follows.

instruction_prompt_template = """
다음은 매장에서 고객이 음식을 주문하는 주문 문장이다. 이를 분석하여 음식명, 옵션, 수량을 추출하여 고객의 의도를 이해하고자 한다.
분석 결과를 완성해주기 바란다.

### 주문 문장: {0} ### 분석 결과: 
"""
def gen(x):
    q = instruction_prompt_template.format(x)
    gened = model.generate(
        **tokenizer(
            q, 
            return_tensors='pt', 
            return_token_type_ids=False
        ).to('cuda'), 
        max_new_tokens=256,
        early_stopping=True,
        do_sample=True,
        eos_token_id=tokenizer.eos_token_id
    )
    decoded_results = tokenizer.batch_decode(gened, skip_special_tokens=True)
    return decoded_results[0]

A generated sample is as follows.

print(gen("오늘은 비가오니깐 이거 먹자. 삼선짬뽕 곱배기하구요, 사천 탕수육 중짜 한그릇 주세요."))
다음은 매장에서 고객이 음식을 주문하는 주문 문장이다. 이를 분석하여 음식명, 옵션, 수량을 추출하여 고객의 의도를 이해하고자 한다.
분석 결과를 완성해주기 바란다.

### 주문 문장: 오늘은 비가오니깐 이거 먹자. 삼선짬뽕 곱배기하구요, 사천 탕수육 중짜 한그릇 주세요. ### 분석 결과: 
분석 결과 0: 음식명:삼선짬뽕,옵션:곱배기
분석 결과 1: 음식명:사천 탕수육,옵션:중짜,수량:한그릇

Training

Fine-tuning was performed using https://github.com/artidoro/qlora. Here is my training script.

python qlora.py \
    --cache_dir /Jupyter/huggingface/.cache \
    --model_name_or_path EleutherAI/polyglot-ko-12.8b \
    --output_dir ../output/polyglot-ko-food-order-understanding-12.8b \
    --logging_steps 10 \
    --save_strategy steps \
    --data_seed 42 \
    --save_steps 200 \
    --save_total_limit 40 \
    --evaluation_strategy steps \
    --eval_dataset_size 1024 \
    --max_eval_samples 1000 \
    --per_device_eval_batch_size 8 \
    --max_new_tokens 32 \
    --dataloader_num_workers 3 \
    --group_by_length \
    --logging_strategy steps \
    --remove_unused_columns False \
    --do_train \
    --do_eval \
    --lora_r 64 \
    --lora_alpha 16 \
    --lora_modules all \
    --double_quant \
    --quant_type nf4 \
    --bf16 \
    --bits 4 \
    --warmup_ratio 0.03 \
    --lr_scheduler_type constant \
    --gradient_checkpointing \
    --dataset /Jupyter/dev_src/ASR-for-noisy-edge-devices/data/food-order-understanding-32k.json \
    --target_max_len 512 \
    --per_device_train_batch_size 16 \
    --gradient_accumulation_steps 1 \
    --max_steps 1000 \
    --eval_steps 200 \
    --learning_rate 0.0002 \
    --adam_beta2 0.999 \
    --max_grad_norm 0.3 \
    --lora_dropout 0.05 \
    --weight_decay 0.0 \
    --seed 0 \
    --report_to tensorboard

Dataset

The dataset was constructed using GPT-API with gpt-3.5-turbo-16k. A prompt template is desginged to generate examples of sentence pairs of a food order and its understanding. Total 32k examples were generated. Some generated examples are as follows:

{
  'input': '\n다음은 매장에서 고객이 음식을 주문하는 주문 문장이다. 이를 분석하여 음식명, 옵션, 수량을 추출하여 고객의 의도를 이해하고자 한다.\n분석 결과를 완성해주기 바란다.\n\n### 주문 문장: 하이, 디트로이트 스타일 피자 XL 두판 주세요. ### 분석 결과: \n', 
  'output': '분석 결과 0: 음식명:디트로이트 스타일 피자,옵션:XL,수량:두판'
},
{
  'input': '\n다음은 매장에서 고객이 음식을 주문하는 주문 문장이다. 이를 분석하여 음식명, 옵션, 수량을 추출하여 고객의 의도를 이해하고자 한다.\n분석 결과를 완성해주기 바란다.\n\n### 주문 문장: 저는 후라이드치킨 반마리와 팟타이 한그릇을 주문하고 싶습니다. ### 분석 결과: \n', 
  'output': '분석 결과 0: 음식명:후라이드치킨,수량:반마리\n분석 결과 1: 음식명:팟타이,수량:한그릇'
}

Limitations

Currently, using GPT-4 yields significantly better generated examples. Therefore, it is reasonable to anticipate improved fine-tuning once GPT-4 API usage becomes available.

The prompts for dataset creation are under ongoing enhancement.