-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BattleTool and full message collection instead of summary #102
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d2c0fdc
BattleTool has been implemented and we provide an option to not
KarmaKamikaze 4a31ad0
Create dummy character in battle and pray archivst is smart
KarmaKamikaze 3fe7024
Adjust battle tool description
KarmaKamikaze b136ae4
Fixed BattleTool (hopefully) and FindCharacter is better at finding the
KarmaKamikaze 04c2d96
Fix linter error
KarmaKamikaze 40fd3b1
Added LLM debug modes for narrator and archivist chains
KarmaKamikaze 10dfc68
Use lower health values for opponent characters so battles do not take
KarmaKamikaze 8f70fdf
Remove unused CharacterTypeDamageDict
KarmaKamikaze bc0a0ee
Added examples to FindCharacter instructions and let BattleTool do
KarmaKamikaze 7268974
Switch to Gpt4OmniModel for speed and moved InitializeCampaign to
KarmaKamikaze 30d9c97
FindCharacter and BattleTool fixes
KarmaKamikaze aeff689
Move scrolling js to every time page is rendered
KarmaKamikaze 6323032
Set ShouldSummarize to true again, or we go broke
KarmaKamikaze a5b3989
Added markdown removal
Sarmisuper ad3b7a0
Improved find character so it knows who the player is and who first-p…
Sarmisuper 7b7d9ea
Linter
Sarmisuper 4d383f2
Fixed bug where campaign page keeps scrolling to bottom
Sarmisuper cbc6d0f
Linter
Sarmisuper 152a59c
Moved the RemoveMarkdown function to ToolUtils
Sarmisuper 0508bae
Linter
Sarmisuper 6f42046
Added examples to Wound and Heal instructions
Sarmisuper abd1cd7
changed example to avoid degen output
Sarmisuper e4a8270
Fixed error in example
Sarmisuper 4aa9579
Made saving into a task so players can input next command without wai…
Sarmisuper 169e489
Linter
Sarmisuper 62202d0
Add progress spinner to indicate that archivist is working
KarmaKamikaze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace ChatRPG.API.Tools; | ||
|
||
public class BattleInput | ||
{ | ||
private static readonly HashSet<string> ValidChancesToHit = | ||
["high", "medium", "low", "impossible"]; | ||
|
||
private static readonly HashSet<string> ValidDamageSeverities = | ||
["harmless", "low", "medium", "high", "extraordinary"]; | ||
|
||
public CharacterInput? Participant1 { get; set; } | ||
public CharacterInput? Participant2 { get; set; } | ||
public string? Participant1HitChance { get; set; } | ||
public string? Participant2HitChance { get; set; } | ||
public string? Participant1DamageSeverity { get; set; } | ||
public string? Participant2DamageSeverity { get; set; } | ||
|
||
public bool IsValid(out List<string> validationErrors) | ||
{ | ||
validationErrors = []; | ||
|
||
if (Participant1 == null) | ||
{ | ||
validationErrors.Add("Participant1 is required."); | ||
} | ||
else if (!Participant1.IsValidForBattle(out var participant1Errors)) | ||
{ | ||
validationErrors.AddRange(participant1Errors.Select(e => $"Participant1: {e}")); | ||
} | ||
|
||
if (Participant2 == null) | ||
{ | ||
validationErrors.Add("Participant2 is required."); | ||
} | ||
else if (!Participant2.IsValidForBattle(out var participant2Errors)) | ||
{ | ||
validationErrors.AddRange(participant2Errors.Select(e => $"Participant2: {e}")); | ||
} | ||
|
||
if (Participant1HitChance != null && !ValidChancesToHit.Contains(Participant1HitChance)) | ||
validationErrors.Add( | ||
"Participant1ChanceToHit must be one of the following: high, medium, low, impossible."); | ||
|
||
if (Participant2HitChance != null && !ValidChancesToHit.Contains(Participant2HitChance)) | ||
validationErrors.Add( | ||
"Participant2ChanceToHit must be one of the following: high, medium, low, impossible."); | ||
|
||
if (Participant1DamageSeverity != null && !ValidDamageSeverities.Contains(Participant1DamageSeverity)) | ||
validationErrors.Add( | ||
"Participant1DamageSeverity must be one of the following: harmless, low, medium, high, extraordinary."); | ||
|
||
if (Participant2DamageSeverity != null && !ValidDamageSeverities.Contains(Participant2DamageSeverity)) | ||
validationErrors.Add( | ||
"Participant2DamageSeverity must be one of the following: harmless, low, medium, high, extraordinary."); | ||
|
||
return validationErrors.Count == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using ChatRPG.Data.Models; | ||
using LangChain.Chains.StackableChains.Agents.Tools; | ||
|
||
namespace ChatRPG.API.Tools; | ||
|
||
public class BattleTool( | ||
IConfiguration configuration, | ||
Campaign campaign, | ||
ToolUtilities utilities, | ||
string name, | ||
string? description = null) : AgentTool(name, description) | ||
{ | ||
private static readonly JsonSerializerOptions JsonOptions = new() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
private static readonly Dictionary<string, double> HitChance = new() | ||
{ | ||
{ "high", 0.9 }, | ||
{ "medium", 0.5 }, | ||
{ "low", 0.3 }, | ||
{ "impossible", 0.01 } | ||
}; | ||
|
||
private static readonly Dictionary<string, (int, int)> DamageRanges = new() | ||
{ | ||
{ "harmless", (0, 1) }, | ||
{ "low", (5, 10) }, | ||
{ "medium", (10, 20) }, | ||
{ "high", (15, 25) }, | ||
{ "extraordinary", (25, 80) } | ||
}; | ||
|
||
public override async Task<string> ToolTask(string input, CancellationToken token = new CancellationToken()) | ||
{ | ||
try | ||
{ | ||
var battleInput = JsonSerializer.Deserialize<BattleInput>(ToolUtilities.RemoveMarkdown(input), JsonOptions) ?? | ||
throw new JsonException("Failed to deserialize"); | ||
var instruction = configuration.GetSection("SystemPrompts").GetValue<string>("BattleInstruction")!; | ||
|
||
if (!battleInput.IsValid(out var errors)) | ||
{ | ||
var errorMessage = new StringBuilder(); | ||
errorMessage.Append( | ||
"Invalid input provided for the battle. Please provide valid input and correct the following errors:\n"); | ||
foreach (var validationError in errors) | ||
{ | ||
errorMessage.Append(validationError + "\n"); | ||
} | ||
|
||
return errorMessage.ToString(); | ||
} | ||
|
||
var participant1 = await utilities.FindCharacter(campaign, | ||
$"{{\"name\": \"{battleInput.Participant1!.Name}\", " + | ||
$"\"description\": \"{battleInput.Participant1.Description}\"}}", instruction); | ||
var participant2 = await utilities.FindCharacter(campaign, | ||
$"{{\"name\": \"{battleInput.Participant2!.Name}\", " + | ||
$"\"description\": \"{battleInput.Participant2.Description}\"}}", instruction); | ||
|
||
// Create dummy characters if they do not exist and pray that the archive chain will update them | ||
participant1 ??= new Character(campaign, campaign.Player.Environment, CharacterType.Humanoid, | ||
battleInput.Participant1.Name!, battleInput.Participant1.Description!, false); | ||
participant2 ??= new Character(campaign, campaign.Player.Environment, CharacterType.Humanoid, | ||
battleInput.Participant2.Name!, battleInput.Participant2.Description!, false); | ||
|
||
var firstHitter = DetermineFirstHitter(participant1, participant2); | ||
|
||
Character secondHitter; | ||
string firstHitChance; | ||
string secondHitChance; | ||
string firstHitSeverity; | ||
string secondHitSeverity; | ||
|
||
if (firstHitter == participant1) | ||
{ | ||
secondHitter = participant2; | ||
firstHitChance = battleInput.Participant1HitChance!; | ||
secondHitChance = battleInput.Participant2HitChance!; | ||
firstHitSeverity = battleInput.Participant1DamageSeverity!; | ||
secondHitSeverity = battleInput.Participant2DamageSeverity!; | ||
} | ||
else | ||
{ | ||
secondHitter = participant1; | ||
firstHitChance = battleInput.Participant2HitChance!; | ||
secondHitChance = battleInput.Participant1HitChance!; | ||
firstHitSeverity = battleInput.Participant2DamageSeverity!; | ||
secondHitSeverity = battleInput.Participant1DamageSeverity!; | ||
} | ||
|
||
return ResolveCombat(firstHitter, secondHitter, firstHitChance, secondHitChance, firstHitSeverity, | ||
secondHitSeverity) + $" {firstHitter.Name} and {secondHitter.Name}'s battle has " + | ||
"been resolved and this pair can not be used for the battle tool again."; | ||
} | ||
catch (Exception) | ||
{ | ||
return | ||
"Could not execute the battle. Tool input format was invalid. " + | ||
"Please provide the input in valid JSON."; | ||
} | ||
} | ||
|
||
private static string ResolveCombat(Character firstHitter, Character secondHitter, string firstHitChance, | ||
string secondHitChance, string firstHitSeverity, string secondHitSeverity) | ||
{ | ||
var resultString = $"{firstHitter.Name} described as \"{firstHitter.Description}\" fights " + | ||
$"{secondHitter.Name} described as \"{secondHitter.Description}\"\n"; | ||
|
||
|
||
resultString += ResolveAttack(firstHitter, secondHitter, firstHitChance, firstHitSeverity); | ||
if (secondHitter.CurrentHealth <= 0) | ||
{ | ||
return resultString; | ||
} | ||
|
||
resultString += " " + ResolveAttack(secondHitter, firstHitter, secondHitChance, secondHitSeverity); | ||
|
||
return resultString; | ||
} | ||
|
||
private static string ResolveAttack(Character damageDealer, Character damageTaker, string hitChance, | ||
string hitSeverity) | ||
{ | ||
var resultString = string.Empty; | ||
Random rand = new Random(); | ||
var doesAttackHit = rand.NextDouble() <= HitChance[hitChance]; | ||
|
||
if (doesAttackHit) | ||
{ | ||
var (minDamage, maxDamage) = DamageRanges[hitSeverity]; | ||
var damage = rand.Next(minDamage, maxDamage); | ||
resultString += $"{damageDealer.Name} deals {damage} damage to {damageTaker.Name}. "; | ||
if (damageTaker.AdjustHealth(-damage)) | ||
{ | ||
if (damageTaker.IsPlayer) | ||
{ | ||
return resultString + $"The player {damageTaker.Name} has no remaining health points. " + | ||
"Their adventure is over. No more actions can be taken."; | ||
} | ||
|
||
return resultString + | ||
$"With no health points remaining, {damageTaker.Name} dies and can no longer " + | ||
"perform actions in the narrative."; | ||
} | ||
|
||
return resultString + | ||
$"They have {damageTaker.CurrentHealth} health points out of {damageTaker.MaxHealth} remaining."; | ||
} | ||
|
||
return $"{damageDealer.Name} misses their attack on {damageTaker.Name}."; | ||
} | ||
|
||
private static Character DetermineFirstHitter(Character participant1, Character participant2) | ||
{ | ||
var rand = new Random(); | ||
var firstHitRoll = rand.NextDouble(); | ||
|
||
return (participant1.Type - participant2.Type) switch | ||
{ | ||
0 => firstHitRoll <= 0.5 ? participant1 : participant2, | ||
1 => firstHitRoll <= 0.4 ? participant1 : participant2, | ||
2 => firstHitRoll <= 0.3 ? participant1 : participant2, | ||
>= 3 => firstHitRoll <= 0.2 ? participant1 : participant2, | ||
-1 => firstHitRoll <= 0.6 ? participant1 : participant2, | ||
-2 => firstHitRoll <= 0.7 ? participant1 : participant2, | ||
<= -3 => firstHitRoll <= 0.8 ? participant1 : participant2 | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pump Up Those Numbers!!!