Artificial Intelligence 26 min read

Prompt Engineering: Concepts, Evolution, Techniques, and a Logistics Application Case

This article explains what Prompt Engineering is, traces its development from early command‑based interactions to modern adaptive and multimodal prompting, details various prompting techniques such as zero‑shot, few‑shot, Chain‑of‑Thought, hallucination‑reduction methods, and demonstrates their practical use in a JD Logistics SKU piece‑type classification case with code examples.

JD Tech Talk
JD Tech Talk
JD Tech Talk
Prompt Engineering: Concepts, Evolution, Techniques, and a Logistics Application Case

1. What is Prompt Engineering?

Prompt Engineering is the design and optimization of prompts or instructions used to interact with AI assistants, requiring clear and specific requests so that the model can understand the task and provide useful responses.

Key Aspects

Define the goal (e.g., write an article, answer a question, hold a conversation).

Design concise prompts that contain all necessary information.

Iteratively optimize and test prompts to improve results.

Anticipate and handle unexpected model outputs.

2. How Prompt Engineering Emerged

Early stage (pre‑2017): Simple commands and template‑based QA dominated interactions.

2017‑2018: Introduction of Seq2Seq models and pretrained language models (e.g., GPT‑1) highlighted the need for clear prompts.

2019‑2020: GPT‑2 and BERT breakthroughs sparked research on guiding models with well‑crafted prompts.

2020‑2021: GPT‑3’s large scale and few‑shot/zero‑shot capabilities made Prompt Engineering essential for systematic study.

2021‑2023: Development of Prompt Tuning, automation tools, and domain‑specific optimizations.

2024 onward: Adaptive prompt generation, multimodal prompting, and human‑AI collaborative optimization.

3. Prompt Engineering Techniques

3.1 Zero‑shot and Few‑shot Prompting

Zero‑shot provides only the task description; few‑shot adds a few examples to guide the model.

3.2 Reasoning and Logic Techniques

Methods such as Chain‑of‑Thought (CoT), Automatic CoT (Auto‑CoT), Self‑Consistency, and Logical CoT improve model reasoning by prompting step‑by‑step thinking or generating multiple reasoning paths.

3.3 Reducing Hallucinations

Approaches like Retrieval‑Augmented Generation (RAG), Chain‑of‑Verification (CoVe), and other verification‑oriented prompts combine external knowledge retrieval or self‑checking to mitigate inaccurate outputs.

3.4 Fine‑tuning and Optimization

Automatic Prompt Engineer (APE) automatically generates, scores, and selects high‑quality prompts using reinforcement learning, enabling dynamic adaptation to different tasks.

3.5 Other Techniques

Includes Chain‑of‑Code (CoC) for programming tasks, Contrastive CoT (CCoT), emotion‑aware prompting, and Rephrase‑and‑Respond (RaR) to improve clarity and tone.

4. Application Case: SKU Piece‑Type Classification in Logistics

A JD Logistics case study uses various prompting strategies (Zero‑shot, Few‑shot, CoT, Auto‑CoT, Self‑Consistency) to classify product piece‑type based on SKU data, improving accuracy from 44.44% (Zero‑shot) to 77.78% (Auto‑CoT).

Code Example: Basic GPT Call

def classify_product(row, rules_text):
    try:
        client = OpenAI(
            api_key=os.environ["OPENAI_API_KEY"],
            base_url=os.environ["OPENAI_API_BASE"]
        )
        description = f"商品编码:{row['goods_code']},描述:{row['goods_name']},重量:{row['weigth']}。"
        system_message = "你是物流行业的一位专家,请基于规则和商品描述,仅输出该商品的件型,不要输出其他任何信息。"
        user_message = (f"规则:\n{rules_text}\n"
                        f"商品描述:{description}\n")
        response = client.chat.completions.create(
            model="gpt-4-1106-preview",
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": user_message}
            ],
            temperature=0,
            max_tokens=6,
            top_p=0.1,
            n=1
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return str(e)

Prompt Variants Used in the Study

Zero‑shot Prompt: Simple rule description; accuracy 44.44%.

Few‑shot Prompt: Adds two labeled examples; accuracy 55.56%.

Chain‑of‑Thought Prompt: Breaks the decision process into steps; accuracy 66.67%.

Automatic Chain‑of‑Thought (Auto‑CoT) Prompt: Instructs the model to first determine product category, then source, then piece‑type; accuracy 77.78%.

Self‑Consistency Prompt: Generates multiple outputs and selects the most frequent result; accuracy 66.67%.

Additional Code: Self‑Consistency Implementation

import random

def classify_product_self_consistency(row, rules_text):
    try:
        client = OpenAI(
            api_key=os.environ["OPENAI_API_KEY"],
            base_url=os.environ["OPENAI_API_BASE"]
        )
        description = f"商品编码:{row['goods_code']},描述:{row['goods_name']},重量:{row['weigth']}。"
        system_message = "你是物流行业的一位专家,请基于规则和商品描述,仅输出该商品的件型,不要输出其他任何信息。"
        user_message = (f"规则:\n{rules_text}\n"
                        f"商品描述:{description}\n")
        responses = client.chat.completions.create(
            model="gpt-4-1106-preview",
            messages=[
                {"role": "system", "content": system_message},
                {"role": "user", "content": user_message}
            ],
            temperature=0.1,
            max_tokens=20,
            top_p=0.1,
            n=5
        )
        piece_type_options = []
        for response in responses.choices:
            match = re.search(r"件型:(.+)", response.message.content)
            if match:
                piece_type = match.group(1)
            else:
                piece_type = response.message.content
            if piece_type not in piece_type_options:
                piece_type_options.append(piece_type)
        if len(piece_type_options) > 1:
            return random.choice(piece_type_options)
        elif piece_type_options:
            return piece_type_options[0]
        else:
            return "无法确定件型,请检查规则或商品描述。"
    except Exception as e:
        return str(e)
prompt engineeringLarge Language ModelsChain-of-Thoughtfew-shot learningLLM applicationsAI prompting
JD Tech Talk
Written by

JD Tech Talk

Official JD Tech public account delivering best practices and technology innovation.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.