FDEAI 工程實戰課AI Engineering for FDEs
入門篇 · 第 1 章Basics · Chapter 1

AI 語言模型在做什麼What a language model does

ChatGPT、Claude、Gemini 這些 AI,背後都是「大型語言模型」。聽起來很玄,但它做的事情可以用一句話說完。這一章用一個你可以親手操作的迷你模型,帶你看懂它。

ChatGPT, Claude and Gemini are all built on "large language models". It sounds mysterious, but what they do fits in one sentence. This chapter uses a tiny model you can play with to show you how.

⏱ 15 分鐘min🧪 labs/beginner/next_word.py

讀完這章你會知道After this chapter you'll know

  • 語言模型的核心動作:猜下一個字。
  • 為什麼 AI 有時候會說出很通順、但完全錯誤的話。
  • token 和 context window 是什麼,為什麼你常常會看到這兩個詞。
  • The core move of a language model: guessing the next word.
  • Why AI sometimes says things that sound fluent but are completely wrong.
  • What tokens and the context window are, and why you keep hearing about them.

一句話說完In one sentence

一句話重點Key point

語言模型是一個「很會接話」的程式:看前面的文字,猜下一個字最可能是什麼,接上去,再猜下一個。

A language model is a program that is very good at continuing text: it looks at the words so far, guesses the most likely next one, adds it, and guesses again.

你的手機輸入法就有類似的功能:打了「今天」,它會建議「天氣」「晚上」「要不要」。大型語言模型做的是同一件事,只是它讀過的文字多到難以想像,所以猜得非常準,準到看起來像在「思考」和「對話」。

Your phone keyboard does something similar: type "see you" and it suggests "tomorrow", "soon", "later". A large language model does the same thing, except it has read an unimaginable amount of text, so its guesses are good enough to look like thinking and conversation.

動手試:一個只讀過六句話的模型Try it: a model that has read six sentences

下面這個模型非常陽春:它只讀過六句話,而且只會看「最後一個字」來猜下一個字。但它的運作方式,和真正的語言模型是同一個概念。

The model below is extremely basic: it has read only six sentences, and it looks at just the last character to guess the next one. But the idea is the same as in a real language model. (The training text is Chinese; the mechanism doesn't depend on the language.)

demo

猜下一個字Guess the next character

模型讀過的六句話(訓練資料)The six sentences it has read (training data)

    下一個字可能是(點一下接上去):

    The next character could be (click to add it):

    多按幾次「讓它自己寫完」,你會發現兩件事:

    Press "Let it finish" a few times and you'll notice two things:

    1. 每次結果可能不一樣。模型是「依機率抽」下一個字,不是每次都選第一名。真正的 AI 也是這樣,所以同一個問題問兩次,答案可能不同。
    2. 它會寫出沒讀過的句子,例如「今天天氣很熱,我們在家喝咖啡。」這句話很通順,但訓練資料裡從來沒有。它不是在「查資料」,而是在「拼出聽起來合理的句子」。
    1. The result can differ every time. The model samples the next character by probability instead of always taking the top one. Real AI works the same way, which is why the same question can get different answers.
    2. It writes sentences it has never read, such as "It's hot today, let's have coffee at home." Fluent, but never in the training data. It isn't looking anything up; it is assembling text that sounds plausible.

    為什麼 AI 會一本正經地說錯話Why AI says wrong things so confidently

    第二點很重要。語言模型學到的是「什麼樣的文字聽起來通順、合理」,而不是「什麼是真的」。大多數時候兩者一致,所以它看起來很博學;但當它不知道答案時,它不會自動說「我不知道」,而是照樣接出一段聽起來很對的話。這種現象叫做「幻覺」(hallucination)。

    That second point matters. A language model learns what text sounds fluent and plausible, not what is true. Most of the time those coincide, so it seems knowledgeable; but when it doesn't know, it doesn't automatically say "I don't know". It continues with something that sounds right. This is called hallucination.

    記住Remember

    AI 說得很有自信,不代表它是對的。重要的事(數字、日期、法規、程式碼能不能跑)一定要查證。後面幾章會教你怎麼用程式自動幫你檢查。

    Sounding confident doesn't make the AI right. Check anything important (numbers, dates, regulations, whether code runs). Later chapters show how to have code check for you automatically.

    它知道什麼、不知道什麼What it knows and doesn't

    通常知道Usually knows

    公開的常識、程式語言、常見的寫作方式、訓練資料截止日期之前的公開資訊。

    Public general knowledge, programming languages, common writing styles, public information up to its training cutoff.

    不知道(除非你告訴它)Doesn't know (unless you tell it)

    你們公司的內部規定、你的客戶資料、今天的日期、最新的新聞、你上一次跟它說過什麼。

    Your company's internal rules, your customer data, today's date, the latest news, what you told it last time.

    右邊那一欄,就是工程師要用程式補上的部分:第 2 章教你怎麼把對話送給它,第 4 章教你怎麼把公司文件給它看。

    The right-hand column is what engineers fill in with code: chapter B2 covers sending it the conversation, and B4 covers giving it your company's documents.

    兩個一定會遇到的詞:token 與 context windowTwo words you'll meet everywhere: tokens and the context window

    Token

    我們的迷你模型一次猜「一個字」。真正的模型一次猜的是一個 token:可能是一個字、半個英文單字,或一個標點。你可以把 token 想成模型讀寫文字的「最小單位」。它重要的原因很實際:AI 服務是依照 token 數量收費的,長度限制也是用 token 計算。一段文字大約是多少 token,依模型與語言而不同。

    Our mini model guesses one character at a time. A real model guesses one token at a time: a word, part of a word, or a punctuation mark. Think of tokens as the model's smallest unit of reading and writing. They matter for a practical reason: AI services charge by the token, and length limits are counted in tokens too. How many tokens a piece of text takes depends on the model and the language.

    Context window

    模型每次回答時,能「看到」的文字有上限,這個上限叫 context window(上下文視窗)。可以把它想成模型的「工作記憶」:你的問題、之前的對話、你貼給它的文件,全部加起來都要放得進去。現在的模型可以一次看非常多文字,但放得越多,每次的費用越高、速度也越慢。

    Each time a model answers, there's a limit on how much text it can "see": the context window. Think of it as working memory: your question, the earlier conversation and any documents you pasted all have to fit. Modern models can take in a great deal of text, but the more you include, the more each request costs and the slower it gets.

    看看程式碼(選讀)Look at the code (optional)

    迷你模型的 Python 版本只有幾十行。核心就是「數一數每個字後面接過哪些字」:

    The Python version of the mini model is a few dozen lines. The core is counting which characters followed each character:

    labs/beginner/next_word.py
    def train(sentences):
        """數一數:每個字後面,出現過哪些字、各幾次。"""
        counts = defaultdict(Counter)
        for sentence in sentences:
            for current_char, next_char in zip(sentence, sentence[1:]):
                counts[current_char][next_char] += 1
        return counts
    
    
    def next_char_probabilities(counts, text):
        """看最後一個字,算出下一個字的機率。"""
        last_char = text[-1]
        followers = counts.get(last_char)
        if not followers:
            return {}
        total = sum(followers.values())
        return {char: n / total for char, n in followers.most_common()}
    terminal
    python -m labs.beginner.next_word

    真正的模型和它差在哪?它不只看最後一個字,而是看前面一大段文字;它不是數次數,而是用神經網路學出非常複雜的規律。但「猜下一個、接上去、再猜」這個流程是一樣的。

    How does a real model differ? It looks at a long stretch of preceding text, not just the last character, and instead of counting it learns very complex patterns with a neural network. But the loop of "guess the next one, append it, guess again" is the same.

    小測驗Quiz

    1. 語言模型產生回答的方式,最接近下面哪一個?1. Which is closest to how a language model produces an answer?
    模型本身不會去查資料庫或上網,它是根據學到的規律,一個 token 一個 token 地產生文字。
    The model itself doesn't query a database or browse the web; it generates text token by token from the patterns it learned.
    2. AI 很有自信地回答了一個你們公司內部的規定。你應該?2. The AI confidently answers a question about your company's internal policy. You should:
    內部規定不在它的訓練資料裡。講得具體不代表正確,兩次答案一樣也可能兩次都是編的。
    Internal policies aren't in its training data. Specific doesn't mean correct, and two matching answers can both be invented.
    3. 為什麼工程師要在意 token 數量?3. Why do engineers care about token counts?
    送出與收到的 token 都會計費,而且每次能放進 context window 的量有上限。
    Tokens sent and received are both billed, and there's a cap on how many fit in the context window.