Skip to content

Commit

Permalink
Merge pull request #639 from canyongbs/feature/ADVAPP-446-ai-assistant
Browse files Browse the repository at this point in the history
[ADVAPP-446] Introduce ability to upload HTML, Markdown, Doc/Docx, JSON, PDF, PowerPoint and Text files to the Enterprise AI Assistant (part 1 - Assistants)
  • Loading branch information
Orrison authored Apr 4, 2024
2 parents 0d66780 + 801f6f2 commit be2a99b
Show file tree
Hide file tree
Showing 29 changed files with 1,136 additions and 160 deletions.
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ DEMO_INTERNAL_USER_EMAILS=
# TODO: Determine from Product whether or not this would be per tenant
AZURE_OPEN_AI_BASE_ENDPOINT=
AZURE_OPEN_AI_API_KEY=
AZURE_OPEN_AI_PERSONAL_ASSISTANT_API_VERSION="2023-05-15"

# Personal Assistant
AZURE_OPEN_AI_PERSONAL_ASSISTANT_API_VERSION=2024-03-01-preview
AZURE_OPEN_AI_REPORT_ASSISTANT_API_VERSION=2024-02-15-preview

# Report Assistant
AZURE_OPEN_AI_PERSONAL_ASSISTANT_DEPLOYMENT_NAME=
AZURE_OPEN_AI_REPORT_ASSISTANT_DEPLOYMENT_NAME=
AZURE_OPEN_AI_ENABLE_TEST_MODE=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class () extends Migration {
public function up(): void
{
Schema::table('assistant_chats', function (Blueprint $table) {
$table->string('assistant_id')->nullable();
$table->string('thread_id')->nullable();
});

Schema::table('assistant_chat_messages', function (Blueprint $table) {
$table->string('message_id')->nullable();
$table->string('run_id')->nullable();
$table->json('file_ids')->nullable();
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;

return new class () extends Migration {
public function up(): void
{
DB::transaction(function () {
DB::table('assistant_chat_messages')->truncate();
DB::table('assistant_chats')->truncate();
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class="w-full resize-none border-0 bg-white p-4 text-sm text-gray-900 focus:ring
</textarea>
</div>
<div class="flex items-center justify-between border-t px-3 py-2 dark:border-gray-600">
<div class="flex items-center gap-3">
<div class="flex w-full items-center gap-3">
@if (!$showCurrentResponse)
<x-filament::button
form="sendMessage,ask"
Expand All @@ -431,18 +431,29 @@ class="w-full resize-none border-0 bg-white p-4 text-sm text-gray-900 focus:ring
</x-filament::button>

{{ $this->insertFromPromptLibraryAction }}

{{-- {{ $this->uploadFilesAction }} --}}
@endif

<div
class="py-2"
wire:loading
wire:target="sendMessage"
>
<x-filament::loading-indicator class="h-5 w-5 text-primary-500" />
</div>

@error('message')
<p class="ml-auto text-xs text-red-500">{{ $message }}</p>
@enderror

@if ($this->files)
@foreach ($this->files as $file)
<div class="text-xs text-gray-600 dark:text-gray-200">
{{ $file['name'] }}
</div>
@endforeach
@endif
</div>

@if (!$showCurrentResponse && !$chat->id && $chat->messages->count() > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,60 +34,46 @@
</COPYRIGHT>
*/

use App\Models\User;

use function Pest\Laravel\{actingAs};

use AdvisingApp\IntegrationAI\Events\AIPromptInitiated;
use AdvisingApp\IntegrationAI\Client\Contracts\AIChatClient;
use AdvisingApp\Assistant\Services\AIInterface\Enums\AIChatMessageFrom;
use AdvisingApp\Assistant\Services\AIInterface\DataTransferObjects\Chat;
use AdvisingApp\Assistant\Services\AIInterface\DataTransferObjects\ChatMessage;

it('will return a streamed response of strings when prompted', function () {
$user = User::factory()->create();

actingAs($user);

$chat = new Chat(
id: null,
messages: ChatMessage::collection([]),
);

$chat->messages[] = new ChatMessage(
message: 'Hello',
from: AIChatMessageFrom::User,
);

$client = app(AIChatClient::class);

$client->ask($chat, function ($response) {
expect($response)->toBeString();
});
});

it('will dispatch an event when a prompt is initiated', function () {
Event::fake([AIPromptInitiated::class]);

$user = User::factory()->create();

actingAs($user);

$chat = new Chat(
id: null,
messages: ChatMessage::collection([]),
);

$chat->messages[] = new ChatMessage(
message: 'Hello',
from: AIChatMessageFrom::User,
);

$client = app(AIChatClient::class);

$client->ask($chat, function ($response) {
expect($response)->toBeString();
});

Event::assertDispatched(AIPromptInitiated::class);
});
namespace AdvisingApp\Assistant\Actions;

use OpenAI\Client;
use Spatie\Multitenancy\Models\Tenant;
use AdvisingApp\IntegrationAI\Settings\AISettings;
use AdvisingApp\IntegrationAI\Client\Contracts\AiChatClient;

class CreateAiAssistant
{
public function __construct(
private AiChatClient $ai
) {}

public function create()
{
/** @var Client $client */
$client = $this->ai->client;

$tenant = Tenant::current();

$settings = resolve(AISettings::class);

/** @var AssistantResponse $response */
$assistantResponse = $client->assistants()->create([
'name' => "{$tenant->name} AI Assistant",
'description' => "An AI Assistant for {$tenant->name}",
'instructions' => $settings->prompt_system_context,
'model' => config('services.azure_open_ai.personal_assistant_deployment_name'),
// Re-enable retrieval support once it's available via the API
// 'tools' => [
// ['type' => 'retrieval'],
// ],
'metadata' => [
'last_updated_at' => now(),
],
]);

$settings->assistant_id = $assistantResponse->id;
$settings->save();

return $settings->assistant_id;
}
}
47 changes: 47 additions & 0 deletions app-modules/assistant/src/Actions/GetAiAssistant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

namespace AdvisingApp\Assistant\Actions;

use AdvisingApp\IntegrationAI\Settings\AISettings;

class GetAiAssistant
{
public function get(): string
{
return resolve(AISettings::class)->assistant_id ?? resolve(CreateAiAssistant::class)->create();
}
}
52 changes: 52 additions & 0 deletions app-modules/assistant/src/Actions/GetAiAssistantFromID.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
<COPYRIGHT>
Copyright © 2016-2024, Canyon GBS LLC. All rights reserved.
Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.
Notice:
- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.
For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].
</COPYRIGHT>
*/

namespace AdvisingApp\Assistant\Actions;

use OpenAI\Responses\Assistants\AssistantResponse;
use AdvisingApp\IntegrationAI\Client\Contracts\AiChatClient;

class GetAiAssistantFromID
{
public function __construct(
private AiChatClient $ai
) {}

public function get(string $id): AssistantResponse
{
return $this->ai->client->assistants()->retrieve($id);
}
}
Loading

0 comments on commit be2a99b

Please sign in to comment.