com.dingtalk.baymax.framework.sdk.mercury.util.BaseMessageUtils Maven / Gradle / Ivy
package com.dingtalk.baymax.framework.sdk.mercury.util;
import com.dingtalk.baymax.framework.sdk.mercury.domain.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author xiaoyan.wjw
* @Date 2023-05-22 15:56:31
*/
public class BaseMessageUtils {
public static String getBufferString(List messages, String humanPrefix, String aiPrefix) {
List stringMessages = new ArrayList<>();
for (BaseMessage m : messages) {
String role;
if (m instanceof HumanMessage) {
role = humanPrefix;
} else if (m instanceof AIMessage) {
role = aiPrefix;
} else if (m instanceof SystemMessage) {
role = "System";
} else if (m instanceof ChatMessage) {
role = ((ChatMessage) m).getRole();
} else {
throw new IllegalArgumentException("Got unsupported message type: " + m);
}
stringMessages.add(role + ": " + m.getContent());
}
return String.join("\n", stringMessages);
}
}