-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from LlmLaraHub/chatUI
Chat UI
- Loading branch information
Showing
58 changed files
with
5,244 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Domains\Messages; | ||
|
||
enum RoleEnum: string | ||
{ | ||
case User = 'user'; | ||
case System = 'system'; | ||
case Assistant = 'assistant'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace App\Domains\Messages; | ||
|
||
use App\LlmDriver\LlmDriverFacade; | ||
use App\LlmDriver\Responses\CompletionResponse; | ||
use App\LlmDriver\Responses\EmbeddingsResponseDto; | ||
use App\Models\Chat; | ||
use App\Models\DocumentChunk; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class SearchOrSummarizeChatRepo | ||
{ | ||
public function search(Chat $chat, string $input): string | ||
{ | ||
/** | ||
* @TODO | ||
* Later using the LLM we will decide if the input is best served | ||
* by searching the data or a summary of the data. | ||
* For now we will search. | ||
*/ | ||
Log::info('ChatController:chat getting embedding', ['input' => $input]); | ||
|
||
/** @var EmbeddingsResponseDto $embedding */ | ||
$embedding = LlmDriverFacade::driver( | ||
$chat->chatable->getDriver() | ||
)->embedData($input); | ||
|
||
$results = DocumentChunk::query() | ||
->join('documents', 'documents.id', '=', 'document_chunks.document_id') | ||
->selectRaw( | ||
'document_chunks.embedding <-> ? as distance, document_chunks.content, document_chunks.embedding as embedding, document_chunks.id as id', | ||
[$embedding->embedding] | ||
) | ||
->where('documents.collection_id', $chat->chatable->id) | ||
->limit(5) | ||
->orderByRaw('distance') | ||
->get(); | ||
|
||
$content = []; | ||
|
||
foreach ($results as $result) { | ||
$content[] = reduce_text_size($result->content); | ||
} | ||
|
||
$content = implode(' ', $content); | ||
|
||
$content = 'This is data from the search results when entering the users prompt please use this for context and only this: '.$content; | ||
|
||
$chat->addInput( | ||
message: $content, | ||
role: RoleEnum::Assistant, | ||
systemPrompt: $chat->chatable->systemPrompt(), | ||
show_in_thread: false | ||
); | ||
|
||
$chat->addInput($input, RoleEnum::User, $chat->chatable->systemPrompt()); | ||
|
||
$latestMessagesArray = $chat->getChatResponse(); | ||
|
||
/** @var CompletionResponse $response */ | ||
$response = LlmDriverFacade::driver( | ||
$chat->chatable->getDriver() | ||
)->chat($latestMessagesArray); | ||
|
||
$chat->addInput($response->content, RoleEnum::Assistant); | ||
|
||
return $response->content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Chat; | ||
use App\Models\Collection; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ChatUpdatedEvent implements ShouldBroadcast | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct(public Collection $collection, public Chat $chat) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('collection.chat.'.$this->collection->id.'.'.$this->chat->id), | ||
]; | ||
} | ||
|
||
/** | ||
* The event's broadcast name. | ||
*/ | ||
public function broadcastAs(): string | ||
{ | ||
return 'status'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class MessageResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'from_ai' => $this->from_ai, | ||
'initials' => ($this->from_ai) ? 'Ai' : 'You', | ||
'type' => 'text', //@TODO | ||
'body' => $this->body, | ||
'diff_for_humans' => $this->created_at->diffForHumans(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class UserResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Requests; | ||
|
||
use Spatie\LaravelData\Data; | ||
|
||
class MessageInDto extends Data | ||
{ | ||
public function __construct( | ||
public string $content, | ||
public string $role, | ||
) { | ||
} | ||
} |
Oops, something went wrong.