Skip to main content

Prompt Template

使用 Prompt Template 核心組件來建立一個 提示,為 LLM 或 agent 提供指令和上下文,與其他輸入(如聊天訊息和檔案上傳)分開。

提示是結構化的輸入,使用自然語言、固定值和動態變數為 LLM 提供基準上下文。例如:

  • 為使用者查詢定義一致的結構,讓 LLM 更容易理解和適當回應。
  • 為 LLM 定義特定的輸出格式,例如 JSON 或結構化文本。
  • 為 LLM 定義角色,例如 You are a helpful assistantYou are an expert in microbiology
  • 允許 LLM 參考聊天記憶。

Prompt Template 組件也可以將變數指令輸出到 Flow中後續的其他組件。

Prompt Template parameters

NameDisplay NameDescription
templateTemplateInput parameter. Create a prompt template with dynamic variables in curly braces, such as {VARIABLE_NAME}.

如果您的範本包含文字和變數,您可以使用雙大括號來轉義範本中的文字大括號,並防止將該文字解釋為變數。 例如:This is a template with {{literal text in curly braces}} and a {variable}

promptPrompt MessageOutput parameter. The built prompt message returned by the build_prompt method.

Define variables in prompts

Prompt Template 組件中的變數會動態為 Prompt Template 組件新增欄位,讓您的FLOW可以從其他組件、AgentBuilder 全域變數或固定輸入接收這些值的定義。

例如,使用 Message History 組件,您可以使用 {memory} 變數將聊天歷史傳遞給提示。 但是,Agent 組件預設啟用了內建的聊天記憶。 如需更多資訊,請參閱記憶管理選項

以下步驟示範如何將變數新增到 Prompt Template 組件:

  1. 根據 Basic prompting 範本建立 Flow。

    此範本已經有 Prompt Template 組件,但範本只包含自然語言指令:Answer the user as if you were a GenAI expert, enthusiastic about helping them get started building something fresh.

    此提示為 LLM 的聊天互動定義角色,但不包含有助於根據不同使用者和環境變化的上下文動態調整的變數。

  2. 點擊 Prompt Template 組件,然後在 Template 欄位中新增一些變數。

    變數透過將變數名稱包圍在大括號中來宣告,例如 {variable_name}。 例如,以下範本建立 contextuser_question 變數:


    _10
    Given the context:
    _10
    _10
    {context}
    _10
    _10
    Answer the question:
    _10
    _10
    {user_question}

    如果您的範本包含文字和變數,您可以使用雙大括號來轉義範本中的文字大括號,並防止將該文字解釋為變數。 例如:This is a template with {{literal text in curly braces}} and a {variable}

  3. 點擊 Check & Save 儲存範本。

    將變數新增到範本後,每個變數都會在 Prompt Template 組件中新增欄位。

  4. 為變數欄位提供輸入:

    • 將欄位連接到其他組件,將這些組件的輸出傳遞給變數。
    • 使用 AgentBuilder 全域變數。
    • 直接在欄位中輸入固定值。

您可以在範本中新增任意數量的變數。 例如,您可以為 {references}{instructions} 新增變數,然後從其他組件(如 Text InputURLFile 組件)輸入該資訊。

See also

Search