diff --git a/app/Modules/CyberBuddy/Helpers/ApiUtils.php b/app/Modules/CyberBuddy/Helpers/ApiUtils.php index 0a2dbf7..6b31f2b 100644 --- a/app/Modules/CyberBuddy/Helpers/ApiUtils.php +++ b/app/Modules/CyberBuddy/Helpers/ApiUtils.php @@ -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; @@ -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', [ @@ -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; diff --git a/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php b/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php index c962182..a2e1722 100644 --- a/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php +++ b/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php @@ -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 { @@ -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, " +
[{$id}] + {$tooltip['text']} +
+ ", $answer); + } + } + $botman->reply($answer); + } + } + }); $botman->fallback(function (BotMan $botman) { $botman->reply('Désolé, je n\'ai pas compris votre commande.'); }); diff --git a/app/Modules/CyberBuddy/Models/Prompt.php b/app/Modules/CyberBuddy/Models/Prompt.php new file mode 100644 index 0000000..62ea12a --- /dev/null +++ b/app/Modules/CyberBuddy/Models/Prompt.php @@ -0,0 +1,26 @@ +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'); + } +}