Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ContentFormatter escape special characters for message content #10319

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions libs/langchain/langchain/chat_models/azureml_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@

class LlamaContentFormatter(ContentFormatterBase):
"""Content formatter for `LLaMA`."""


SUPPORTED_ROLES: List[str] = ["user", "assistant", "system"]

@staticmethod
def _convert_message_to_dict(message: BaseMessage) -> Dict:
"""Converts message to a dict according to role"""
if isinstance(message, HumanMessage):
return {"role": "user", "content": message.content}
if isinstance(message, HumanMessage):
return {"role": "user", "content": ContentFormatterBase.escape_special_characters(message.content)}
elif isinstance(message, AIMessage):
return {"role": "assistant", "content": message.content}
return {"role": "assistant", "content": ContentFormatterBase.escape_special_characters(message.content)}
elif isinstance(message, SystemMessage):
return {"role": "system", "content": message.content}
return {"role": "system", "content": ContentFormatterBase.escape_special_characters(message.content)}
elif (
isinstance(message, ChatMessage)
and message.role in LlamaContentFormatter.SUPPORTED_ROLES
):
return {"role": message.role, "content": message.content}
return {"role": message.role, "content": ContentFormatterBase.escape_special_characters(message.content)}
else:
supported = ",".join(
[role for role in LlamaContentFormatter.SUPPORTED_ROLES]
Expand All @@ -46,7 +47,7 @@ def _convert_message_to_dict(message: BaseMessage) -> Dict:
def _format_request_payload(
self, messages: List[BaseMessage], model_kwargs: Dict
) -> bytes:
chat_messages = [
chat_messages = [
LlamaContentFormatter._convert_message_to_dict(message)
for message in messages
]
Expand Down