Skip to content

Commit

Permalink
CyberBuddy - Backport prompts and wire Botman (PoC)
Browse files Browse the repository at this point in the history
  • Loading branch information
csavelief committed Sep 24, 2024
1 parent 5b634a2 commit 9e595d0
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/Modules/CyberBuddy/Helpers/ApiUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Modules\CyberBuddy\Helpers;

use App\Modules\CyberBuddy\Exceptions\ApiException;
use App\Modules\CyberBuddy\Models\Prompt;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
Expand Down Expand Up @@ -49,6 +50,14 @@ public function delete_chunks(array $uids, string $collectionName): array
return $this->json($response);
}

/** @deprecated */
public function ask_chunks_demo(string $question): array
{
/** @var Prompt $prompt */
$prompt = Prompt::where('name', 'default_debugger')->firstOrfail();
return $this->ask_chunks($question, 'test-local-3', $prompt->template, true, true, 'fr');
}

public function ask_chunks(string $question, string $collectionName, string $prompt, bool $rerankings = true, bool $showContext = true, string $lang = 'en', int $maxDocsUsed = 5): array
{
$response = $this->post('/ask_chunks', [
Expand Down Expand Up @@ -115,6 +124,21 @@ public function capsule(array $facts, string $title, string $prompt): array
return $this->json($response);
}

public function chat_manual(string $question, string $collectionName, string $historyKey, string $prompt, string $historyPrompt, int $maxDocsUsed = 5, string $lang = 'en'): array
{
$response = $this->post('/chat_manual', [
'question' => $question,
'collection_name' => $collectionName,
'history_key' => $historyKey,
'prompt' => $prompt,
'history_prompt' => $historyPrompt,
'max_docs_used' => $maxDocsUsed,
'lang' => $lang,
'show_context' => true
]);
return $this->json($response);
}

private function get($endpoint, $json): ResponseInterface
{
$url = Config::get('towerify.cyberbuddy.api') . $endpoint;
Expand Down
35 changes: 35 additions & 0 deletions app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use App\Models\YnhServer;
use App\Modules\AdversaryMeter\Http\Controllers\Controller;
use App\Modules\CyberBuddy\Helpers\ApiUtilsFacade as ApiUtils;
use App\User;
use BotMan\BotMan\BotMan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class CyberBuddyController extends Controller
{
Expand Down Expand Up @@ -99,6 +101,39 @@ public function handle(): void
");
}
});
$botman->hears('/debug {question}', function (BotMan $botman, string $question) {

$botman->types();
$response = ApiUtils::ask_chunks_demo($question);
if ($response['error']) {
$botman->reply('Une erreur s\'est produite. Veuillez reposer votre question ultérieurement.');
} else {

$answer = $response['response'];
$context = collect($response['context'] ?? []);
$matches = array();
$isOk = preg_match_all("/\[\[\d+]]/", $answer, $matches);

if (!$isOk) {
$botman->reply($answer);
} else {
/** @var array $refs */
$refs = $matches[0];
foreach ($refs as $ref) {
$id = Str::replace(['[', ']'], '', $ref);
$tooltip = $context->filter(fn($ctx) => $ctx['id'] === $id)->first();
if ($tooltip) {
$answer = Str::replace($ref, "
<div class=\"tooltip\" style=\"color:#f8b500\">[{$id}]
<span class=\"tooltiptext tooltip-right\">{$tooltip['text']}</span>
</div>
", $answer);
}
}
$botman->reply($answer);
}
}
});
$botman->fallback(function (BotMan $botman) {
$botman->reply('Désolé, je n\'ai pas compris votre commande.');
});
Expand Down
26 changes: 26 additions & 0 deletions app/Modules/CyberBuddy/Models/Prompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Modules\CyberBuddy\Models;

use App\Modules\CyberBuddy\Traits\HasTenant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property int id
* @property string name
* @property string template
* @property int created_by
*/
class Prompt extends Model
{
use HasFactory, HasTenant;

protected $table = 'cb_prompts';

protected $fillable = [
'name',
'template',
'created_by',
];
}
10 changes: 10 additions & 0 deletions app/Modules/CyberBuddy/Observers/PromptObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Modules\CyberBuddy\Observers;

use App\Modules\CyberBuddy\Traits\IsTenantAware;

class PromptObserver
{
use IsTenantAware;
}
5 changes: 4 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@
use App\Modules\AdversaryMeter\Observers\HiddenAlertObserver;
use App\Modules\AdversaryMeter\Observers\HoneypotObserver;
use App\Modules\CyberBuddy\Models\Chunk;
use App\Modules\CyberBuddy\Models\ChunkTag;
use App\Modules\CyberBuddy\Models\ChunkCollection;
use App\Modules\CyberBuddy\Models\ChunkTag;
use App\Modules\CyberBuddy\Models\Prompt;
use App\Modules\CyberBuddy\Observers\ChunkObserver;
use App\Modules\CyberBuddy\Observers\ChunkTagObserver;
use App\Modules\CyberBuddy\Observers\CollectionObserver;
use App\Modules\CyberBuddy\Observers\PromptObserver;
use App\Observers\AddressObserver;
use App\Observers\AdjustmentObserver;
use App\Observers\BillpayerObserver;
Expand Down Expand Up @@ -222,6 +224,7 @@ public function boot()
Chunk::observe(ChunkObserver::class);
ChunkTag::observe(ChunkTagObserver::class);
ChunkCollection::observe(CollectionObserver::class);
Prompt::observe(PromptObserver::class);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions database/migrations/2024_09_24_212100_create_prompts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePromptsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cb_prompts', function (Blueprint $table) {

$table->id();
$table->timestamps();

// Scope collections
$table->intOrBigIntBasedOnRelated('created_by', Schema::connection(null), 'users.id')->cascadeOnDelete();
$table->foreign('created_by')->references('id')->on('users')->cascadeOnDelete();

// The collection properties
$table->string('name')->unique();
$table->string('template', 5000);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cb_prompts');
}
}

0 comments on commit 9e595d0

Please sign in to comment.