يقوم الشاعر المطالب بتبسيط وتبسيط التصميم السريع لكل من المطورين والمستخدمين غير التقنيين مع نهج الكود المنخفض. باستخدام مزيج من YAML و Jinja2 ، يسمح الشاعر المطري بإنشاء موجه مرن وديناميكي ، وتعزيز كفاءة وجودة التفاعلات مع نماذج الذكاء الاصطناعي. يوفر الوقت في معالجة سلسلة الهندسة ، مما يتيح للجميع التركيز أكثر على صياغة المطالبات المثلى لمستخدميها.
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 و Breadix 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 )باختصار ، اقتطاع عملية اقتطاع مدركة لذاكرة التخزين المؤقت حتى نقطة اقتطاع ثابت في كل مرة يتم فيها استدعاءها - تحريك نقطة الاقتطاع هذه فقط في المتوسط كل تحول. يتيح هذا لمزود 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:'