Skip to content

Commit

Permalink
create from json again
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethKnudsen97 committed May 1, 2024
1 parent 60681c7 commit 854cee5
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions packages/langchain_core/lib/src/chat_models/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ sealed class ChatMessage {

/// Merges this message with another by concatenating the content.
ChatMessage concat(final ChatMessage other);

///Converts ChatMessage to json string
Map<String, dynamic> toJson();
}

/// {@template system_chat_message}
Expand Down Expand Up @@ -175,6 +178,15 @@ SystemChatMessage{
content: $content,
}''';
}

@override
Map<String, dynamic> toJson() => {
'type': defaultPrefix,
'content': content,
};

factory SystemChatMessage.fromJson(final Map<String, dynamic> json) =>
SystemChatMessage(content: json['content']);
}

/// {@template human_chat_message}
Expand Down Expand Up @@ -267,6 +279,18 @@ HumanChatMessage{
content: $content,
}''';
}

@override
Map<String, dynamic> toJson() => {
'type': defaultPrefix,
'content': content,
};

factory HumanChatMessage.fromJson(final Map<String, dynamic> json) {
return HumanChatMessage(
content: json['content'],
);
}
}

/// {@template ai_chat_message}
Expand Down Expand Up @@ -328,6 +352,22 @@ AIChatMessage{
functionCall: $functionCall,
}''';
}

@override
Map<String, dynamic> toJson() => {
'type': defaultPrefix,
'content': content,
'functionCall': functionCall?.toJson(),
};

factory AIChatMessage.fromJson(final Map<String, dynamic> json) {
return AIChatMessage(
content: json['content'],
functionCall: json['functionCall'] != null
? AIChatMessageFunctionCall.fromJson(json['functionCall'])
: null,
);
}
}

/// {@template ai_chat_message_function_call}
Expand Down Expand Up @@ -382,6 +422,22 @@ AIChatMessageFunctionCall{
arguments: $arguments,
}''';
}

///The toJson function will convert the ChatMessage to a json object
Map<String, dynamic> toJson() => {
'name': name,
'argumentsRaw': argumentsRaw,
'arguments': arguments,
};

///The fromJson will create a AI
factory AIChatMessageFunctionCall.fromJson(final Map<String, dynamic> json) {
return AIChatMessageFunctionCall(
name: json['name'],
argumentsRaw: json['argumentsRaw'],
arguments: json['arguments'],
);
}
}

/// {@template function_chat_message}
Expand Down Expand Up @@ -431,6 +487,16 @@ FunctionChatMessage{
content: $content,
}''';
}

@override
Map<String, dynamic> toJson() => {
'type': defaultPrefix,
'name': name,
'content': content,
};

factory FunctionChatMessage.fromJson(final Map<String, dynamic> json) =>
FunctionChatMessage(content: json['content'], name: json['name']);
}

/// {@template custom_chat_message}
Expand Down Expand Up @@ -476,6 +542,15 @@ CustomChatMessage{
role: $role,
}''';
}

@override
Map<String, dynamic> toJson() => {
'type': 'Custom',
'content': content,
'role': role,
};
factory CustomChatMessage.fromJson(final Map<String, dynamic> json) =>
CustomChatMessage(content: json['content'], role: json['role']);
}

/// Role of a chat message
Expand Down

0 comments on commit 854cee5

Please sign in to comment.