From 5161e6740fa3edd6046f6dcabf646bf23476061a Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Sat, 11 May 2024 09:29:23 -0400 Subject: [PATCH 1/2] update to search it was lacking --- app/Domains/Prompts/SearchPrompt.php | 36 ++++----- .../Prompts/SummarizeDocumentPrompt.php | 2 +- app/Domains/Prompts/SummarizePrompt.php | 2 +- app/Domains/Prompts/VerificationPrompt.php | 2 +- app/Domains/Sources/WebSearchSource.php | 11 +-- app/Http/Controllers/WebSourceController.php | 29 ++++++- app/Http/Resources/SourceResource.php | 2 + app/Jobs/GetWebContentJob.php | 18 ++--- resources/js/Pages/Sources/Index.vue | 19 +++-- resources/js/Pages/Sources/WebSource/Edit.vue | 80 +++++++++++++++++++ routes/web.php | 4 + .../Controllers/WebSourceControllerTest.php | 29 ++++++- 12 files changed, 185 insertions(+), 49 deletions(-) create mode 100644 resources/js/Pages/Sources/WebSource/Edit.vue diff --git a/app/Domains/Prompts/SearchPrompt.php b/app/Domains/Prompts/SearchPrompt.php index 66158c9d..542f92fb 100644 --- a/app/Domains/Prompts/SearchPrompt.php +++ b/app/Domains/Prompts/SearchPrompt.php @@ -9,28 +9,26 @@ class SearchPrompt public static function prompt(string $originalPrompt): string { + Log::info('[LaraChain] - SearchPrompt'); - Log::info("[LaraChain] - SearchPrompt"); return <<getDriver()) - ->completion($prompt); - - $search = $response->content; - Log::info('[LaraChain] Starting web search ', [ 'content reworked' => $search, ]); @@ -69,7 +60,7 @@ public function handle(Source $source): void notify_collection_ui( collection: $source->collection, status: CollectionStatusEnum::PENDING, - message: "Search complete getting results from each page" + message: 'Search complete getting results from each page' ); } catch (\Exception $e) { diff --git a/app/Http/Controllers/WebSourceController.php b/app/Http/Controllers/WebSourceController.php index e37763d0..50a72baf 100644 --- a/app/Http/Controllers/WebSourceController.php +++ b/app/Http/Controllers/WebSourceController.php @@ -38,11 +38,36 @@ public function store(Collection $collection) 'details' => $validated['details'], 'collection_id' => $collection->id, 'type' => SourceTypeEnum::WebSearchSource, - 'meta_data' => [], + 'meta_data' => [ + 'driver' => 'brave', + 'limit' => 5 + ], ]); - request()->session()->flash('flas.banner', 'Web source added successfully'); + request()->session()->flash('flash.banner', 'Web source added successfully'); return to_route('collections.sources.index', $collection); } + + public function edit(Collection $collection, Source $source) { + + return inertia('Sources/WebSource/Edit', [ + 'source' => $source, + 'collection' => new CollectionResource($source->collection), + ]); + } + + public function update(Collection $collection, Source $source) { + + $validated = request()->validate([ + 'title' => 'required|string', + 'details' => 'required|string', + ]); + + $source->update($validated); + + request()->session()->flash('flash.banner', "Updated"); + + return back(); + } } diff --git a/app/Http/Resources/SourceResource.php b/app/Http/Resources/SourceResource.php index bac82c77..8fc149ec 100644 --- a/app/Http/Resources/SourceResource.php +++ b/app/Http/Resources/SourceResource.php @@ -17,6 +17,8 @@ public function toArray(Request $request): array return [ 'id' => $this->id, 'title' => $this->title, + 'collection_id' => $this->collection_id, + 'details' => $this->details, 'description' => $this->description, 'type' => str($this->type->name)->headline()->toString(), ]; diff --git a/app/Jobs/GetWebContentJob.php b/app/Jobs/GetWebContentJob.php index 7c596469..adc36853 100644 --- a/app/Jobs/GetWebContentJob.php +++ b/app/Jobs/GetWebContentJob.php @@ -64,23 +64,23 @@ public function handle(): void ] ); - Log::info("[Larachain] GetWebContentJob - {$this->source->title} - URL: {$this->webResponseDto->url}"); + Log::info("[LaraChain] GetWebContentJob - {$this->source->title} - URL: {$this->webResponseDto->url}"); $html = GetPage::make($this->source->collection)->handle($this->webResponseDto->url); + /** + * @NOTE + * making them PDF for now + * I ran into "noise" issues + * of just a lot of script tags and stuff + * there is some code in the getPage for html + * that might be worth it later + */ if (! Feature::active('html_to_text')) { $document->update([ 'type' => TypesEnum::PDF, 'file_path' => md5($this->webResponseDto->url).'.pdf', ]); - /** - * @NOTE - * making them PDF for now - * I ran into "noise" issues - * of just a lot of script tags and stuff - * there is some code in the getPage for html - * that might be worth it later - */ Bus::batch([ new ParsePdfFileJob($document), ]) diff --git a/resources/js/Pages/Sources/Index.vue b/resources/js/Pages/Sources/Index.vue index b837f8bb..9e139249 100644 --- a/resources/js/Pages/Sources/Index.vue +++ b/resources/js/Pages/Sources/Index.vue @@ -65,8 +65,11 @@ const run = (source) => {
-
- No sources yet. +
+ + + +
No sources yet. Choose one below
@@ -74,9 +77,15 @@ const run = (source) => {
Type: {{ source.type }}
+
+ Details: {{ source.details }} +
- + Edit
@@ -89,7 +98,7 @@ const run = (source) => { Web Search @@ -102,7 +111,7 @@ const run = (source) => {
- + diff --git a/resources/js/Pages/Sources/WebSource/Edit.vue b/resources/js/Pages/Sources/WebSource/Edit.vue new file mode 100644 index 00000000..ee0fe1d1 --- /dev/null +++ b/resources/js/Pages/Sources/WebSource/Edit.vue @@ -0,0 +1,80 @@ + + + diff --git a/routes/web.php b/routes/web.php index abdc4b24..712853f9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -53,6 +53,10 @@ function () { ->name('collections.sources.websearch.create'); Route::post('/collections/{collection}/sources/websearch', 'store') ->name('collections.sources.websearch.store'); + Route::get('/collections/{collection}/sources/websearch/{source}/edit', 'edit') + ->name('collections.sources.websearch.edit'); + Route::put('/collections/{collection}/sources/websearch/{source}/update', 'update') + ->name('collections.sources.websearch.update'); } ); diff --git a/tests/Feature/Http/Controllers/WebSourceControllerTest.php b/tests/Feature/Http/Controllers/WebSourceControllerTest.php index 80ddbeab..8f659275 100644 --- a/tests/Feature/Http/Controllers/WebSourceControllerTest.php +++ b/tests/Feature/Http/Controllers/WebSourceControllerTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Controllers; use App\Models\Collection; +use App\Models\Source; use App\Models\User; use Tests\TestCase; @@ -21,7 +22,33 @@ public function test_store(): void 'title' => 'Test Title', 'details' => 'Test Details', ]); - $response->assertSessionHas('flas.banner', 'Web source added successfully'); + $response->assertSessionHas('flash.banner', 'Web source added successfully'); $this->assertDatabaseCount('sources', 1); + + $source = Source::first(); + + $this->assertNotEmpty($source->meta_data); + $this->assertEquals('brave', $source->meta_data['driver']); + } + + public function test_update() { + $source = Source::factory()->create(); + + $user = User::factory()->create(); + + $this->actingAs($user) + ->put(route('collections.sources.websearch.update', + [ + 'collection' => $source->collection->id, + 'source' => $source->id + ] + ), [ + 'title' => 'Test Title2', + 'details' => 'Test Details2', + ]) + ->assertSessionHasNoErrors() + ->assertStatus(302); + + $this->assertEquals($source->refresh()->details, 'Test Details2'); } } From 0d7981d753cbb16c238a8c287015ad57d852f1d5 Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Sat, 11 May 2024 09:31:18 -0400 Subject: [PATCH 2/2] fix some search related go distracted --- app/Domains/Sources/WebSearchSource.php | 1 - app/Http/Controllers/WebSourceController.php | 10 ++++++---- .../Http/Controllers/WebSourceControllerTest.php | 5 +++-- tests/Feature/WebSearchSourceTest.php | 14 ++------------ 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/app/Domains/Sources/WebSearchSource.php b/app/Domains/Sources/WebSearchSource.php index ed265f2a..90742e1e 100644 --- a/app/Domains/Sources/WebSearchSource.php +++ b/app/Domains/Sources/WebSearchSource.php @@ -3,7 +3,6 @@ namespace App\Domains\Sources; use App\Domains\Collections\CollectionStatusEnum; -use App\Domains\Prompts\SearchPrompt; use App\Domains\Sources\WebSearch\Response\SearchResponseDto; use App\Domains\Sources\WebSearch\WebSearchFacade; use App\Jobs\GetWebContentJob; diff --git a/app/Http/Controllers/WebSourceController.php b/app/Http/Controllers/WebSourceController.php index 50a72baf..56efba6f 100644 --- a/app/Http/Controllers/WebSourceController.php +++ b/app/Http/Controllers/WebSourceController.php @@ -40,7 +40,7 @@ public function store(Collection $collection) 'type' => SourceTypeEnum::WebSearchSource, 'meta_data' => [ 'driver' => 'brave', - 'limit' => 5 + 'limit' => 5, ], ]); @@ -49,7 +49,8 @@ public function store(Collection $collection) return to_route('collections.sources.index', $collection); } - public function edit(Collection $collection, Source $source) { + public function edit(Collection $collection, Source $source) + { return inertia('Sources/WebSource/Edit', [ 'source' => $source, @@ -57,7 +58,8 @@ public function edit(Collection $collection, Source $source) { ]); } - public function update(Collection $collection, Source $source) { + public function update(Collection $collection, Source $source) + { $validated = request()->validate([ 'title' => 'required|string', @@ -66,7 +68,7 @@ public function update(Collection $collection, Source $source) { $source->update($validated); - request()->session()->flash('flash.banner', "Updated"); + request()->session()->flash('flash.banner', 'Updated'); return back(); } diff --git a/tests/Feature/Http/Controllers/WebSourceControllerTest.php b/tests/Feature/Http/Controllers/WebSourceControllerTest.php index 8f659275..6379fbc2 100644 --- a/tests/Feature/Http/Controllers/WebSourceControllerTest.php +++ b/tests/Feature/Http/Controllers/WebSourceControllerTest.php @@ -31,7 +31,8 @@ public function test_store(): void $this->assertEquals('brave', $source->meta_data['driver']); } - public function test_update() { + public function test_update() + { $source = Source::factory()->create(); $user = User::factory()->create(); @@ -40,7 +41,7 @@ public function test_update() { ->put(route('collections.sources.websearch.update', [ 'collection' => $source->collection->id, - 'source' => $source->id + 'source' => $source->id, ] ), [ 'title' => 'Test Title2', diff --git a/tests/Feature/WebSearchSourceTest.php b/tests/Feature/WebSearchSourceTest.php index 4d442347..48eebe22 100644 --- a/tests/Feature/WebSearchSourceTest.php +++ b/tests/Feature/WebSearchSourceTest.php @@ -6,8 +6,6 @@ use App\Domains\Sources\WebSearch\Response\SearchResponseDto; use App\Domains\Sources\WebSearch\WebSearchFacade; use Illuminate\Support\Facades\Bus; -use LlmLaraHub\LlmDriver\LlmDriverFacade; -use LlmLaraHub\LlmDriver\Responses\CompletionResponse; use Tests\TestCase; class WebSearchSourceTest extends TestCase @@ -19,14 +17,6 @@ public function test_searches(): void { Bus::fake(); - LlmDriverFacade::shouldReceive('driver->completion') - ->once()->andReturn(CompletionResponse::from([ - 'content' => 'updated query', - ])); - - LlmDriverFacade::shouldReceive('driver->onQueue') - ->once()->andReturn('ollama'); - WebSearchFacade::shouldReceive('driver->search') ->once() ->andReturn( @@ -39,9 +29,9 @@ public function test_searches(): void 'type' => SourceTypeEnum::WebSearchSource, ]); - $websource = new \App\Domains\Sources\WebSearchSource(); + $web_source = new \App\Domains\Sources\WebSearchSource(); - $websource->handle($source); + $web_source->handle($source); Bus::assertBatchCount(1); }