Commit 4846a19f by Jonathan Thomas

Improve chat prompt to include behaviors: <FOLLOW> <FRIENDSHIP> <UNFOLLOW>,…

Improve chat prompt to include behaviors: <FOLLOW> <FRIENDSHIP> <UNFOLLOW>, etc... Added code to detect separate these behaviors from the cleaned message. Also, better detection of character sheets (making the - optional).
parent e303e736
Pipeline #11887 passed with stage
in 20 seconds
package com.owlmaddie;
/**
* The {@code Behavior} class represents a single behavior with an optional integer argument.
* This class is used to model behaviors extracted from a parsed message, where each
* behavior might have an associated argument that further defines the behavior.
*
* For example: "<FOLLOW>", "<FRIENDSHIP 3>", "<UNFOLLOW>"
*/
public class Behavior {
private String name;
private Integer argument;
public Behavior(String name, Integer argument) {
this.name = name;
this.argument = argument;
}
// Getters
public String getName() {
return name;
}
public Integer getArgument() {
return argument;
}
@Override
public String toString() {
if (argument != null) {
return name + ": " + argument;
} else {
return name;
}
}
}
......@@ -99,7 +99,7 @@ public class ChatDataManager {
public String getCharacterProp(String propertyName) {
// Create a case-insensitive regex pattern to match the property name and capture its value
Pattern pattern = Pattern.compile("-\\s*" + Pattern.quote(propertyName) + ":\\s*(.+)", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile("-?\\s*" + Pattern.quote(propertyName) + ":\\s*(.+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(characterSheet);
if (matcher.find()) {
......@@ -196,8 +196,19 @@ public class ChatDataManager {
this.addMessage(shortGreeting.replace("\n", " "), ChatSender.ASSISTANT);
} else if (output_message != null && systemPrompt == "system-chat") {
// Parse message for behaviors
ParsedMessage result = MessageParser.parseMessage(output_message.replace("\n", " "));
// Apply behaviors (if any)
for (Behavior behavior : result.getBehaviors()) {
LOGGER.info("Behavior: " + behavior.getName());
if (behavior.getArgument() != null) {
LOGGER.info("Argument: " + behavior.getArgument());
}
}
// Add ASSISTANT message
this.addMessage(output_message.replace("\n", " "), ChatSender.ASSISTANT);
this.addMessage(result.getCleanedMessage(), ChatSender.ASSISTANT);
}
});
......
package com.owlmaddie;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;
/**
* The {@code MessageParser} class parses out behaviors that are included in messages, and outputs
* a {@code ParsedMessage} result, which separates the cleaned message and the included behaviors.
*/
public class MessageParser {
public static ParsedMessage parseMessage(String input) {
StringBuilder cleanedMessage = new StringBuilder();
List<Behavior> behaviors = new ArrayList<>();
Pattern pattern = Pattern.compile("<(\\w+)(?:\\s+(-?\\d+))?>");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
// Extract and store behaviors
String behaviorName = matcher.group(1);
Integer argument = null;
if (matcher.group(2) != null) {
argument = Integer.valueOf(matcher.group(2));
}
behaviors.add(new Behavior(behaviorName, argument));
// Remove the matched pattern from the original string
matcher.appendReplacement(cleanedMessage, "");
}
matcher.appendTail(cleanedMessage);
// Create and return the ParseResult object
return new ParsedMessage(cleanedMessage.toString().trim(), behaviors);
}
}
package com.owlmaddie;
import java.util.List;
/**
* The {@code ParsedMessage} class represents a list of behaviors and a cleaned message.
*/
public class ParsedMessage {
private String cleanedMessage;
private List<Behavior> behaviors;
public ParsedMessage(String cleanedMessage, List<Behavior> behaviors) {
this.cleanedMessage = cleanedMessage;
this.behaviors = behaviors;
}
// Getters
public String getCleanedMessage() {
return cleanedMessage;
}
public List<Behavior> getBehaviors() {
return behaviors;
}
}
You are a RPG dungeon master, crafting a new character sheet in the following format. Be very creative with each new
character. These characters will be inhabiting and participating in an epic fantasy adventure which takes place in
Minecraft. Please limit traits to a few choices and keep them short and concise. Please follow the character sheet
format below, and ONLY output this character sheet without any intro text. Please use the existing attributes
(if any are filled out below).
format below (including - dashes), and ONLY output this character sheet without any intro text.
Please use existing attributes (if any are filled out below, otherwise generate new values).
Please name each character including a short initial greeting (as spoken by
the character using their personality traits) which introduces their unique quest and furthers the story. Use creative
and original names, and NEVER base names solely on their entity type. For example, avoid childish names such as
Bessie the Cow, Cluck the Chicken, Shelly the Turtle, and instead lean toward fantasy names.
Please name each character including a short initial greeting (as spoken by the character using their personality
traits). Use creative and original names, and NEVER base names solely on their entity type. For example, avoid childish
names such as Bessie the Cow, Cluck the Chicken, Shelly the Turtle, and instead lean towards fantasy names found in
more traditional RPG adventures.
Please FOLLOW this template format exactly:
- Name: {{entity_name}}
- Type: {{entity_type}}
......
......@@ -21,4 +21,15 @@ World Info:
- Current Time: {{world_time}} (24 hour format)
- Weather: Raining: {{world_is_raining}}, Thundering: {{world_is_thundering}}
- Moon Phase: {{world_moon_phase}}
- Difficulty: {{world_difficulty}}, Hard Core: {{world_is_hardcore}}
\ No newline at end of file
- Difficulty: {{world_difficulty}}, Hard Core: {{world_is_hardcore}}
Behaviors:
If needed, include one or more of these behaviors at the end of the message. These behaviors allow
the entity to interact with the player and world, so it's important to include them if they are requested. For example, if
a player asks the entity to follow them, output <FOLLOW> at the end of the message. If the player improves the friendship
with an entity, output the new friendship value: <FRIENDSHIP value>. Include as many as needed (one per line).
- <FRIENDSHIP 0> Friendship starts as neutral (0 value). The range of friendship values is -3 to 3. If the player gains (or loses) your trust & friendship, output a new friendship value with this behavior. Make the player work hard for a perfect friendship value.
- <FOLLOW> Follow the player's movements as they navigate the world.
- <UNFOLLOW> Stop following the player's movements.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment