Skip to content

Commit

Permalink
add saily send
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Sep 10, 2024
1 parent a33b005 commit fe71d3f
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function setToolType(ToolTypes $toolType): self
return $this;
}

public function setSystemPrompt(string $systemPrompt = ''): self
{
$this->system = $systemPrompt;

return $this;
}

public function setLimitByShowInUi(bool $limitByShowInUi): self
{
$this->limitByShowInUi = $limitByShowInUi;
Expand Down
63 changes: 63 additions & 0 deletions app/Domains/Projects/DailyReportService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Domains\Projects;

use App\Domains\Messages\RoleEnum;
use App\Models\Task;
use App\Models\Project;
use App\Notifications\DailyReport;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use LlmLaraHub\LlmDriver\Functions\ToolTypes;
use LlmLaraHub\LlmDriver\LlmDriverFacade;

class DailyReportService
{
public function handle(): void
{
foreach (Project::active()->get() as $project) {
$this->sendReport($project);
}
}

public function sendReport(Project $project)
{
$tasks = Task::where('project_id', $project->id)
->notCompleted()
->where('due_date', '>=', now()->addDays(7))
->get()
/** @phpstan-ignore-next-line */
->transform(function (Task $item) {
return sprintf(
'Task: %s %s %s',
$item->name,
$item->details,
$item->due_date
);
})->implode(', ');

$prompt = ProjectDailyReportPrompt::getPrompt($project, $tasks);

$project->getChat()->addInput(
message: $prompt,
role: RoleEnum::User,
);

$messages = $project->getChat()->getMessageThread();

$results = LlmDriverFacade::driver($project->getDriver())
->setToolType(ToolTypes::Chat)
->chat($messages);

$project->getChat()->addInput(
message: $results->content,
role: RoleEnum::Assistant,
);

Log::info('DailyReportService::handle', [
'results' => $results->content,
]);

Notification::send($project->team->allUsers(), new DailyReport($results->content, $project));
}
}
2 changes: 1 addition & 1 deletion app/Domains/Projects/KickOffProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function handle(Project $project)

$project->tasks()->delete();

Orchestrate::handle($chat, $project->content, $project->system_prompt);
Orchestrate::handle($chat, $project->getContent(), $project->getSystemPrompt());

$chat->updateQuietly([
'chat_status' => UiStatusEnum::Complete,
Expand Down
3 changes: 3 additions & 0 deletions app/Domains/Projects/Orchestrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function handle(Chat $chat, string $prompt, string $systemPrompt = ''): v
$messages = $chat->getMessageThread();

$response = LlmDriverFacade::driver($chat->getDriver())
->setSystemPrompt($systemPrompt)
->setToolType(ToolTypes::Chat)
->chat($messages);

Expand Down Expand Up @@ -61,6 +62,8 @@ public function handle(Chat $chat, string $prompt, string $systemPrompt = ''): v
$response = LlmDriverFacade::driver(
$chat->getDriver()
)
->setToolType(ToolTypes::Chat)
->setSystemPrompt($systemPrompt)
->chat($messages);

$chat->addInput(
Expand Down
48 changes: 48 additions & 0 deletions app/Domains/Projects/ProjectDailyReportPrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Domains\Projects;

use App\Models\Project;

class ProjectDailyReportPrompt
{
public static function getPrompt(
Project $project,
?string $tasks = ''): string
{

$now = now()->toISOString();

$context = $project->content;

return <<<PROMPT
<role>
You are an Ai marketing assistant specialized in digital marketing campaigns. Below is info about the campaign and previous conversations.
<task>
Using the `<context> section below, give the user a report of tasks that are due and when
so they can see what they need to do today and or in the next day or two. They
get this report every day so they can see what they need to do today and what.
If there are not tasks then put in the Tasks sections "No tasks today or tomorrow"
<format>
TLDR: What is on the schedule
Tasks:
* Task Name and due date
* Task Name and due date
<content>
Today's date is $now
The list of tasks if any are:
$tasks
The campaign content is:
$context
PROMPT;
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/DailyReportSendController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Facades\App\Domains\Projects\DailyReportService;

class DailyReportSendController extends Controller
{
public function __invoke(Project $project)
{
DailyReportService::sendReport($project);
\request()->session()->flash('flash.banner', 'Sent!');
return back();
}
}
6 changes: 5 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public function chat(Project $project, Chat $chat)
'chat_status' => UiStatusEnum::InProgress->value,
]);

Orchestrate::handle($chat, $validated['input']);
Orchestrate::handle(
chat: $chat,
prompt: $validated['input'],
systemPrompt: $project->getSystemPrompt()
);

$chat->update([
'chat_status' => UiStatusEnum::Complete->value,
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Domains\Projects\StatusEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -43,6 +44,12 @@ public function getDriver(): string
return $this->chats()->first()->chat_driver?->value;
}

public function scopeActive(Builder $query): Builder
{
return $query->where('end_date', '>=', now())
->orWhere('end_date', null);
}

public function getEmbeddingDriver(): string
{
return $this->chats()->first()->embedding_driver->value;
Expand Down Expand Up @@ -77,4 +84,30 @@ public function getChat(): ?Chat
{
return $this->chats->first();
}

public function getContent(): string
{
$context = $this->content;
$now = now()->toISOString();

return <<<PROMPT
Current Date: $now
$context
PROMPT;
}

public function getSystemPrompt(): string
{
$context = $this->system_prompt;
$now = now()->toISOString();

return <<<PROMPT
Current Date: $now
$context
PROMPT;
}
}
58 changes: 58 additions & 0 deletions app/Notifications/DailyReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Notifications;

use App\Models\Project;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class DailyReport extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*/
public function __construct(
public string $message,
public Project $project)
{
//
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->subject('Daily Report')
->markdown('mail.daily_report', [
'message' => $this->message,
'url' => route('projects.show', $this->project),
]);
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
12 changes: 12 additions & 0 deletions database/factories/ChatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Collection;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use LlmLaraHub\LlmDriver\DriversEnum;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Chat>
Expand All @@ -25,4 +26,15 @@ public function definition(): array
'chatable_type' => Collection::class,
];
}

public function withDrivers(): Factory
{
return $this->state(function (array $attributes) {
return [
'chat_driver' => DriversEnum::Claude->value,
'embedding_driver' => DriversEnum::Claude->value,
];
});
}

}
11 changes: 11 additions & 0 deletions resources/views/mail/daily_report.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<x-mail::message>

{{ $message }}

<x-mail::button :url="$url">
View the Project
</x-mail::button>

Thanks,<br>
Your assistant!
</x-mail::message>
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ function () {
}
);

Route::post('/daily-report/{project}', \App\Http\Controllers\DailyReportSendController::class)->name('daily-report.send');


Route::controller(\App\Http\Controllers\AssistantEmailBoxSourceController::class)->group(
function () {
Route::get('/collections/{collection}/sources/email_source/create', 'create')
Expand Down
Loading

0 comments on commit fe71d3f

Please sign in to comment.