- 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 }}:
ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">PromptPoet 从 UI 设计中汲取灵感,将提示视为其运行时状态的动态函数,涵盖模板、数据和令牌限制等元素。ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">提示模板ingFang SC", Cambria, Cochin, Georgia, serif;font-size: medium;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 400;orphans: 2;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">PromptPoet 将重点从提示工程转向设计,使用户能够在模板上进行迭代,而不是在代码上。利用 YAML 和 Jinja2 的结合,这些模板既灵活又易于适应,促进了高效的提示创建和管理。模板处理涉及两个主要阶段:渲染:这个初始阶段涉及 Jinja2 处理输入数据,执行控制逻辑,验证数据,将其绑定到变量,并在模板内评估函数。
加载:渲染后,输出被结构化为 YAML 文件,由重复的段落组成,每个段落组织成一个 Python 数据结构。这些段落具有几个关键属性:
名称:段落的清晰标识符。
内容:构成提示一部分的字符串负载。
角色(可选):指定参与者角色,帮助区分用户和系统组件。
截断优先级(可选):在需要时建立截断顺序,具有相同优先级的段落按顺序截断。
PromptPoet 库提供了众多功能,包括标记化和截断,优化了高效缓存和快速响应。这些功能对于提升性能至关重要。
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,PromptPoet 提供了一种强大而富有表现力的模板语言。Jinja2 允许在模板内直接绑定数据、调用函数和进行基本控制流。YAML 提供结构,使在超出令牌限制时能够进行复杂的截断。这种结合类似于 Ansible 等系统中使用的方法。
提示可移植性
在 Character.AI,模型增强是持续进行的,以更好地与用户偏好对齐。为此,提示需要在离线环境中重建,以便进行评估和后训练任务。模板化提示使团队之间的无缝共享成为可能,无需集成单独的代码段。
模板内的函数调用
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 %}
自定义编码选项
默认情况下,PromptPoet 使用 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 亲和性和前缀缓存,可以利用 Character.AI 的截断算法来最大化前缀缓存率。该率由缓存提示令牌与总提示令牌的比率决定。调整截断步骤和令牌限制可以优化缓存性能,尽管增加步骤可能会导致更多的令牌截断。
截断策略在通过优化消息截断来实现高缓存率方面至关重要。该策略并不是每次都截断到固定的令牌限制,而是每隔几轮截断到一个稳定点。这种方法保持了连续的令牌序列,最大化了GPU前缀缓存的使用。仅在必要时移动截断点确保了资源的有效利用。
简单截断问题
在包含消息M1到M10的聊天场景中,简单截断在每轮调整截断点,降低了缓存效率并增加了计算成本。
缓存感知截断的好处
Character.AI的缓存感知算法在每隔几轮保持一致的截断点,维护到最新消息的令牌序列。这允许重用来自前几轮的缓存计算,从而提高效率。参数k反映了截断步骤与平均令牌数量之间的平衡,尽管无法直接控制。
PromptPoet现在是开源的,应该会鼓励许多开发者在其功能基础上进行构建。至少,PromptPoet已经封装了Character.ai在提示设计方面学到的许多经验教训。无疑是对提示工程领域的一个受欢迎的补充。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |