LLM及LangChain開發筆記(11)_文本語言模型LLMs的FewShotPromptTemplate
透過給模型幾個示範範例,讓它更好地模仿這些範例風格來回答新問題。
示範程式1.實作一個 few-shot learning 對話提示系統。
使用 LangChain + OpenAI GPT 模型(gpt-3.5-turbo-instruct)
透過 few-shot learning(少樣本學習) 方式,讓 GPT 模型根據兩個範例學會風格與語氣,並回答新的問題。
import openai import os from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) # read local .env file openai.api_key = os.getenv('OPENAI_API_KEY') from langchain import FewShotPromptTemplate, PromptTemplate from langchain.llms import OpenAI llm = OpenAI(model_name="gpt-3.5-turbo-instruct",temperature=0.0,max_tokens=1024) # create our examples examples = [ { "query": "你好嗎?", "answer": "還不錯,但有時我們仍會抱怨。" }, { "query": "現在幾點了?", "answer": "是時候買支手錶了。" } ] # create a example template example_template = """ User: {query} AI: {answer} """ # create a prompt example from above template example_prompt = PromptTemplate( input_variables=["query", "answer"], template=example_template, ) # the prefix is our instructions prefix = """下面是與一位 AI 助手的對話摘錄。該助手通常很誠實和風趣,對用戶的問題做出創造性和有趣的回答。 以下是一些例子: """ suffix = """ User: {query} AI: """ # now create the few shot prompt template few_shot_prompt_template = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["query"], example_separator="\n\n" ) query = "生命的意義是什麼?" print(few_shot_prompt_template.format(query=query)) print(llm.invoke( few_shot_prompt_template.format(query=query) ))
初始化 OpenAI 模型
- gpt-3.5-turbo-instruct 是針對「指令跟隨」場景設計的模型(輸入一段 prompt → 生成補全)。
- temperature=0.0:輸出更穩定不隨機。
- max_tokens=1024:最多回傳 1024 個 token。
定義 Few-shot 範例資料
- 模擬 AI 風格——誠實、有點幽默,會影響之後模型的回答風格。
範例模板格式定義
- 定義每筆對話的格式,將每筆 query 和 answer 套用進去。
設定 Prompt 前後文
- prefix:引導語,說明 AI 的風格。
- suffix:接上你這次要問的問題({query}),讓模型生成 AI 的回應。
建立 Few-Shot 範例,提供一些「範例對話」,讓模型學習對問題做出幽默、創意回答。
定義每組範例格式,將範例包裝為固定格式(提示樣板):User 問什麼,AI 怎麼答。
定義 Prompt 結構
- prefix:給模型的背景指令(角色、語調風格設定)。
- suffix:加入新的提問,請模型繼續回應。
建立完整的 Prompt 組合器
- 前綴 prefix +兩個範例(按 example_prompt 格式展開)+suffix 中帶入使用者新問題
- 組合成完整 prompt,送給 GPT 模型。
執行查詢與輸出結果
- 第一行印出完整 prompt 結果(可供除錯與確認)。
- 第二行呼叫 GPT 模型並印出回應結果。
示範程式2.LengthBasedExampleSelector的使用。
使用 LangChain 建立了一個具備 few-shot 提示樣板(prompt template) 的對話系統,並使用 LengthBasedExampleSelector 根據長度自動選擇合適的 few-shot 範例,最終輸出給語言模型來生成回應。
examples = [ { "query": "你怎麼樣?", "answer": "我不能抱怨但我有時還是會這樣做。" }, { "query": "現在幾點了?", "answer": "該買塊手錶了。" }, { "query": "生命的意義是什麼?", "answer": "吃飽睡好" }, { "query": "今天天氣怎麼樣?", "answer": "多雲,有下大雨可能。" }, { "query": "你用什麼類型的人工智慧來處理複雜任務?", "answer": "我使用了最先進的神經網絡、模糊邏輯和一點魔法。" }, { "query": "你最喜歡的顏色是什麼?", "answer": "天空藍" }, { "query": "你最喜歡的食物是什麼?", "answer": "基於碳的有機體。" }, { "query": "你最喜歡的電影是什麼?", "answer": "駭客任務" }, { "query": "世界上最好的東西是什麼?", "answer": "完美的披薩。" }, { "query": "你最好的朋友是誰?", "answer": "Siri。我們經常就生命的意義展開熱情的辯論。" }, { "query": "如果你可以做世界上任何事情,你會做什麼?", "answer": "當然是征服世界!" }, { "query": "我應該去哪裡旅行?", "answer": "如果你想要冒險,試試外緣地帶。" }, { "query": "我今天應該做什麼?", "answer": "別再網上和聊天機器人聊天了,出門走走。" } ]
- PromptTemplate:定義提示樣板(如一組對話格式)。
- FewShotPromptTemplate:允許使用多個 few-shot 範例的提示組合器。
- LengthBasedExampleSelector:根據提示長度自動挑選合適的 few-shot 範例(避免 prompt 過長)。
- prefix 是提示開頭:引導語言模型理解角色與回答風格
- suffix 是你的「真實提問」的格式結尾(使用者的輸入 query)
from langchain.prompts.example_selector import LengthBasedExampleSelector from langchain import FewShotPromptTemplate, PromptTemplate example_template = """ User: {query} AI: {answer} """ example_prompt = PromptTemplate( input_variables=["query", "answer"], template=example_template ) example_selector = LengthBasedExampleSelector( examples=examples, example_prompt=example_prompt, max_length=50 # 設定範例總長度上限(避免提示太長) ) prefix = """下面是與一位 AI 助手的對話摘錄。該助手通常很誠實和風趣,對用戶的問題做出創造性和有趣的回答。 以下是一些例子: """ suffix = """ User: {query} AI: """ dynamic_prompt_template = FewShotPromptTemplate( example_selector=example_selector, # 使用 selector 而非靜態 examples example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["query"], example_separator="\n" ) print(dynamic_prompt_template.format(query="鳥兒是怎麼飛的?")) print(llm.invoke(dynamic_prompt_template.format(query="鳥兒是怎麼飛的?")))
example_selector = LengthBasedExampleSelector(
examples=examples, # 範例集(你之前定義好的 examples[])
example_prompt=example_prompt, # 套用上述格式
max_length=50 # 最大長度限制(字符或 token 數,避免 prompt 過長)
)
會自動根據 examples 的長度動態選出「不超過 max_length」的範例。
目的是確保 prompt 不會過大導致 token 超限。
留言
張貼留言