mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
30 lines
906 B
Python
30 lines
906 B
Python
|
from typing import List
|
||
|
|
||
|
from langchain_core.messages import (
|
||
|
AIMessage,
|
||
|
BaseMessage,
|
||
|
ChatMessage,
|
||
|
HumanMessage,
|
||
|
SystemMessage,
|
||
|
)
|
||
|
|
||
|
|
||
|
def _convert_one_message_to_text_llama(message: BaseMessage) -> str:
|
||
|
if isinstance(message, ChatMessage):
|
||
|
message_text = f"\n\n{message.role.capitalize()}: {message.content}"
|
||
|
elif isinstance(message, HumanMessage):
|
||
|
message_text = f"[INST] {message.content} [/INST]"
|
||
|
elif isinstance(message, AIMessage):
|
||
|
message_text = f"{message.content}"
|
||
|
elif isinstance(message, SystemMessage):
|
||
|
message_text = f"<<SYS>> {message.content} <</SYS>>"
|
||
|
else:
|
||
|
raise ValueError(f"Got unknown type {message}")
|
||
|
return message_text
|
||
|
|
||
|
|
||
|
def convert_messages_to_prompt_llama(messages: List[BaseMessage]) -> str:
|
||
|
return "\n".join(
|
||
|
[_convert_one_message_to_text_llama(message) for message in messages]
|
||
|
)
|