促使诗人精简并简化了具有低代码方法的开发人员和非技术用户的及时设计。及时诗人使用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:'