Skip to content

Commit

Permalink
add NoArgsConstructor in Message
Browse files Browse the repository at this point in the history
add NoArgsConstructor so that object can be deserialized from their json format str, other case may also need a NoArgsConstructor ( this is needed when load message from database).
  • Loading branch information
zhangxiaojiawow committed Jul 20, 2023
1 parent 959d271 commit 3a267e8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package com.hw.langchain.schema;

import lombok.NoArgsConstructor;

/**
* Type of message that is spoken by the AI.
* @author HamaWhite
*/
@NoArgsConstructor
public class AIMessage extends BaseMessage {

public AIMessage(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

package com.hw.langchain.schema;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.hw.langchain.chains.query.constructor.JsonUtils;
import com.hw.langchain.exception.LangChainException;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -46,4 +51,26 @@ protected BaseMessage(String content) {
* Type of the message, used for serialization.
*/
public abstract String type();

public Map<String, Object> toMap() {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
objectMapper.registerModule(module);
Map<String, Object> map = objectMapper.convertValue(this, new TypeReference<>() {});
return Map.of("type", type(), "data", map);
}

public static BaseMessage fromMap(Map<String, Object> message) {
String type = (String) message.get("type");
Object data = message.get("data");
String jsonStr = JsonUtils.toJsonStringWithIndent(data, 0);
return switch (type) {
case "ai" -> JsonUtils.convertFromJsonStr(jsonStr, AIMessage.class);
case "human" -> JsonUtils.convertFromJsonStr(jsonStr, HumanMessage.class);
case "system" -> JsonUtils.convertFromJsonStr(jsonStr, SystemMessage.class);
case "chat" -> JsonUtils.convertFromJsonStr(jsonStr, ChatMessage.class);
case "function" -> JsonUtils.convertFromJsonStr(jsonStr, FunctionMessage.class);
default -> throw new LangChainException(String.format("Got unexpected message type:%s", type));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package com.hw.langchain.schema;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Type of message with arbitrary speaker.
*
* @author HamaWhite
*/
@Data
@NoArgsConstructor
public class ChatMessage extends BaseMessage {

private String role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package com.hw.langchain.schema;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author HamaWhite
*/
@Data
@NoArgsConstructor
public class FunctionMessage extends BaseMessage {

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package com.hw.langchain.schema;

import lombok.NoArgsConstructor;

/**
* Type of message that is spoken by the human.
* @author HamaWhite
*/
@NoArgsConstructor
public class HumanMessage extends BaseMessage {

public HumanMessage(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

package com.hw.langchain.schema;

import lombok.NoArgsConstructor;

/**
* Type of message that is a system message.
* @author HamaWhite
*/
@NoArgsConstructor
public class SystemMessage extends BaseMessage {

public SystemMessage(String content) {
Expand Down

0 comments on commit 3a267e8

Please sign in to comment.