PromptTemplate
使用場景
單對話文本輸出場景下使用,例如文本翻譯 提取標簽等
使用方式
prompt = PromptTemplate.from_template(
"基于如下要求寫一幅對聯(lián):{text}"
)
chain = prompt|model
print(chain.invoke({"text": "喜慶,給新人用的"}))
ChatPromptTemplate
使用場景
對話模式下使用,里面會包含對話的內容
使用方式
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate(
[
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
]
)
print(prompt.format(name="小張", user_input="hello"))
- 輸出
System: You are a helpful AI bot. Your name is 小張.
Human: Hello, how are you doing?
AI: I'm doing well, thanks!
Human: hello
SystemMessagePromptTemplate、AIMessagePromptTemplate、HumanMessagePromptTemplate
使用場景
結合ChatPromptTemplate使用,將里面原來的用("<role>","<message>")替換為對象形式
使用方式
from langchain_core.prompts import ChatPromptTemplate,SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate
prompt = ChatPromptTemplate(
[
SystemMessagePromptTemplate.from_template( "You are a helpful AI bot. Your name is {name}."),
HumanMessagePromptTemplate.from_template("Hello, how are you doing?"),
AIMessagePromptTemplate.from_template( "I'm doing well, thanks!"),
HumanMessagePromptTemplate.from_template("{user_input}"),
]
)
print(prompt.format(name="小張", user_input="hello"))
FewShotPromptTemplate
使用場景
給大模型一些示例,需要結合PromptTemplate使用
使用示例
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
example_data = [
{"user_id":"123242","message":"格式不正確"},
{"user_id":"W23242","message":"處理成功"},
{"user_id":"A23242","message":"格式不正確"},
{"user_id":"W23242","message":"處理成功"},
] # 數(shù)據(jù)集
# 數(shù)據(jù)集的拼接模版
example_template = PromptTemplate.from_template(
"用戶ID:{user_id}\n處理結果:{message}"
)
few_prompt = FewShotPromptTemplate(
examples=example_data , # 傳入準備好的示例數(shù)據(jù)列表
example_prompt=example_template , # 傳入剛才定義的單個示例排版模板
prefix="請模仿以下示例,處理數(shù)據(jù):", # 放在所有示例前面的引導語
suffix="用戶ID:{user_id}\n處理結果:", # 放在所有示例后面的真實提問
)
print(few_prompt .format(user_id="2343242"))
- 輸出
用戶ID:W23242
處理結果:處理成功
用戶ID:A23242
處理結果:格式不正確
用戶ID:W23242
處理結果:處理成功
用戶ID:2343242
處理結果:
FewShotChatMessagePromptTemplate
使用場景
在對話模式下使用,主要是仿照出對話時的回答示例
使用方式
from langchain_core.prompts import FewChatMessagePromptTemplate, ChatPromptTemplate
# 1. 準備示例數(shù)據(jù)
examples = [
{"input": "2+2", "output": "4"},
{"input": "2+3", "output": "5"},
]
# 2. 定義單個示例的格式化模板(這里必須用 ChatPromptTemplate 的格式)
# 這里的 from_template 會自動把 input 和 output 轉化為 HumanMessage 和 AIMessage
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{output}"),
]
)
# 3. 創(chuàng)建 FewShotChatMessagePromptTemplate
few_shot_prompt = FewShotChatMessagePromptTemplate(
examples=examples,
example_prompt=example_prompt,
)
# 4. 將它嵌入到最終的 ChatPromptTemplate 中
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "你是一個計算小能手,請模仿示例回答用戶的問題。"),
few_shot_prompt, # 直接把少樣本提示作為消息的一部分插進來
("human", "{input}"),
]
)
# 5. 查看最終生成的消息結構
messages = final_prompt.invoke({"input": "10-3"})
for msg in messages:
print(f"{msg.type}: {msg.content}")
# 輸出結果會是一個完美的對話消息流:
# system: 你是一個計算小能手,請模仿示例回答用戶的問題。
# human: 2+2
# ai: 4
# human: 2+3
# ai: 5
# human: 10-3