ChatGPT's response that worked for me:
Thank you for providing the output. It seems that the
context_str_template
looks correct with the appropriate placeholders
{context_str}
and
{{system_prompt}}
. The error is likely happening because of how the format method is used in the
_get_prefix_messages_with_context
function.
In Python, when you have double curly braces
{{}}
in a format string, it's used to escape the curly braces and treat them as literal characters in the formatted string. So,
{{system_prompt}}
becomes
{system_prompt}
in the formatted string. When you later try to format the
context_str_template
again, Python is looking for a key named
system_prompt
in the template string. This seems to be causing the issue.
You can modify the code to handle this by using the formatted string once, and replace the placeholder with the actual system prompt. Here's the modified
_get_prefix_messages_with_context
method:
def _get_prefix_messages_with_context(
self, context_str_template: str
) -> List[ChatMessage]:
"""Get the prefix messages with context"""
# ensure we grab the user-configured system prompt
system_prompt = ""
prefix_messages = self._prefix_messages
if (
len(self._prefix_messages) != 0
and self._prefix_messages[0].role == "system"
):
system_prompt = str(self._prefix_messages[0].content)
prefix_messages = self._prefix_messages[1:]
# Replace the placeholder with the actual system prompt
context_str = context_str_template.replace("{system_prompt}", system_prompt)
return [ChatMessage(content=context_str, role="system")] + prefix_messages
Try making this change and test the code again to see if the error persists.