迅速な詩人は、コードアプローチが低い開発者と非技術ユーザーの両方の迅速な設計を合理化および簡素化します。 YAMLとJINJA2の組み合わせを使用すると、PROMPT POETは柔軟で動的な迅速な作成を可能にし、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のミックスを使用します。テンプレート処理は、2つの主要な段階で発生します。
- 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の傑出した機能の1つは、実行時にテンプレート内で任意の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アフィニティとプレフィックスキャッシュをサポートしている場合、Character.AIの切り捨てアルゴリズムを使用してプレフィックスキャッシュレートを最大化します。プレフィックスキャッシュレートは、プロンプトトークンの総数でキャッシュから取得されたプロンプトトークンの数として定義されます。ユースケースの切り捨てステップとトークン制限の最適な値を見つけます。切り捨てステップが増加すると、プレフィックスキャッシュレートも上昇しますが、プロンプトからより多くのトークンが切り捨てられます。
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 }}:このPythonコードを同じディレクトリから実行して、ファイルchat_template.yml.j2を保存しました。
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:'