พรอมต์กวีมีความคล่องตัวและลดความซับซ้อนของการออกแบบที่รวดเร็วสำหรับทั้งนักพัฒนาและผู้ใช้ที่ไม่ใช่ด้านเทคนิคด้วยวิธีการรหัสต่ำ การใช้การผสมผสานของ 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 %} Prompt Poet จะตัดช่องว่างโดยค่าเริ่มต้นเพื่อหลีกเลี่ยงสายใหม่ที่ไม่พึงประสงค์ในพรอมต์สุดท้ายของคุณ หากคุณต้องการรวมพื้นที่ที่ชัดเจนให้ใช้เครื่องหมายอวกาศในตัวพิเศษ“ <| 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 โดยพลการโดยตรงภายในเทมเพลตที่รันไทม์ คุณลักษณะนี้มีความสำคัญสำหรับการดึงข้อมูลแบบ on-the-fly การจัดการและการตรวจสอบความถูกต้องทำให้เพรียวลมวิธีการสร้างพรอมต์ ที่นี่ 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 % } โดยค่าเริ่มต้นกวีพรอมต์จะใช้ tokenizer 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 Affinity และ Prefix 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 }}: เรียกใช้รหัส 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:'