Chuyển đến nội dung chính

第 19 課:現代 LLM 和 NLP — RAG、代理和 2026 年趨勢

從傳統的NLP到LLM時代。檢索增強生成。情境學習與微調。 NLP 任務的快速工程。用於 NLP 工作流程的 AI 代理程式。多模式自然語言處理。趨勢:小語言模式、合成資料、憲法人工智慧。

🧠 人工智慧與機器學習 — 第 18 課 第 19 課:現代 LLM 與 NLP — RAG、代理、 和 2026 年趨勢

NLP 從基礎到進階:掌握自然語言處理

第 6 部分:NLP 產生與現代趨勢

亞洲開發網

簡介

2026 年的 NLP 與 5 年前完全不同。法學碩士改變了幾乎所有 NLP 問題的處理方式。本文總結了最現代的趨勢和技術。


1. 傳統NLP vs LLM時代

傳統法學碩士時代
每個任務都需要自己的模型法學碩士可以解決許多任務
需要標記資料零/少射擊,快速工程
訓練→評估→部署提示→測試→RAG/微調→部署
BERT + 特定任務頭GPT-4/雙子座+提示
幾週打造原型製作時間

什麼時候仍然使用傳統 NLP?

  • 延遲關鍵:BERT 推理 ~5ms vs LLM ~500ms
  • 成本敏感:微調的小模型 << LLM API
  • Offline: On-device, no internet
  • 特定領域:當需要非常高的精確度時(醫療、法律)

2. Retrieval-Augmented Generation (RAG)

┌────────────────────────────────────────────────────────┐
│                    RAG PIPELINE                         │
│                                                        │
│  User Query                                            │
│      │                                                 │
│      ▼                                                 │
│  ┌──────────┐    ┌───────────────┐                    │
│  │  Embed   │───▶│ Vector Search │── Top-K docs       │
│  │  Query   │    │ (FAISS/PGVector)│                   │
│  └──────────┘    └───────────────┘                    │
│                         │                              │
│                         ▼                              │
│  ┌──────────────────────────────────────────┐         │
│  │  LLM (GPT-4 / Gemini)                    │         │
│  │  System: "Answer based on context below"  │         │
│  │  Context: [retrieved documents]            │         │
│  │  Question: [user query]                    │         │
│  └──────────────────────────────────────────┘         │
│                         │                              │
│                         ▼                              │
│                     Answer                             │
└────────────────────────────────────────────────────────┘

RAG cho NLP Tasks

前(火車模型)之後(RAG)
Fine-tune BERT cho QARAG + LLM: retrieve docs → generate answer
在標記資料上訓練分類器少量範例 + LLM
建構 NER 管道LLM 擷取實體並提示

3. Prompt Engineering cho NLP Tasks

from openai import OpenAI

client = OpenAI()

# NER bằng prompt (không cần train!)
def extract_entities(text):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{
            "role": "system",
            "content": """Extract named entities from Vietnamese text.
Return JSON: {"persons": [], "organizations": [], "locations": [], "dates": []}"""
        }, {
            "role": "user",
            "content": text
        }],
        response_format={"type": "json_object"},
    )
    return response.choices[0].message.content

# Classification bằng prompt
def classify_text(text, categories):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{
            "role": "system",
            "content": f"Classify text into one of: {categories}. Return only the category name."
        }, {
            "role": "user",
            "content": text
        }],
    )
    return response.choices[0].message.content

4. AI Agents cho NLP Workflows

# Agent tự động phân tích document
# 1. Extract entities → 2. Classify sentiment → 3. Summarize → 4. Store results

from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain.tools import tool

@tool
def extract_entities_tool(text: str) -> 字典:
    """從文字中擷取命名實體。"""
    ner = pipeline("ner", grouped_entities=True)
    返回ner(文本)

@工具
def Classify_sentiment_tool(text: str) -> str:
    """將文本情緒分類。"""
    分類器 = pipeline("情緒分析")
    返回分類器(文字)[0]

# Agent結合了許多NLP工具
# → 決定使用哪些工具以及按什麼順序

5. 2026 年 NLP 趨勢

5.1 Small Language Models (SLMs)

  • Phi-3, Gemma 2, LLaMA 3.2 (1B-7B params)
  • 可在筆記型電腦、行動裝置上執行
  • 在消費級 GPU 上輕鬆微調
  • 足以勝任許多 NLP 任務

5.2 Multimodal NLP

  • GPT-4o, Gemini: text + image + audio + video
  • NLP 不再只是文字 - 多模態理解
  • Document AI: OCR + NLP cho invoice, form, report

5.3 Synthetic Data

  • 使用大型LLM產生小型模型的訓練數據
  • 標籤成本降低 10-100 倍
  • Quality control: LLM-as-judge

5.4 Structured Generation

# 確保 LLM 輸出始終採用正確的格式
從 pydantic 匯入 BaseModel

NEROutput 類別(基礎模型):
    人員:列表[str]
    組織:列表[str]
    位置:列表[str]

# 使用講師、大綱或 JSON 模式

6. 決策架構:選擇哪一種方法?

您需要解決 NLP 任務嗎?
    │
    ├── 快速原型? ──→ LLM API + 即時工程
    │
    ├── 對成本敏感? ──→ 微調小模型(BERT/PhoBERT)
    │
    ├── 需要知識庫嗎? ──→ RAG管道
    │
    ├── 延遲<50ms? ──→ 蒸餾/量化模型
    │
    └── 工作流程複雜? ──→ AI Agent + NLP 工具

總結

趨勢意義
法學碩士優先使用 LLM 製作原型,然後進行最佳化
抹布結合檢索+生成
SLM體積小但功能強大,運作優勢
多式聯運文字+影像+音訊
代理商自動化 NLP 工作流程

下一篇文章

第 20 課:Capstone 專案 — 建構端對端 NLP 平台:針對真實領域的分類 + NER + QA。