Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADVAPP-361]: Extend user model with job title, [ADVAPP-360]: Enhance AI to be dynamically aware of the user creating prompts #552

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app-modules/assistant/src/Filament/Pages/PersonalAssistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
use AdvisingApp\Assistant\Enums\AssistantChatShareWith;
use AdvisingApp\IntegrationAI\Client\Contracts\AIChatClient;
use AdvisingApp\IntegrationAI\Exceptions\ContentFilterException;
use AdvisingApp\IntegrationAI\DataTransferObjects\DynamicContext;
use AdvisingApp\IntegrationAI\Exceptions\TokensExceededException;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use AdvisingApp\Assistant\Services\AIInterface\Enums\AIChatMessageFrom;
Expand Down Expand Up @@ -220,9 +221,14 @@ public function sendMessage(): void
public function ask(AIChatClient $ai): void
{
try {
$this->currentResponse = $ai->ask($this->chat, function (string $partial) {
$this->stream('currentResponse', nl2br($partial));
});
/** @var User $user */
$user = auth()->user();

$this->currentResponse = $ai
->provideDynamicContext(new DynamicContext($user))
->ask($this->chat, function (string $partial) {
$this->stream('currentResponse', nl2br($partial));
});
Orrison marked this conversation as resolved.
Show resolved Hide resolved
} catch (ContentFilterException|TokensExceededException $e) {
$this->renderError = true;
$this->error = $e->getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function ask(Chat $chat, ?Closure $callback): string

public function provideDynamicContext(DynamicContext $context): self
{
$this->setDynamicContext($context->context);
$this->setDynamicContext($context->getContext());

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@
namespace AdvisingApp\IntegrationAI\DataTransferObjects;

use Spatie\LaravelData\Data;
use AdvisingApp\IntegrationAi\Models\Concerns\ProvidesDynamicContext;
use AdvisingApp\IntegrationAI\Models\Concerns\ProvidesDynamicContext;

class DynamicContext extends Data
{
public ProvidesDynamicContext $record;
private ?string $context;

public ?string $context;
public function __construct(
protected ProvidesDynamicContext $record
) {
$this->context = $this->record->getDynamicContext();
}

public function __construct()
public function getContext(): ?string
{
$this->context = $this->record->getDynamicContext();
return $this->context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</COPYRIGHT>
*/

namespace AdvisingApp\IntegrationAi\Models\Concerns;
namespace AdvisingApp\IntegrationAI\Models\Concerns;

use Illuminate\Database\Eloquent\Model;

Expand Down
4 changes: 4 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public static function form(Form $form): Form
->email()
->required()
->maxLength(255),
TextInput::make('job_title')
->string()
->maxLength(255),
Toggle::make('is_external')
->label('User can only log in via a social provider.'),
TextInput::make('created_at')
Expand Down Expand Up @@ -122,6 +125,7 @@ public static function table(Table $table): Table
TextColumn::make('name'),
TextColumn::make('email')
->label('Email address'),
TextColumn::make('job_title'),
TextColumn::make('created_at')
->label('Created At')
->dateTime(config('project.datetime_format') ?? 'Y-m-d H:i:s')
Expand Down
15 changes: 14 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@
use AdvisingApp\Notification\Models\Contracts\NotifiableInterface;
use AdvisingApp\ServiceManagement\Models\ServiceRequestAssignment;
use AdvisingApp\Engagement\Models\Concerns\HasManyEngagementBatches;
use AdvisingApp\IntegrationAI\Models\Concerns\ProvidesDynamicContext;
use AdvisingApp\ServiceManagement\Enums\ServiceRequestAssignmentStatus;

/**
* @mixin IdeHelperUser
*/
class User extends Authenticatable implements HasLocalePreference, FilamentUser, Auditable, HasMedia, HasAvatar, NotifiableInterface, HasFilamentResource
class User extends Authenticatable implements HasLocalePreference, FilamentUser, Auditable, HasMedia, HasAvatar, NotifiableInterface, HasFilamentResource, ProvidesDynamicContext
{
use DefinesPermissions;
use HasFactory;
Expand Down Expand Up @@ -169,6 +170,7 @@ class User extends Authenticatable implements HasLocalePreference, FilamentUser,
'working_hours_are_enabled',
'are_working_hours_visible_on_profile',
'working_hours',
'job_title',
];

public $orderable = [
Expand Down Expand Up @@ -498,6 +500,17 @@ public function getApiPermissions(): Collection
return collect(['view-email', ...$this->apiPermissions()]);
}

public function getDynamicContext(): string
{
$context = "My name is {$this->name}";

if ($this->job_title) {
$context .= " and I am a {$this->job_title}";
}

return "{$context}. When you respond please use this information about me to tailor your response.";
}

protected function serializeDate(DateTimeInterface $date): string
{
return $date->format(config('project.datetime_format') ?? 'Y-m-d H:i:s');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function up(): void
$table->boolean('working_hours_are_enabled')->default(false);
$table->boolean('are_working_hours_visible_on_profile')->default(false);
$table->jsonb('working_hours')->nullable();
$table->string('job_title')->nullable();

$table->foreignUuid('pronouns_id')->nullable()->constrained('pronouns')->nullOnDelete();
$table->boolean('are_pronouns_visible_on_profile')->default(false);
Expand Down