大語(yǔ)言模型發(fā)展到今天,寫(xiě)提示詞不僅是個(gè)體力活,還是一個(gè)技術(shù)活。提示詞的措辭稍微改一下,性能波動(dòng) 20-50% 是常有的事。那能不能讓 LLM 自己改進(jìn)提示詞呢?
本文主要講的就是這個(gè),一個(gè)處理復(fù)雜推理任務(wù)的 Agent,經(jīng)過(guò)幾輪自動(dòng)迭代,準(zhǔn)確率從 34% 飆到 97%相對(duì)提升 184%。下面是完整的步驟:從環(huán)境搭建到代碼實(shí)現(xiàn),還包含了優(yōu)缺點(diǎn)和適用場(chǎng)景。
什么是自動(dòng)提示詞優(yōu)化
自動(dòng)提示詞優(yōu)化有時(shí)也叫 Automatic Prompt Engineering(APE),核心思路是把寫(xiě)提示詞當(dāng)成優(yōu)化問(wèn)題來(lái)解。流程大概是這樣:先拿一個(gè)初始提示詞(哪怕寫(xiě)得很一般),在一批標(biāo)注好的樣本上跑一遍,用評(píng)分函數(shù)(精確匹配、BLEU、LLM-as-judge 之類的)算個(gè)分,然后讓 LLM 生成新的候選提示詞,挑表現(xiàn)好的繼續(xù)迭代,直到分?jǐn)?shù)不再漲為止。
2023 年的論文 "Automatic Prompt Optimization with Gradient-Free Methods" 把這套方法講得比較系統(tǒng)。從工具層面將DSPy、LangChain 的 PromptOptimizer 都能干這個(gè)活,不過(guò)有一個(gè)專門(mén)的庫(kù)叫 OPIK 。
OPIK + MetaPromptOptimizer 的特點(diǎn)
OPIK 這套東西比較吸引人的是完全開(kāi)源,Apache 2.0 協(xié)議,商用也不花錢;可以純本地跑,Ollama、LM Studio、任何 OpenAI 兼容的接口都行;自帶追蹤儀表板,功能類似 LangSmith 但是確是免費(fèi);API 設(shè)計(jì)簡(jiǎn)單,一個(gè)類就能搞定優(yōu)化流程;評(píng)估指標(biāo)隨便你定義,只要能寫(xiě)成代碼就行。
環(huán)境搭建
創(chuàng)建虛擬環(huán)境
python -m venv opik-apo
source opik-apo/bin/activate # Windows: opik-apo\Scripts\activate
pip install opik[all] datasets sentence-transformers
本地 LLM 安裝(可選但推薦)
# Example with Ollama
ollama pull llama3.2:8b-instruct-qat
ollama serve
準(zhǔn)備評(píng)估數(shù)據(jù)集
不需要太多數(shù)據(jù),20 到 100 條高質(zhì)量樣本就夠了。下面是個(gè) GSM8K 風(fēng)格的 JSONL 例子,保存成 eval_data.jsonl:
{"question": "Janet's ducks lay 16 eggs per day. She eats 3 for breakfast, gives 8 to friends, and her bakery uses the rest. How many eggs does the bakery receive?", "answer": "5"}
{"question": "A store sells apples for $2 each or 6 for $10. How much do you save per apple by buying 6?", "answer": "0.33"}
完整代碼
import opik
from opik import track
from opik.opimization import MetaPromptOptimizer
from opik.opimization.evaluation_metrics import LevenshteinRatio
from opik.integrations.langchain import OpikTracer
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
import json
# 1. 初始化 Opik(默認(rèn)本地)
opik_client = opik.Opik()
# 2. 加載評(píng)估數(shù)據(jù)集
def load_dataset(path="eval_data.jsonl"):
data = []
with open(path) as f:
for line in f:
item = json.loads(line)
data.append({"input": item["question"], "expected": item["answer"]})
return data
eval_dataset = load_dataset()
# 3. 定義你的基礎(chǔ)提示詞模板
initial_prompt = """You are an expert assistant. Your task is answer questions accurately and concisely. Consider the context carefully before responding.
Question: {input}
Answer:"""
prompt_template = PromptTemplate.from_template(initial_prompt)
# 4. 創(chuàng)建 LLM(本地或 OpenAI)
llm = Ollama(model="llama3.2:8b-instruct-qat", temperature=0.2)
# llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.2) # for cloud
# 5. 定義你想要優(yōu)化的鏈
@track
def math_chain(question):
prompt = prompt_template.format(input=question)
response = llm.invoke(prompt)
return response.strip()
# 6. 定義評(píng)估函數(shù)
def evaluation_function(task):
expected = task["expected"]
generated = math_chain(task["input"])
score = LevenshteinRatio()(expected, generated)
return score
# 7. 運(yùn)行優(yōu)化器
optimizer = MetaPromptOptimizer(
task="You are an expert at solving math word problems.",
evaluation_dataset=eval_dataset,
evaluation_function=evaluation_function,
llm=llm,
n_iterations=5, # 通常 3-7 次就足夠了
population_size=10
)
best_prompt = optimizer.run()
print("\n=== BEST PROMPT FOUND ===\n")
print(best_prompt)
運(yùn)行腳本:
python optimize_prompt.py
跑完 5 輪迭代后,打開(kāi)http://localhost:5173就能看到儀表板,每個(gè)候選提示詞的得分、最終選中的版本都在上面。
優(yōu)化效果示例
優(yōu)化前的提示詞(34% 精確匹配):
You are an expert assistant. Your task is answer questions accurately and concisely. Consider the context carefully before responding.
優(yōu)化后(97% 精確匹配):
![]()
優(yōu)缺點(diǎn)總結(jié)
優(yōu)點(diǎn)
![]()
性能提升幅度可觀,50-200% 的相對(duì)改進(jìn)并不罕見(jiàn)。整個(gè)過(guò)程完全可復(fù)現(xiàn),每一輪迭代都有日志記錄,方便審計(jì)。本地運(yùn)行意味著初始配置之后就不再產(chǎn)生 Token 費(fèi)用。也不依賴梯度任何黑盒 LLM 都能用。遷移到新任務(wù)也簡(jiǎn)單,改幾個(gè)參數(shù)就行。
局限性
評(píng)估數(shù)據(jù)集的質(zhì)量至關(guān)重要:如果數(shù)據(jù)不行,優(yōu)化出來(lái)的提示詞也不會(huì)好到哪去,就是典型的 garbage in, garbage out;本地 LLM 生成候選提示詞的速度可能偏慢;小數(shù)據(jù)集上容易過(guò)擬合模型可能只是"記住"了答案而非真正學(xué)會(huì)解題;優(yōu)化出來(lái)的提示詞有時(shí)候又長(zhǎng)又怪,雖然效果好,但可讀性堪憂。
結(jié)論
OPIK 的 MetaPromptOptimizer 讓自動(dòng)提示詞變得很簡(jiǎn)單:拿一個(gè)勉強(qiáng)能用的提示詞,半小時(shí)內(nèi)就能把它調(diào)成接近 SOTA 水平的 one-shot Agent,省下原本要花幾周手動(dòng)迭代的時(shí)間。
找一個(gè)現(xiàn)有的 Agent,攢 30 條標(biāo)注樣本,跑一遍上面的腳本,看準(zhǔn)確率曲線往上走就對(duì)了。提示詞工程的下一步,不是人寫(xiě)出更好的提示詞,而是讓系統(tǒng)替人寫(xiě)。
地址
https://avoid.overfit.cn/post/be1860b7176b474b9389d88501402048
作者:Minervee
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺(tái)“網(wǎng)易號(hào)”用戶上傳并發(fā)布,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.