Skip to content

Commit

Permalink
banwarden fix for less token usage and option to disable ai insight b…
Browse files Browse the repository at this point in the history
…ased on punishment types
  • Loading branch information
Xinecraft committed Jan 8, 2025
1 parent 14547db commit 3e4f41c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ MAIL_FROM_NAME="${APP_NAME}"
# BanWarden
BANWARDEN_ENABLED=true
BANWARDEN_AI_INSIGHTS_ENABLED=true
BANWARDEN_AI_INSIGHTS_TYPES=ban,mute,kick,warn
BANWARDEN_SHOW_PUBLIC=true
BANWARDEN_SHOW_MASKED_IP_PUBLIC=false
BANWARDEN_MODULE_DISK=private
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ MineTrax is a web suite & analytics tool for your minecraft servers. Using it yo
![Alt](https://repobeats.axiom.co/api/embed/0aac0565a812462007f7d4b1612c47b546e09a48.svg "Repobeats analytics image")

## Requirements
- PHP 8.1+
- MySQL 5.7+ or MariaDB 10.2.7+
- PHP 8.3+
- MySQL 8.0+ or MariaDB 10.3+
- Apache2 or Nginx
- NodeJS 12+
- NodeJS 16+
- Redis Server
- Git
- Composer
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/ApiBanWardenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public function postReportPunishment(Request $request, GeolocationService $geolo

// AI Insights
$insightEnabled = config('minetrax.banwarden.ai_insights_enabled');
if ($insightEnabled && !$punishment->insights) {
$insightTypes = config('minetrax.banwarden.ai_insights_types');
if ($insightEnabled && !$punishment->insights && in_array($punishment->type->value, $insightTypes)) {
GeneratePunishmentInsightsJob::dispatch($punishment);
}

Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/BanWardenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function show(PlayerPunishment $playerPunishment, Request $request)

// Generate insights if enabled and not already generated
$insightEnabled = config('minetrax.banwarden.ai_insights_enabled');
if ($insightEnabled && !$playerPunishment->insights) {
$insightTypes = config('minetrax.banwarden.ai_insights_types');
if ($insightEnabled && !$playerPunishment->insights && in_array($playerPunishment->type->value, $insightTypes)) {
GeneratePunishmentInsightsJob::dispatch($playerPunishment);
}

Expand Down
52 changes: 35 additions & 17 deletions app/Jobs/GeneratePunishmentInsightsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,52 @@ public function handle(AiService $aiService): void
->makeVisible('ip_address')
->loadMissing([
'country:id,name,iso_code',
'victimPlayer:id,uuid,username,skin_texture_id',
'creatorPlayer:id,uuid,username,skin_texture_id',
'removerPlayer:id,uuid,username,skin_texture_id',
'victimPlayer:id,uuid,username',
'creatorPlayer:id,uuid,username',
'removerPlayer:id,uuid,username',
]);

// Details
$this->punishment->victimPlayer->makeHidden([
'skin_texture_id',
'avatar_url',
]);
$punishmentJsonString = json_encode($this->punishment);

// Last Punishments
if ($this->punishment->uuid) {
$lastPunishments = PlayerPunishment::with([
'country:id,name,iso_code',
'victimPlayer:id,uuid,username,skin_texture_id',
'creatorPlayer:id,uuid,username,skin_texture_id',
'removerPlayer:id,uuid,username,skin_texture_id',
'victimPlayer:id,uuid,username',
'creatorPlayer:id,uuid,username',
'removerPlayer:id,uuid,username',
])
->where('uuid', $this->punishment->uuid)
->where('id', '!=', $this->punishment->id)
->orderByDesc('start_at')
->limit(10)
->limit(5)
->get()
->makeVisible('ip_address');
} else {
$lastPunishments = PlayerPunishment::with([
'country:id,name,iso_code',
'victimPlayer:id,uuid,username,skin_texture_id',
'creatorPlayer:id,uuid,username,skin_texture_id',
'removerPlayer:id,uuid,username,skin_texture_id',
'victimPlayer:id,uuid,username',
'creatorPlayer:id,uuid,username',
'removerPlayer:id,uuid,username',
])
->where('ip_address', 'LIKE', $this->punishment->ip_address)
->where('id', '!=', $this->punishment->id)
->orderByDesc('start_at')
->limit(10)
->limit(5)
->get()
->makeVisible('ip_address');
}
$lastPunishments->each(function ($punishment) {
$punishment->country?->makeHidden(['photo_path']);
$punishment->victimPlayer?->makeHidden(['skin_texture_id', 'avatar_url']);
$punishment->creatorPlayer?->makeHidden(['skin_texture_id', 'avatar_url']);
$punishment->removerPlayer?->makeHidden(['skin_texture_id', 'avatar_url']);
});
$lastPunishmentsJsonString = json_encode($lastPunishments);

// Last Sessions
Expand All @@ -94,18 +104,22 @@ public function handle(AiService $aiService): void
->where('player_uuid', $this->punishment->uuid)
->where('session_started_at', '<=', $this->punishment->start_at)
->orderByDesc('session_started_at')
->limit(10)
->limit(5)
->get()
->makeVisible('player_ip_address');
} else {
$pastSessions = MinecraftPlayerSession::with(['country:id,name,iso_code', 'server:id,name'])
->where('player_ip_address', 'LIKE', $this->punishment->ip_address)
->where('session_started_at', '<=', $this->punishment->start_at)
->orderByDesc('session_started_at')
->limit(10)
->limit(5)
->get()
->makeVisible('player_ip_address');
}
$pastSessions->each(function ($session) {
$session->country?->makeHidden(['photo_path']);
$session->makeHidden('avatar_url');
});
$lastSessionsJsonString = json_encode($pastSessions);

// Possible Alts
Expand All @@ -118,15 +132,19 @@ public function handle(AiService $aiService): void
->where('player_uuid', '!=', $this->punishment->uuid)
->where('player_ip_address', 'LIKE', $firstTwoOctets)
->pluck('player_uuid');
$altPlayers = Player::select(['id', 'uuid', 'username', 'skin_texture_id', 'first_seen_at', 'last_seen_at', 'country_id', 'ip_address', 'play_time'])
$altPlayers = Player::select(['id', 'uuid', 'username', 'first_seen_at', 'last_seen_at', 'country_id', 'ip_address', 'play_time'])
->whereIn('uuid', $altUuids)
->with('country:id,name,iso_code')
->withCount('punishments')
->orderByDesc('last_seen_at')
->limit(10)
->limit(5)
->get()
->makeVisible('ip_address');
}
$altPlayers->each(function ($player) {
$player->country?->makeHidden(['photo_path']);
$player->makeHidden('avatar_url');
});
$possibleAltsJsonString = json_encode($altPlayers);

$systemPrompt = view('gptprompts.punishment-insights', [
Expand All @@ -136,10 +154,10 @@ public function handle(AiService $aiService): void
Punishment Details:
$punishmentJsonString
Last 10 Punishments:
Last 5 Punishments:
$lastPunishmentsJsonString
Last 10 Sessions (before this punishment):
Last 5 Sessions (before this punishment):
$lastSessionsJsonString
Possible Alts (players with similar ip address):
Expand Down
3 changes: 3 additions & 0 deletions app/Services/AiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EchoLabs\Prism\Text\Generator;
use EchoLabs\Prism\Text\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class AiService
{
Expand Down Expand Up @@ -57,6 +58,8 @@ public function simplePrompt(
->usingProviderConfig($providerConfig)
->generate();

Log::info("[SimplePrompt] Prompt Tokens: {$response->usage->promptTokens} Completion Tokens: {$response->usage->completionTokens}");

return $response->text;
}

Expand Down
8 changes: 8 additions & 0 deletions config/minetrax.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
|--------------------------------------------------------------------------
|
| By default the PING uses hostname when server is bungee.
| By default the PING uses ip address when server is not bungee.
| By default the QUERY uses ip address when server is bungee.
|
| Change accordingly in .env to use hostname or ip as per requirement.
Expand Down Expand Up @@ -428,6 +429,13 @@
*/
'ai_insights_enabled' => env('BANWARDEN_AI_INSIGHTS_ENABLED', true) && env('AI_ENABLED', false),

/*
|--------------------------------------------------------------------------
| Comma separated list of punishment types to analyze with AI (ban,mute,kick,warn)
|--------------------------------------------------------------------------
*/
'ai_insights_types' => explode(',', env('BANWARDEN_AI_INSIGHTS_TYPES', 'ban,mute,kick,warn')),

/*
|--------------------------------------------------------------------------
| Disk to use to store all files of Banwarden Module.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Response SHOULD BE a valid JSON in the following format (DONT INCLUDE ANYTHING ELSE IN RESPONSE):
{"score": 50,"insights": ["Insight 1","Insight 2","Insight 3","Insight 4","Insight 5"]}

IP Ban doesn't indicate severity of infraction.
IP Ban doesn't indicate severity of infraction so don't use it to determine score.

Current Time is {{ now() }}

Expand Down

0 comments on commit 3e4f41c

Please sign in to comment.