Skip to content

Commit

Permalink
Merge pull request #15 from LlmLaraHub/checkbox_ui
Browse files Browse the repository at this point in the history
Search Fix
  • Loading branch information
alnutile authored May 11, 2024
2 parents 09e05d5 + 0d7981d commit 9ddb7d4
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 62 deletions.
36 changes: 17 additions & 19 deletions app/Domains/Prompts/SearchPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ class SearchPrompt
public static function prompt(string $originalPrompt): string
{

Log::info('[LaraChain] - SearchPrompt');

Log::info("[LaraChain] - SearchPrompt");
return <<<PROMPT
The user is asking to search the web but I want you to review the query and clean it keeping it as
a string.
### Context, Action, Result, Example (C.A.R.E.)
**Context**: The system processes various web search queries that require specific refinements to ensure that the search results are relevant and focused. These refinements include emphasizing key terms.
**Action**: Modify the original search query by emphasizing keywords relevant to the query's intent applying the format adjustments as required.
**Result**: The refined search queries effectively filter highlight pertinent information, leading to cleaner and more relevant search results.
**Example**:
- **in**: Search the web for PHP news and Laravel news
**return**: "php news OR laravel news"
- **in**: current data on the llm industry
**return**: "llm industry news OR llm industry updates"
- **in**: latest news on the laravel framework
**return**: "laravel framework news OR laravel framework updates"
### USERS SEARCH QUERY TO TRANSFORM
**in**: $originalPrompt
**return**: [Your transformed search string here on return the string between these brackets]
### END USERS SEARCH QUERY TO TRANSFORM
Here are some examples of how I want you to return the data:
### RETURN FORMAT
in: Search the web for PHP news and Laravle news
out: php news OR laravel news -filetype:pdf -intitle:pdf
in: current data on the llm industry
out: llm industry news OR llm industry updates -filetype:pdf -intitle:pdf
in: latest news on the laravel framework
out: laravel framework news OR laravel framework updates -filetype:pdf -intitle:pdf
### END RETURN FORMAT
### START USER QUERY
$originalPrompt
### END USER QUERY
PROMPT;
}
}
2 changes: 1 addition & 1 deletion app/Domains/Prompts/SummarizeDocumentPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SummarizeDocumentPrompt
{
public static function prompt(string $documentContent): string
{
Log::info("[LaraChain] - SummarizeDocumentPrompt");
Log::info('[LaraChain] - SummarizeDocumentPrompt');

return <<<PROMPT
# **Task, Action, Goal (T.A.G)**
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Prompts/SummarizePrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SummarizePrompt
{
public static function prompt(string $originalPrompt, string $context): string
{
Log::info("[LaraChain] - SummarizePrompt");
Log::info('[LaraChain] - SummarizePrompt');

return <<<PROMPT
# **Role, Task, Format (R.T.F)**
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Prompts/VerificationPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class VerificationPrompt
{
public static function prompt(string $llmResponse, string $context): string
{
Log::info("[LaraChain] - VerificationPrompt");
Log::info('[LaraChain] - VerificationPrompt');

return <<<PROMPT
# **Role, Task, Format (R.T.F)**
Expand Down
12 changes: 1 addition & 11 deletions app/Domains/Sources/WebSearchSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,15 +23,6 @@ public function handle(Source $source): void
$limit = data_get($meta_data, 'limit', 5);
$driver = data_get($meta_data, 'driver', 'mock');

$prompt = SearchPrompt::prompt($search);

Log::info('[LaraChain] Asking LLM to optimize search query');

$response = LlmDriverFacade::driver($source->getDriver())
->completion($prompt);

$search = $response->content;

Log::info('[LaraChain] Starting web search ', [
'content reworked' => $search,
]);
Expand Down Expand Up @@ -69,7 +59,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) {
Expand Down
31 changes: 29 additions & 2 deletions app/Http/Controllers/WebSourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,38 @@ 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();
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/SourceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down
18 changes: 9 additions & 9 deletions app/Jobs/GetWebContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])
Expand Down
19 changes: 14 additions & 5 deletions resources/js/Pages/Sources/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,27 @@ const run = (source) => {
</Intro>

<div class="border border-gray-200 p-5 mt-5 flex">
<div v-if="sources.data.lenth === 0" class="text-gray-500 text-lg">
No sources yet.
<div v-if="sources.data.length === 0" class="text-center w-full">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 mx-auto text-gray-400">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 8.25H7.5a2.25 2.25 0 0 0-2.25 2.25v9a2.25 2.25 0 0 0 2.25 2.25h9a2.25 2.25 0 0 0 2.25-2.25v-9a2.25 2.25 0 0 0-2.25-2.25H15M9 12l3 3m0 0 3-3m-3 3V2.25" />
</svg>
<div class="text-xl text-gray-600">No sources yet. Choose one below</div>
</div>
<div v-else class="card rounded-none w-96 bg-base-100 shadow-xl" v-for="source in sources.data" :key="source.id">
<div class="card-body">
<h2 class="card-title text-gray-600">{{ source.title }}</h2>
<div>
Type: <span class="font-bold text-gray-600">{{ source.type }}</span>
</div>
<div>
Details: <span class="font-bold text-gray-600">{{ source.details }}</span>
</div>
<div class="card-actions justify-end">
<button @click="run(source)" type="button" class="btn btn-primary rounded-none">Run</button>
<button class="btn btn-primary rounded-none">Edit</button>
<Link :href="route('collections.sources.websearch.edit', {
collection: source.collection_id,
source: source.id
})" class="btn btn-primary rounded-none">Edit</Link>
</div>
</div>
</div>
Expand All @@ -89,7 +98,7 @@ const run = (source) => {

<Link
class="btn btn-info rounded-none"
:href="route('collections.sources.websearch.create',
:href="route('collections.sources.websearch.create',
{collection: collection.data.id}
)"
>Web Search</Link>
Expand All @@ -102,7 +111,7 @@ const run = (source) => {
</div>

</div>

</div>
</AppLayout>
</template>
80 changes: 80 additions & 0 deletions resources/js/Pages/Sources/WebSource/Edit.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<script setup>
import AppLayout from '@/Layouts/AppLayout.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import SecondaryButton from '@/Components/SecondaryButton.vue';
import { ref } from 'vue';
import Intro from '@/Components/Intro.vue';
import SecondaryLink from '@/Components/SecondaryLink.vue';
import Resources from './Components/Resources.vue';
import { useForm } from '@inertiajs/vue3';
const props = defineProps({
collection: {
type: Object,
required: true,
},
source: {
type: Object
}
});
const form = useForm({
title: props.source.title,
details: props.source.details,
});
const submit = () => {
form.put(
route('collections.sources.websearch.update', {
collection: props.collection.data.id,
source: props.source.id
}), {
preserveScroll: true,
});
}
</script>

<template>
<AppLayout title=" Edit Web Source">
<template #header>
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Edit Web Source
</h2>
</template>

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg p-5">
<Intro>
Web Search Source
<template #description>
Add a query below and you will be able to run it as a web search.
This will add documents to your collection.
</template>
</Intro>

<form @submit.prevent="submit" class="p-10 ">
<Resources
v-model="form">

</Resources>

<div class="flex justify-end items-center gap-4">
<PrimaryButton type="submit">
Save
</PrimaryButton>
<SecondaryLink :href="route('collections.sources.index', {
collection: collection.data.id
})">
Cancel
</SecondaryLink>
</div>
</form>

</div>
</div>
</div>
</AppLayout>
</template>
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
);

Expand Down
30 changes: 29 additions & 1 deletion tests/Feature/Http/Controllers/WebSourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\Http\Controllers;

use App\Models\Collection;
use App\Models\Source;
use App\Models\User;
use Tests\TestCase;

Expand All @@ -21,7 +22,34 @@ 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');
}
}
Loading

0 comments on commit 9ddb7d4

Please sign in to comment.