計算語言學與語言學理論

week 10 謝舒凱

接下來規劃

大型語言模型:簡介、發展與應用

今天主題

  • Prompt Engineering
  • Tokenization

Transformer

補課時再回來談細節

大型語言模型

Large Language Models: quick guide

Transformer (2017) 架構的核心特點是其注意力機制(Attention Mechanism),它允許模型在處理文本時,對不同的單詞給予不同程度的關注,從而更有效地捕捉語言中的長距離依賴關係。

  • (encoder) BERT
  • (decoder) chatGPT (2022.11) >

NLP paradigm shift

chatGPT 之後

各個模態的轉換

文本(自然語言)回到核心

Text2Robot

LLM-based AI/NLP tasks

  • Prompting
  • Semantic Search
  • Optimizing LLMs with fine-tuning
  • Deploying LLMs in production

課堂練習

  • 註冊 openAI API 帳號 (建立金鑰)
    https://platform.openai.com

  • 可先利用 playground 熟悉 API

  • 看看類型,與不同的選項。(加註 Legacy表示是歷史產物)

chat 模式的三種角色

  • system (對於 LLM 的人設)
  • user (使用者這端)
  • assistant (LLM 這端)

Settings

  • top P controls how many of those tokens are considered for the next token prediction (i.e. completion). E.g., if top P is set to 0.5, the model will consider the top 50% of tokens with the highest probabilities for the next token prediction.

  • temperature controls the randomness of the model's predictions. A higher temperature will make the model's predictions more random, while a lower temperature will make the model's predictions more deterministic.

LLM eco-system

  • openAI GPTs

  • 聯發科 Davinci

  • 一鍵式自建LLM 生態圈 lobeHub
    🤯 Lobe Chat - an open-source, modern-design LLMs/AI chat framework. Supports Multi AI Providers, Multi-Modals and plugin system.

LLM prompt engineering

using generative models through prompt engineering and output verification.

有可能不是它不懂,是你還不會問。

Prompting

  • prompt engineering is the process of designing prompts to guide the LLM to generate the desired output.

  • prompts 本身是一個語言物件,可以是問題、敘述、指引。目的是引出要的回應。

  • Linguistically, it involves the elicitation techniques. 引語技術與語用策略

"Semantic and Pragmatic Prompting"

  • Pragmatic prompting 一開始常被忽略的LLM應用知識點

    • 情勒 (using emotional stimuli, e.g., “This is very important for my career.”)

    • 委婉

    • 其他請求、獲取協助的語用策略?

  • Semantic prompting

    • 用理性邏輯的方式引導

Prompting 的功能

  • 不只在於產生 effective prompt, 也用在檢驗模型的輸出、設計保障措施、安全性減緩方法。這是一個迭代的過程,需要創意實驗。

The Basic Ingredients of a Prompt

  • simple prompt

  • basic instruction prompt

Instruction-based Prompting

畢竟不只有聊天,還有其他應用(解決特定問題的)場景

prompting is often used to have the LLM answer a specific question or resolve a certain task. (instruction-based prompting).

  • Each of these tasks requires different formats of prompting and more specifically, different questions to be asked of the LLM.

通用言談策略

  • Specificity
    精準描述問題,不要含糊不清

  • Hallucination
    制止產生幻覺,不知道就說不知道

  • Order
    (指示)重點放前後,太長的訊息在中間常被忽略

Advanced Prompt Engineering

(參考 Prompt Engineering Guide)
Anthopic’s meta prompt / prompt library / prompt cookbook !!!!

The Potential Complexity of a Prompt

Some common components are:

  • Persona; Instructions; Context; Audience; Tone; Examples; data; Constraints; Output Format; Safety and Ethical Considerations; ......

其實我們可以從語言與溝通的角度,重新思考與分類這些元素

prompting linguistics

綜合例子

Modular and Iterational

  • Iterating over modular components is a vital part of prompt engineering.
  • 這是一個迭代的過程,需要不斷的嘗試與調整。In other words, experimentation is vital when finding the best prompt for your use case.


# Prompt components
persona = "You are an expert in Large Language models. You excel at breaking down complex papers into digestible summaries.\n"
instruction = "Summarize the key findings of the paper provided.\n"
context = "Your summary should extract the most crucial points that can help researchers quickly understand the most vital information of the paper.\n"
data_format = "Create a bullet-point summary that outlines the method. Follow this up with a concise paragraph that encapsulates the main results.\n"
audience = "The summary is designed for busy researchers that quickly need to grasp the newest trends in Large Language Models.\n"
tone = "The tone should be professional and clear.\n"
data = "Text to summarize: PUT_THE_DATA_TO_SUMMARIZE_HERE"
# The full prompt - remove and add pieces to view its impact on the generated output
query = persona + instruction + context + data_format + audience + tone + data
prompt = format_prompt(query)

創造力讓 PE 變成一份職業
Part of the fun in prompt engineering is that you can be as creative as possible to figure out which combination of prompt components contribute to your use case.

In-Context Learning: Providing Examples

Instead of describing the task, why do we not just show the task?

This is often referred to as in-context learning, where we provide the model with correct examples.

幾個例子,幾個 shot

課堂練習

》 Generate a sentence with a made-up word.

# Use a single example of using the made-up word in a sentence
one_shot_prompt = format_prompt(messages=[
    {"role": "user", "content": "Q: A 'Gigamuru' is a type of Japanese musical instrument. An example of a sentence that uses the word Gigamuru is:"},
    {"role": "assistant", "content": "A: I have a Gigamuru that my uncle gave me as a gift. I love to play it at home."},
    {"role": "user", "content": "Q: To 'screeg' something is to swing a sword at it. An example of a sentence that uses the word screeg is:"}
])
print(one_shot_prompt)

Chain Prompting

Breaking up the Problem

  • Instead of breaking the problem within a prompt, we can do so between prompts.

  • Essentially, we take the output of one prompt and use it as input for the next. Thereby creating a continuous chain of interactions that solves our problem.

E.g, use an LLM to create a product name, slogan, and sales pitch for us based on a number of product features.


Using a description of a product’s features, chain prompts to create a suitable name, slogan, and sales pitch.

應用場景

  • Response validation
    Asking the LLM to double-check previously generated outputs

  • Parallel prompts
    Create multiple prompts in parallel and do a final pass to merge them. For example, ask multiple LLMs to generate multiple recipes in parallel and use the combined result to create a shopping list.

  • Writing stories/books

Reasoning with Generative Models

prompt chaining, proved to be the first step toward enabling complex reasoning with LLMs.

快思慢想

系統1(直覺式思考)和系統2(邏輯式思考)

  • To allow for this complex reasoning, it is a good moment to step back and explore what reasoning entails.

  • To simplify, our methods of reasoning can be divided into system 1 and 2 thinking processes

Chain-of-Thought

Think Before Answering

  • The first and major step towards complex reasoning in generative models was through a method called Chain-of-Thought (CoT), which aims to have the generative model “think” first rather than answering the question directly without any reasoning.

thoughts(thinking process)



Chain-of-Thought prompting uses reasoning examples to persuade the LLM to use reasoning in its answer.

Zero-shot Chain-of-Thought

Let's think step-by-step

為何被稱為咒語也不是沒有道理

  • Although the prompt “Let’s think step-by-step” can improve the output, you are not constrained by this exact formulation.

  • Alternatives exist like “Take a deep breath and think step-by-step” and “Let’s work through this problem step-by-step”. The authors demonstrated the usefulness of coming up with alternative formulations.

Self-Consistency

Sampling Outputs

  • (temperature, top_p 調成不同),LLM 對於同一個問題的回覆不相同,如何處理?

  • 用表決來處理隨機性
    This method asks the generative model the same prompt multiple times and takes the majority result as the final answer.

By sampling from multiple reasoning paths, we can use majority voting to extract the most likely answer.

Tree-of-Thought

Exploring Intermediate Steps

A recent improvement can be found in Tree-of-Thought which allows for an in-depth exploration of several ideas.

避免call 太多次,可用不同角色來模擬

Tokenization

  • 在這些應用中,可以發現一個關鍵的問題。

  • LLM processes texts using tokens,which are common sequences of characters found in a set of text. (同時也是商用版的計價單位!)

openAI tokenizer vis

Other tool
\

The English-Centric Bias: Non-English languages may have less efficient tokenization.

比較為人所知的不公是訓練語料比例

影響了許多任務 (用英文取得最佳解答)

Unfairness in Tokenization

prompting for developer

用程式來開發與實驗

Deeplearning.ai

佛心課程

Lab

☑️ chatGPT prompt engineering for developers

Homework

Walk through Deeplearning.ai short course (🙏)

如何改進大型語言模型

  • continual pretraining
  • fine-tuning
  • RAG (retrieval-augmented generation)

References

  • Li, C., Wang, J., Zhang, Y., Zhu, K., Hou, W., Lian, J., Luo, F., Yang, Q., & Xie, X. (2023). Large Language models understand and can be enhanced by emotional stimuli. arXiv preprint arXiv:2307.11760.

  • Jay Alammar, Maarten Grootendorst. 2024. Hands-On Large Language Models. O'Reilly Media.

Although a prompt is a single piece of text it is tremendously helpful to think of prompts as pieces of a larger puzzle. Have I described the context of my question? Does the prompt have an example of the output?

This technique of chaining prompts allows the LLM to spend more time on each individual question instead of tackling the whole problem.

System 1 thinking represents automatic, intuitive, and near-instantaneous. It shares similarities with generative models that automatically generate tokens without any self-reflective behavior. In contrast, systems 2 thinking is a conscious, slow, and logical process, akin to brainstorming and self-reflection.

As illustrated in Figure 4-16, it provides examples in a prompt that demonstrate the reasoning the model should do before generating its response. These reasoning processes are referred to as “thoughts”.

--- ## Output verification