促使詩人精簡併簡化了具有低代碼方法的開發人員和非技術用戶的及時設計。及時詩人使用YAML和Jinja2的混合,可以靈活,動態的及時創建,從而提高與AI模型的互動的效率和質量。它節省了工程串操作的時間,使每個人都可以更多地專注於為用戶制定最佳提示。
pip install prompt-poet import os
import getpass
from prompt_poet import Prompt
from langchain import ChatOpenAI
# Uncomment if you need to set OPENAI_API_KEY.
# os.environ["OPENAI_API_KEY"] = getpass.getpass()
raw_template = """
- name: system instructions
role: system
content: |
Your name is {{ character_name }} and you are meant to be helpful and never harmful to humans.
- name: user query
role: user
content: |
{{ username}}: {{ user_query }}
- name: response
role: user
content: |
{{ character_name }}:
"""
template_data = {
"character_name" : "Character Assistant" ,
"username" : "Jeff" ,
"user_query" : "Can you help me with my homework?"
}
prompt = Prompt (
raw_template = raw_template ,
template_data = template_data
)
model = ChatOpenAI ( model = "gpt-4o-mini" )
response = model . invoke ( prompt . messages )及時的詩人模板使用YAML和Jinja2的混合物。模板處理髮生在兩個主要階段:
- name : system instructions
role : system
content : |
Your name is {{ character_name }} and you are meant to be helpful and never harmful to humans.
- name : user query
role : user
content : |
{{ username}}: {{ user_query }}
- name : reply_prompt
role : user
content : |
{{ character_name }}: 如果您在列表中有元素(例如消息),則可以將它們解析到模板中。
{% for message in current_chat_messages %}
- name : chat_message
role : user
content : |
{{ message.author }}: {{ message.content }}
{% endfor %} 上下文長度是有限的,不能總是適合整個聊天歷史記錄 - 因此我們可以在消息部件上設置截斷優先級,而提示詩人將以它們顯示的順序截斷這些部分(最古老到最新)。
{% for message in current_chat_messages %}
- name : chat_message
role : user
truncation_priority : 1
content : |
{{ message.author }}: {{ message.content }}
{% endfor %} 根據用戶的當前方式(音頻或文本)量身定制說明。
{% if modality == "audio" %}
- name : special audio instruction
role : system
content : |
{{ username }} is currently using audio. Keep your answers succinct.
{% endif %} 在需要時包含特定於上下文的示例,例如作業幫助。
{% if extract_user_query_topic(user_query) == "homework_help" %}
{% for homework_example in fetch_few_shot_homework_examples(username, character_name) %}
- name : homework_example_{{ loop.index }}
role : user
content : |
{{ homework_example }}
{% endfor %}
{% endif %} 提示詩人默認情況下將剝離空格,以避免在最終提示中避免新的新線。如果要包含一個明確的空間,請使用特殊的內置空間標記“ <| Space |>”來確保正確格式。
- name : system instructions
role : system
content : |
Your name is {{ character_name }} and you are meant to be helpful and never harmful to humans.
- name : user query
role : user
content : |
<|space|>{{ username}}: {{ user_query }} 組成性是迅速詩人模板的核心優勢,可以創建複雜的動態提示。
- name : system instructions
role : system
content : |
Your name is {{ character_name }} and you are meant to be helpful and never harmful to humans.
{% if modality == "audio" %}
- name : special audio instruction
role : system
content : |
{{ username }} is currently using audio modality. Keep your answers succinct and to the point.
{% endif %}
{% if extract_user_query_topic(user_query) == "homework_help" %}
{% for homework_example in fetch_few_shot_homework_examples(username, character_name) %}
- name : homework_example_{{ loop.index }}
role : user
content : |
{{ homework_example }}
{% endfor %}
{% endif %}
{% for message in current_chat_messages %}
- name : chat_message
role : user
truncation_priority : 1
content : |
{{ message.author }}: {{ message.content }}
{% endfor %}
- name : user query
role : user
content : |
{{ username}}: {{ user_query }}
- name : reply_prompt
role : user
content : |
{{ character_name }}: 要在模板中保持乾燥的原理,請將它們分解為可重複使用的部分,這些部分可以在不同的模板上應用,例如A/B測試新提示時。
{% include 'sections/system_instruction.yml.j2' %}
{% include 'sections/audio_instruction.yml.j2' %}
{% if extract_user_query_topic(user_query) == "homework_help" %}
{% include 'sections/homework_examples.yml.j2' %}
{% endif %}
{% include 'sections/chat_messages.yml.j2' %}
{% include 'sections/user_query.yml.j2' %}
{% include 'sections/reply_prompt.yml.j2' %}提示詩人庫提供了各種功能和設置,包括提示屬性。諸如令牌化和截斷之類的關鍵特徵有助於有效緩存和低潛伏期響應
prompt . tokenize ()
prompt . truncate ( token_limit = TOKEN_LIMIT , truncation_step = TRUNCATION_STEP )
# Inspect prompt as a raw string.
prompt . string : str
> >> "..."
# Inpsect the prompt as raw tokens.
prompt . tokens : list [ int ]
> >> [...]
# Inspect the prompt as LLM API message dicts.
prompt . messages : list [ dict ]
> >> [...]
# Inspect the prompt as first class parts.
prompt . parts : list [ PromptPart ]
> >> [...]Jinja2和Yaml合併以提供一種令人難以置信的擴展和表現力的模板語言。 Jinja2促進了直接數據綁定,任意函數調用和模板內的基本控制流。 YAML為我們的模板提供結構(深度= 1),使我們在達到令牌限制時可以執行複雜的截斷。 Jinja2和YAML的這種配對並不是唯一的 - 最值得注意的是Ansible使用。
Jinja2的一個出色功能是能夠在運行時直接在模板中調用任意Python函數。此功能對於即時數據檢索,操縱和驗證至關重要,即精簡提示的構建方式。這裡extract_user_query_topic可以對模板控制流中使用的用戶查詢進行任意處理 - 也許是通過對主題分類器進行往返。
{ % if extract_user_query_topic ( user_query ) == "homework_help" % }
{ % for homework_example in fetch_few_shot_homework_examples ( username , character_name ) % }
- name : homework_example_ {{ loop . index }}
role : user
content : |
{{ homework_example }}
{ % endfor % }
{ % endif % }默認情況下,詩人將使用Tiktoken“ O200K_Base”令牌,儘管可以在頂級tiktoken_encoding_name中提供替代編碼名稱。另外,用戶可以使用頂級encode_func: Callable[[str], list[int]]提供自己的編碼功能。
from tiktoken import get_encoding
encode_func = get_encoding ( "o200k_base" )
prompt = Prompt (
raw_template = raw_template ,
template_data = template_data ,
encode_func = encode_func
)
prompt . tokenize ()
prompt . tokens
> >> [...]如果您的LLM提供商支持GPU親和力和前綴緩存,請使用targin.ai的截斷算法來最大化前綴-CACHE速率。前綴緩存速率定義為在提示令的總數上從緩存中檢索到的提前令牌的數量。為您的用例找到截斷步驟和令牌限制的最佳值。隨著截斷步驟的增加,前綴緩存速率也會上升,但是從提示中截斷了更多的令牌。
TOKEN_LIMIT = 128000
TRUNCATION_STEP = 4000
# Tokenize and truncate the prompt.
prompt . tokenize ()
prompt . truncate ( token_limit = TOKEN_LIMIT , truncation_step = TRUNCATION_STEP )
response = model . invoke ( prompt . messages )簡而言之,緩存意識到的截斷每次調用時都會截斷至固定的截斷點 - 僅每k轉彎平均移動此截斷點。這使您的LLM提供商可以最大程度地利用GPU的前綴緩存,以優化推理。相反,如果我們簡單地截斷直到達到令牌極限(l),則此截斷點將移動每個回合,這將大大降低前綴緩存速率。這種方法的權衡是,我們經常截斷超出了我們嚴格的要求。

模板註冊表僅僅是將模板作為磁盤上的文件存儲的概念。在使用模板註冊表時,您可以將模板文件從Python代碼隔離,然後直接從磁盤加載這些文件。在生產系統中,可以選擇從連續用途的內存中加載這些模板文件,並保存在磁盤I/O上。將來,模板註冊表可能會成為迅速詩人的一流公民。
文件名: chat_template.yml.j2
- name : system instructions
role : system
content : |
Your name is {{ character_name }} and you are meant to be helpful and never harmful to humans.
- name : user query
role : user
content : |
{{ username}}: {{ user_query }}
- name : response
role : user
content : |
{{ character_name }}:從您已將文件chat_template.yml.j2同一目錄中運行此Python代碼。
from prompt_poet import Prompt
prompt = Prompt (
template_path = "chat_template.yml.j2" ,
template_data = template_data
)
print ( prompt . string )
> >> 'Your name is Character Assistant and you are meant to be helpful and never harmful to humans.Jeff: Can you help me with my homework?Character Assistant:'