Skip to content

Commit

Permalink
this fixes a timeout issue with ollama and a repeat user input when s…
Browse files Browse the repository at this point in the history
…kipping functions. Lastly while I was in there I set reverse on the messages to help with ollama need to qa with othres.
  • Loading branch information
alnutile committed Apr 13, 2024
1 parent 0588b8b commit 04b296c
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 36 deletions.
37 changes: 37 additions & 0 deletions app/Console/Commands/ClearAllHorizonQueues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ClearAllHorizonQueues extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:clear-all-horizon-queues';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear them all!';

/**
* Execute the console command.
*/
public function handle()
{
$queues = config('horizon.defaults');
foreach ($queues as $keyName => $queue) {
$names = data_get($queue, 'queue', []);
foreach ($names as $name) {
$this->info("Clearing queue: $name");
$this->call('horizon:clear', [$name]);
}
}
}
}
6 changes: 2 additions & 4 deletions app/Domains/Messages/SearchOrSummarizeChatRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,15 @@ public function search(Chat $chat, string $input): string

$content = implode(' ', $content);

$content = 'This is data from the search results when entering the users prompt please use this for context and only this and return as markdown so I can render it: '.$content;
$content = "This is data from the search results when entering the users prompt which is ### START PROMPT ### {$input} ### END PROMPT ### please use this with the following context and only this and return as markdown so I can render it: ".$content;

$chat->addInput(
message: $content,
role: RoleEnum::Assistant,
role: RoleEnum::User,
systemPrompt: $chat->chatable->systemPrompt(),
show_in_thread: false
);

$chat->addInput($input, RoleEnum::User, $chat->chatable->systemPrompt());

$latestMessagesArray = $chat->getChatResponse();

/** @var CompletionResponse $response */
Expand Down
13 changes: 13 additions & 0 deletions app/Events/CollectionStatusEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public function broadcastOn(): array
];
}

/**
* Get the data to broadcast.
*
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return [
'id' => $this->collection->id,
'status' => $this->status->value,
];
}

/**
* The event's broadcast name.
*/
Expand Down
37 changes: 36 additions & 1 deletion app/Http/Controllers/ReindexCollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,42 @@

namespace App\Http\Controllers;

use App\Domains\Collections\CollectionStatusEnum;
use App\Domains\Documents\StatusEnum;
use App\Events\CollectionStatusEvent;
use App\Jobs\SummarizeDataJob;
use App\Jobs\SummarizeDocumentJob;
use App\Jobs\VectorlizeDataJob;
use App\Models\Collection;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;

class ReindexCollectionController extends Controller
{
//
public function reindex(Collection $collection)
{
foreach ($collection->documents as $document) {
$chunks = [];

foreach ($document->document_chunks as $chunk) {
$chunk->status_embeddings = StatusEnum::Pending;
$chunks[] = [
new VectorlizeDataJob($chunk),
new SummarizeDataJob($chunk),
];

CollectionStatusEvent::dispatch($collection, CollectionStatusEnum::PROCESSING);

}

Bus::batch($chunks)
->name("Reindexing - {$document->id}")
->finally(function (Batch $batch) use ($document) {
SummarizeDocumentJob::dispatch($document);
})
->allowFailures()
->dispatch();
}

}
}
20 changes: 0 additions & 20 deletions app/Jobs/ProcessFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,13 @@ public function __construct(public Document $document)
*/
public function handle(): void
{
/**
* @TODO
* Make a test. I am keeping this simple to just run the batch
* but if it does more needs a test
*/
//This will trigger a batch job chain that will do the following for all the document pages
// it will save this on a Collections Watchers so we can see it in the UI.
//1) Chunk up the data
//2) vectorize the data
//3) Summarize the data
//4) Tag the data
$document = $this->document;

$batch = Bus::batch([
new ParsePdfFileJob($this->document),
//new VectorizeDataJob($this->document),
//new SummarizeDataJob($this->document),
//new TagDataJob($this->document),
//then mark it all as done and notify the ui
])
->name('Process PDF Document - '.$document->id)
->finally(function (Batch $batch) use ($document) {
/**
* @TODO
* make a job that does that and also
* closes up the batch on the run watcher
*/
DocumentProcessingCompleteJob::dispatch($document);
})
->allowFailures()
Expand Down
28 changes: 23 additions & 5 deletions app/Jobs/VectorlizeDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ class VectorlizeDataJob implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 25;

/**
* The maximum number of unhandled exceptions to allow before failing.
*
* @var int
*/
public $maxExceptions = 3;

/**
* Create a new job instance.
*/
Expand All @@ -28,13 +42,17 @@ public function __construct(public DocumentChunk $documentChunk)

public function middleware(): array
{
if (! LlmDriverFacade::driver($this->documentChunk->getDriver())->isAsync()) {
return [];
$defaults = [];

if (LlmDriverFacade::driver($this->documentChunk->getDriver())->isAsync()) {
return $defaults;
}

return [(
new WithoutOverlapping($this->documentChunk->getDriver())
)];
return [
(new WithoutOverlapping($this->documentChunk->getDriver()))
->releaseAfter(30)
->expireAfter(600),
];
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/LlmDriver/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ protected function getConfig(string $driver): array
return config("llmdriver.drivers.$driver");
}

public function isAsync(): bool
{
return true;
}

public function getFunctions(): array
{
$functions = LlmDriverFacade::getFunctions();
Expand Down
4 changes: 3 additions & 1 deletion app/LlmDriver/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ protected function getClient()

return Http::withHeaders([
'content-type' => 'application/json',
])->baseUrl($baseUrl);
])
->timeout(120)
->baseUrl($baseUrl);
}

public function getFunctions(): array
Expand Down
7 changes: 5 additions & 2 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public function getAiResponse($input)

public function getChatResponse(int $limit = 5): array
{
$latestMessages = $this->messages()->latest()->limit($limit)->get()->sortBy('id');
$latestMessages = $this->messages()
->latest()
->limit(5)
->get();

$latestMessagesArray = [];

Expand All @@ -118,7 +121,7 @@ public function getChatResponse(int $limit = 5): array
]);
}

return $latestMessagesArray;
return array_reverse($latestMessagesArray);

}

Expand Down
18 changes: 18 additions & 0 deletions database/factories/DocumentChunkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ public function openAi(): Factory
];
});
}

public function ollama(): Factory
{

return $this->state(function (array $attributes) {
$collection = Collection::factory()->create([
'driver' => DriversEnum::Ollama,
'embedding_driver' => DriversEnum::Ollama,
]);
$document = Document::factory()->create([
'collection_id' => $collection->id,
]);

return [
'document_id' => $document->id,
];
});
}
}
12 changes: 12 additions & 0 deletions resources/js/Pages/Collection/Components/ReindexAllDocuments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ const form = useForm({});
const submit = () => {
toast.info("Running reindexing jobs...")
emit('reindexed');
form.post(route('collections.reindex', {
collection: props.collection.id
}), {
preserveScroll: true,
onSuccess: () => {
toast.success("Reindexing jobs have been started.");
},
onError: () => {
toast.error("Failed to start reindexing jobs.");
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Collection/Edit.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<TransitionRoot as="template" :show="open">
<Dialog as="div" class="relative z-10" @close="open = false">
<Dialog as="div" class="relative z-50" @close="open = false">
<div class="fixed inset-0" />

<div class="fixed inset-0 overflow-hidden">
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Collection/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const reset = () => {
<summary class="m-1 btn border-none">
<EllipsisVerticalIcon class="h-5 w-5"/>
</summary>
<ul class="p-2 shadow menu dropdown-content z-[50] w-52">
<ul class="p-2 shadow menu dropdown-content z-[49] w-52">
<li>
<button type="button" class="btn-link"
@click="showEditCollectionSlideOut">Edit</button>
Expand Down
9 changes: 9 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Http\Controllers\CollectionController;
use App\Http\Controllers\ExampleChatBotController;
use App\Http\Controllers\ExampleController;
use App\Http\Controllers\ReindexCollectionController;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Expand All @@ -22,11 +23,19 @@
config('jetstream.auth_session'),
'verified',
])->group(function () {

Route::get('/dashboard', function () {
return to_route('collections.index');
//return Inertia::render('Dashboard');
})->name('dashboard');

Route::controller(ReindexCollectionController::class)->group(
function () {
Route::post('/collections/{collection}/reindex', 'reindex')
->name('collections.reindex');
}
);

Route::controller(ExampleChatBotController::class)->group(function () {
Route::get('/examples/chatbot', 'show')->name('example.chatbot.show');
Route::put('/examples/chat', 'chat')->name('example.chatbot.chat');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace Tests\Feature\Http\Controllers;

use App\Events\CollectionStatusEvent;
use App\Models\Collection;
use App\Models\Document;
use App\Models\DocumentChunk;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ReindexCollectionControllerTest extends TestCase
Expand All @@ -11,6 +18,23 @@ class ReindexCollectionControllerTest extends TestCase
*/
public function test_reindex(): void
{
$this->markTestSkipped('@TODO had to get back to work!');
Bus::fake();
Event::fake();
Storage::fake('collections');
$user = $this->createUserWithCurrentTeam();
$this->actingAs($user);
$collection = Collection::factory()->create([
'team_id' => $user->currentTeam->id,
]);
Document::factory()
->has(DocumentChunk::factory(), 'document_chunks')->create([
'collection_id' => $collection->id,
]);
/**
* @TODO Policy in place by now
*/
$response = $this->post(route('collections.reindex', $collection));

Event::assertDispatched(CollectionStatusEvent::class);
}
}
19 changes: 19 additions & 0 deletions tests/Feature/Jobs/VectorlizeDataJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,23 @@ public function test_gets_data(): void

$this->assertNotEmpty($documentChunk->embedding_3072);
}

public function test_middleware()
{
$documentChunk = DocumentChunk::factory()
->ollama()
->create();

$job = new VectorlizeDataJob($documentChunk);

$this->assertNotEmpty($job->middleware());

$documentChunk = DocumentChunk::factory()
->openAi()
->create();

$job = new VectorlizeDataJob($documentChunk);

$this->assertEmpty($job->middleware());
}
}
Loading

0 comments on commit 04b296c

Please sign in to comment.