Skip to content

Commit

Permalink
webhook working
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 23, 2024
1 parent 5ffd955 commit b859925
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
11 changes: 2 additions & 9 deletions app/Domains/Sources/WebhookSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace App\Domains\Sources;

use App\Domains\Chat\MetaDataDto;
use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\PromptMerge;
use App\Helpers\ChatHelperTrait;
use App\Helpers\TextChunker;
use App\Jobs\DocumentProcessingCompleteJob;
use App\Jobs\SummarizeDocumentJob;
use App\Jobs\VectorlizeDataJob;
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Source;
use App\Models\SourceTask;
use Illuminate\Bus\Batch;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Bus;
Expand All @@ -25,7 +21,6 @@

class WebhookSource extends BaseSource
{

public SourceTypeEnum $sourceTypeEnum = SourceTypeEnum::WebhookSource;

protected array $payload = [];
Expand Down Expand Up @@ -57,7 +52,7 @@ public function handle(Source $source): void
$payloadMd5 = md5(json_encode($this->payload, 128));
$key = md5($payloadMd5.$this->source->id);

if($this->skip($this->source, $key)) {
if ($this->skip($this->source, $key)) {
return;
}

Expand Down Expand Up @@ -85,9 +80,7 @@ public function handle(Source $source): void

$promptResultsOriginal = $results->content;

$chat = $source->chat;

$this->addUserMessage($chat, $promptResultsOriginal);
$this->addUserMessage($source, $promptResultsOriginal);

$promptResults = $this->arrifyPromptResults($promptResultsOriginal);

Expand Down
16 changes: 9 additions & 7 deletions app/Helpers/ChatHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public function ifNotActionRequired(string $results): bool

public function skip(Source $source, string $key): bool
{
if(! $source->force &&
if (! $source->force &&
SourceTask::where('source_id', $source->id)->where('task_key', $key)->exists()) {
Log::info('[LaraChain] GetWebContentJob - Skipping - already ran');

return true;
} else {
return false;
Expand All @@ -69,25 +70,26 @@ public function skip(Source $source, string $key): bool
public function createSourceTask(Source $source, string $key): SourceTask
{
return SourceTask::create([
'source_id' => $this->source->id,
'source_id' => $source->id,
'task_key' => $key,
]);
}

public function addUserMessage(Chat $chat, string $message): void
public function addUserMessage(Source $source, string $message): void
{
$chat->addInput(
$source->refresh()->getChat()->addInput(
message: $message,
role: RoleEnum::User,
show_in_thread: true,
meta_data: MetaDataDto::from([
'driver' => $this->source->getDriver(),
'source' => $this->source->title,
'driver' => $source->getDriver(),
'source' => $source->title,
]),
);
}

public function arrifyPromptResults(string $original) : array {
public function arrifyPromptResults(string $original): array
{
$promptResults = json_decode($original, true);

if (is_null($promptResults)) {
Expand Down
10 changes: 3 additions & 7 deletions app/Jobs/GetWebContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Source;
use App\Models\SourceTask;
use Facades\App\Domains\Sources\WebSearch\GetPage;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Batchable;
Expand All @@ -22,7 +21,6 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
Expand Down Expand Up @@ -59,7 +57,7 @@ public function handle(): void

$key = md5($this->webResponseDto->url.$this->source->id);

if($this->skip($this->source, $key)) {
if ($this->skip($this->source, $key)) {
return;
}

Expand Down Expand Up @@ -91,9 +89,7 @@ public function handle(): void
} else {
$promptResultsOriginal = $results->content;

$chat = $this->source->chat;

$this->addUserMessage($chat, $promptResultsOriginal);
$this->addUserMessage($this->source, $promptResultsOriginal);

$promptResults = $this->arrifyPromptResults($promptResultsOriginal);

Expand Down Expand Up @@ -182,7 +178,7 @@ public function handle(): void
* I could move this into the loop if it is not
* enough here
*/
$assistantMessage = $chat->addInput(
$assistantMessage = $this->source->getChat()->addInput(
message: json_encode($promptResults),
role: RoleEnum::Assistant,
show_in_thread: true,
Expand Down
6 changes: 1 addition & 5 deletions app/Models/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public function getChatable(): HasDrivers

public function getChat(): ?Chat
{
/**
* @TODO
* I need to come back to this
*/
return $this->collection->chats()->first();
return $this->chat ?: $this->collection->chats()->first();
}

public function getSummary(): string
Expand Down

0 comments on commit b859925

Please sign in to comment.