Convert from yaml to python. here is an example of a correct conversion.
yaml
instruction_template: |-
{%- set ns = namespace(found=false) -%}
{%- for message in messages -%}
{%- if message['role'] == 'system' -%}
{%- set ns.found = true -%}
{%- endif -%}
{%- endfor -%}
{%- if not ns.found -%}
{{- '<|im_start|>system\n' + '' + '<|im_end|>\n' -}}
{%- endif %}
{%- for message in messages %}
{%- if message['role'] == 'system' -%}
{{- '<|im_start|>system\n' + message['content'] + '<|im_end|>\n' -}}
{%- else -%}
{%- if message['role'] == 'user' -%}
{{-'<|im_start|>user\n' + message['content'] + '<|im_end|>\n'-}}
{%- else -%}
{{-'<|im_start|>assistant\n' + message['content'] + '<|im_end|>\n' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{-'<|im_start|>assistant\n'-}}
{%- endif -%}
this yaml becomes
python
class ChatmlPromptStyle(AbstractPromptStyleWithSystemPrompt):
def __init__(self, default_system_prompt: str | None = None) -> None:
default_system_prompt = default_system_prompt or self._DEFAULT_SYSTEM_PROMPT
super().__init__(default_system_prompt)
self.system_prompt: str = default_system_prompt
def _messages_to_prompt(self, messages: Sequence[ChatMessage]) -> str:
messages = list(messages)
if messages[0].role != MessageRole.SYSTEM:
logger.info(
"Adding system_promt='%s' to the given messages as there are none given in the session",
self.system_prompt,
)
messages = [
ChatMessage(content=self.system_prompt, role=MessageRole.SYSTEM),
*messages,
]
return self._format_messages_to_prompt(messages)
def _completion_to_prompt(self, completion: str) -> str:
return (
f"<|im_start|>system\n{self.system_prompt.strip()}<|im_end|>\n"
f"<|im_start|>user\n{completion.strip()}<|im_end|>\n"
"<|im_start|>assistant"
)
Now used what you have learned in the example to convert this yaml to python:
python
instruction_template: |-
{%- set ns = namespace(found=false) -%}
{%- for message in messages -%}
{%- if message['role'] == 'system' -%}
{%- set ns.found = true -%}
{%- endif -%}
{%- endfor -%}
{%- if not ns.found -%}
{{- '' + '' + '' -}}
{%- endif %}
{%- for message in messages %}
{%- if message['role'] == 'system' -%}
{{- '' + message['content'] + '' -}}
{%- else -%}
{%- if message['role'] == 'user' -%}
{{-'GPT4 User: ' + message['content'] + '<|end_of_turn|>'-}}
{%- else -%}
{{-'GPT4 Assistant: ' + message['content'] + '<|end_of_turn|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{-'GPT4 Assistant:'-}}
{%- endif -%}
Assistant
Based on the provided example, here's the equivalent Python code for your given YAML:
python
class GPT4PromptStyle(AbstractPromptStyle):
def __init__(self, default_system_prompt: str | None = None) -> None:
default_system_prompt = default_system_prompt or self._DEFAULT_SYSTEM_PROMPT
super().__init__(default_system_prompt)
self.system_prompt: str = default_system_prompt
def _messages_to_prompt(self, messages: Sequence[ChatMessage]) -> str:
messages = list(messages)
if messages[0].role != MessageRole.SYSTEM:
logger.info(
"Adding system_prompt='%s' to the given messages as there are none given in the session",
self.system_prompt,
)
messages = [
ChatMessage(content=self.system_prompt, role=MessageRole.SYSTEM),
*messages,
]
return self._format_messages_to_prompt(messages)
def _format_messages_to_prompt(self, messages: Sequence[ChatMessage]) -> str:
output = []
system_found = False
for message in messages:
if message.role == MessageRole.SYSTEM:
system_found = True
if not system_found and message.role == MessageRole.SYSTEM:
output.append("\n\n")
elif message.role == MessageRole.SYSTEM:
output.append(f"\n\n{message.content}\n\n")
elif message.role == MessageRole.USER:
output.append(f"GPT4 User: {message.content}<|end_of_turn|>\n")
else:
output.append(f"GPT4 Assistant: {message.content}<|end_of_turn|>\n")
return "".join(output)
def _completion_to_prompt(self, completion: str) -> str:
return f"\n\n{self.system_prompt.strip()}\n\n<|end_of_turn|>\nGPT4 User: {completion.strip()}<|end_of_turn|>\nGPT4 Assistant:"
Note: This code assumes that the AbstractPromptStyle, ChatMessage, and MessageRole classes are already defined in your codebase, as they are not provided in the example. You may need to modify the code to match your specific implementation.