如何創建 Prompt Template

Langchain 提供了一個簡單而靈活的類 PromptTemplate 用于創建提示模板。可以通過硬編碼或動態參數化來定義模板。

示例代碼

from langchain import PromptTemplate

template = """
I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product}?
"""

prompt = PromptTemplate(
    input_variables=["product"],
    template=template,
)
prompt.format(product="colorful socks")

在上面的示例中,通過定義一個模板字符串和一組輸入變量,我們可以生成具體的提示內容。Langchain 的靈活性還允許用戶通過 from_template 方法自動推斷輸入變量。

Few Shot Examples 的作用

Few Shot Examples 是指一組示例,它們可以幫助模型生成更準確的響應。這些示例通過提供上下文和預期輸出,指導模型理解復雜的請求。

示例代碼

from langchain import PromptTemplate, FewShotPromptTemplate

examples = [
    {"word": "happy", "antonym": "sad"},
    {"word": "tall", "antonym": "short"},
]

example_formatter_template = """
Word: {word}
Antonym: {antonym}n
"""
example_prompt = PromptTemplate(
    input_variables=["word", "antonym"],
    template=example_formatter_template,
)

few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="Word: {input}nAntonym:",
    input_variables=["input"],
    example_separator="nn",
)

print(few_shot_prompt.format(input="big"))

通過 Few Shot Examples,用戶可以大幅提升模型的響應質量,尤其是在處理復雜或不常見的請求時。

Prompt Template 的高級用法

使用多個變量

Prompt Template 支持多個輸入變量,這使得模板可以在更復雜的場景中使用。

代碼示例

template = "請用簡明的語言介紹一下{topic},并解釋它的{aspect}。"

prompt_template = PromptTemplate(
    input_variables=["topic", "aspect"],
    template=template
)

input_variables = {"topic": "機器學習", "aspect": "應用"}
prompt = prompt_template.format(**input_variables)
print(prompt)

嵌套模板

通過嵌套模板,用戶可以在復雜場景中重用模板,提高代碼的復用性和可維護性。

聊天提示模板

Langchain 還支持聊天模型的提示模板,通過 ChatPromptTemplate 可以輕松構建帶有角色的聊天提示。

代碼示例

from langchain.prompts import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

system_message_prompt = SystemMessagePromptTemplate.from_template("您是將 {input_language} 翻譯成 {output_language} 的得力助手。")
human_message_prompt = HumanMessagePromptTemplate.from_template("{text}")

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()

通過這種方式,用戶可以構建復雜的聊天機器人,支持多角色交互。

Langchain Chat

應用模板與大模型交互

Prompt Template 可以與 OpenAI 等大模型結合使用,生成高質量的內容。

示例代碼

import openai
from langchain.prompts import PromptTemplate

template = "請用簡明的語言介紹一下{topic}。"
prompt_template = PromptTemplate(
    input_variables=["topic"],
    template=template
)

input_variables = {"topic": "人工智能"}
prompt = prompt_template.format(**input_variables)

openai.api_key = 'your-api-key'

response = openai.Completion.create(
    engine="davinci-codex",
    prompt=prompt,
    max_tokens=150
)

print("模型的響應:", response.choices[0].text.strip())

總結

Langchain 的 Prompt Template 提供了一種簡潔而強大的方式來與大型語言模型交互。通過靈活的模板定義和 Few Shot Examples 的支持,用戶可以顯著提高生成內容的質量和效率。

FAQ

  1. 問:Prompt Template 與 Few Shot Examples 如何結合使用?

  2. 問:如何在 Prompt Template 中使用多個變量?

  3. 問:Chat Prompt Template 有什么優勢?

Langchain 的高度靈活性和易用性使其成為處理大模型交互的理想選擇。通過合理使用 Prompt Template,您可以大幅提升工作效率并獲得更優質的輸出。

上一篇:

Claude API購買指南及使用詳解

下一篇:

使用 cURL 進行 POST 請求接口詳解
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費