프롬프트 시인은 코드 접근 방식이 낮은 개발자와 비 기술적 사용자 모두에게 신속한 설계를 간소화하고 단순화합니다. 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 선호도 및 접두사 캐시를 지원하는 경우 문자를 사용하여 접두사 캐시 속도를 최대화하기 위해 AAI의 Truncation 알고리즘을 사용하십시오. 접두사 캐시 속도는 총 프롬프트 토큰 수보다 캐시에서 검색된 프롬프트 토큰 수로 정의됩니다. Truncation 단계 및 사용 사례의 토큰 제한에 대한 최적의 값을 찾으십시오. 잘림 단계가 증가함에 따라 접두사 캐시 속도도 상승하지만 프롬프트에서 더 많은 토큰이 잘립니다.
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)이 잘림 지점은 매 턴을 이동하여 접두사 캐시 속도가 크게 줄어 듭니다. 이 접근법의 트레이드 오프는 우리가 종종 우리가 필요한 것보다 더 많은 것을 잘라내는 것입니다.

템플릿 레지스트리는 단순히 템플릿을 디스크에 파일로 저장하는 개념입니다. 템플릿 레지스트리를 사용하면 파이썬 코드에서 템플릿 파일을 격리하고 디스크에서 직접이 파일을로드 할 수 있습니다. 프로덕션 시스템에서 이러한 템플릿 파일은 연속적인 용도에 대한 메모리 캐시에서 선택적으로로드하여 디스크 I/O를 저장할 수 있습니다. 앞으로 템플릿 레지스트리는 프롬프트 시인의 일류 시민이 될 수 있습니다.
filename : 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 를 저장 한 동일한 디렉토리 에서이 파이썬 코드를 실행하십시오.
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:'