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

Source prompting #40

Merged
merged 20 commits into from
Jul 24, 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
1 change: 1 addition & 0 deletions app/Domains/Chat/MetaDataDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
public mixed $date_range = '',
public mixed $input = '',
public mixed $driver = '',
public mixed $source = '',
public mixed $reference_collection_id = '',
) {

Expand Down
1 change: 0 additions & 1 deletion app/Domains/Documents/Transformers/CSVTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function handle(Document $document): array

$filePath = $this->document->pathToFile();

//$filePath = null, string $disk = null, string $readerType = null
$collection = (new DocumentsImport())
->toCollection($filePath, null, $this->readerType);

Expand Down
12 changes: 9 additions & 3 deletions app/Domains/EmailParser/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Client
'Drafts',
];

public function handle(): void
public function handle(int $limit = 10): void
{
$mail = [];

Expand All @@ -31,7 +31,7 @@ public function handle(): void

$full_name = data_get($folder, 'full_name');
if (! in_array($full_name, $this->ignore)) {
$messages = $folder->messages()->all()->limit(10, 0)->get();
$messages = $folder->messages()->all()->limit($limit, 0)->get();

logger('[LaraChain] - Email Count', [
'count' => $messages->count(),
Expand All @@ -40,6 +40,12 @@ public function handle(): void

/** @var Message $message */
foreach ($messages as $message) {
//@NOTE the Seen flag made it too hard to
// then have different sources
// check the same email box.
// the Source will track repeats
//$flags = $message->getFlags();

$messageDto = MailDto::from([
'to' => $message->getTo()->toString(),
'from' => $message->getFrom()->toString(),
Expand All @@ -63,7 +69,7 @@ public function handle(): void
'slug' => $slug,
]);
$mail[] = new MailBoxParserJob($messageDto);
$message->delete(expunge: true);
$message->addFlag('Seen');
} else {
\Illuminate\Support\Facades\Log::info('Did not find Source with Slug To', [
'to' => $message->getTo()->toString(),
Expand Down
50 changes: 24 additions & 26 deletions app/Domains/EmailParser/EmailClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public function setConfig(array $config): self
* @throws \Webklex\PHPIMAP\Exceptions\MessageNotFoundException
* @throws \Webklex\PHPIMAP\Exceptions\RuntimeException
*/
public function handle(CredentialsDto $credentials, bool $delete = true): array
public function handle(CredentialsDto $credentials,
bool $delete = false,
int $limit = 10): array
{
$mail = [];

Expand Down Expand Up @@ -63,6 +65,7 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array
$client = EmailClientFacade::setConfig($config);

try {

$client->connect();

Log::info('Connected to email box', [
Expand All @@ -88,7 +91,7 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array
'folders_to_check' => $foldersToCheck,
]);

$messages = $folder->messages()->all()->get();
$messages = $folder->messages()->all()->limit($limit, 0)->get();

Log::info('[LaraChain] - Email Box Count', [
'count' => $messages->count(),
Expand All @@ -97,30 +100,25 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array

/** @var Message $message */
foreach ($messages as $message) {
$flags = $message->getFlags();

if (! $flags->contains('Seen')) {
$messageDto = MailDto::from([
'to' => $message->getTo()->toString(),
'from' => $message->getFrom()->toString(),
'body' => $message->getTextBody(),
'subject' => $message->getSubject(),
'date' => $message->getDate()->toString(),
'header' => $message->getHeader()->raw,
]);

$mail[] = $messageDto;

if ($delete) {
$message->delete(expunge: true);
} else {
$message->addFlag('Seen');
}
} else {
Log::info('[LaraChain] - Flag Seen', [
'flags' => $flags->toArray(),
]);
}
//@NOTE the Seen flag made it too hard to
// then have different sources
// check the same email box.
// the Source will track repeats
//$flags = $message->getFlags();

$messageDto = MailDto::from([
'to' => $message->getTo()->toString(),
'from' => $message->getFrom()->toString(),
'body' => $message->getTextBody(),
'subject' => $message->getSubject(),
'date' => $message->getDate()->toString(),
'header' => $message->getHeader()->raw,
'email_message' => $message,
]);

$mail[] = $messageDto;

$message->addFlag('Seen');
}

}
Expand Down
4 changes: 3 additions & 1 deletion app/Domains/EmailParser/MailDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domains\EmailParser;

use Spatie\LaravelData\Data;
use Webklex\PHPIMAP\Message;

class MailDto extends Data
{
Expand All @@ -12,7 +13,8 @@ public function __construct(
public ?string $to,
public ?string $body,
public ?string $header,
public ?string $date
public ?string $date,
public ?Message $email_message = null
) {
}

Expand Down
62 changes: 62 additions & 0 deletions app/Domains/Prompts/EventPagePrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Domains\Prompts;

use Illuminate\Support\Facades\Log;

class EventPagePrompt
{
public static function prompt(string $context): string
{
Log::info('[LaraChain] - EventPagePrompt');

return <<<PROMPT
<ROLE>
You are an AI assistant tasked with extracting event data from website content.

<INSTRUCTIONS>
1. Analyze the provided website HTML content below the <CONTENT> tag.
2. Look for information about events within the content.
3. If no event data is found, respond with a single word: false
4. If event data is found, extract the following information for each event:
- Event Title
- Start Date
- End Date
- Location
- Description
- Any other relevant data
5. Format the extracted data as a JSON array according to the specifications below.

<OUTPUT_FORMAT>
If events are found, return a JSON array with the following structure:

[
{
"title": "Event Title",
"startDate": "Start Date",
"endDate": "End Date",
"location": "Location",
"description": "Description",
"additionalInfo": "Any other relevant data"
},
{
"title": "Event Title",
"startDate": "Start Date",
"endDate": "End Date",
"location": "Location",
"description": "Description",
"additionalInfo": "Any other relevant data"
}
]

If no events are found, return an empty JSON array: []

<CONTENT>
$context
</CONTENT>

Respond only with the JSON array or 'false' if no events are found. Do not include any explanations or additional text in your response.

PROMPT;
}
}
37 changes: 37 additions & 0 deletions app/Domains/Prompts/SpecificTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Domains\Prompts;

use Illuminate\Support\Facades\Log;

class SpecificTopic
{
public static function prompt(string $context): string
{

Log::info('[LaraChain] - SpecificTopic');

return <<<'PROMPT'
<ROLE>
You are an email reading assistant who will follow the prompts to help parse my email box. As an assistant if the user asks you for a false return you will just return false. NOTHING MORE

<TASKS>
If the email content passed in is about Web Application work the frame work then keep and and summarize it. Else if it is about anything else just return the word false and only the word false. Please IGNORE Spam emails or Subjects that are about web applications but then the body is SPAM

<Format>
On a non false response, Summary and original message as Markdown.
On a false response just the word false,

<EXAMPLE>
I would like to hire you to build an awesome application for me with DailyAi
"You have an email from Teddy asking you to use DailAi to automate his business.

I would like to sell you property in Alaska
False
<END EXAMPLES>

<CONTENT>
[CONTEXT]
PROMPT;
}
}
19 changes: 19 additions & 0 deletions app/Domains/Sources/BaseSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\Prompts\PromptMerge;
use App\Domains\UnStructured\StructuredTypeEnum;
use App\Helpers\ChatHelperTrait;
use App\Jobs\DocumentProcessingCompleteJob;
use App\Jobs\SummarizeDocumentJob;
use App\Jobs\VectorlizeDataJob;
Expand All @@ -17,12 +18,17 @@
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\ToolsHelper;
use LlmLaraHub\TagFunction\Jobs\TagDocumentJob;

abstract class BaseSource
{
use ChatHelperTrait, ToolsHelper;

public string $batchTitle = 'Chunking Source';

public bool $promptPower = true;

public static string $description = 'Sources are ways we get data into the system. They are the core of the system.';

public ?Document $document = null;
Expand Down Expand Up @@ -215,4 +221,17 @@ protected function getEmailSummary(Document $document): string

return $content;
}

public function getSourceFromSlug(string $slug): ?Source
{
$source = Source::where('type', $this->sourceTypeEnum)
->slug($slug)
->first();

if ($source) {
return $source;
}

return null;
}
}
41 changes: 2 additions & 39 deletions app/Domains/Sources/EmailBoxSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use App\Domains\EmailParser\CredentialsDto;
use App\Models\Source;
use Facades\App\Domains\EmailParser\EmailClient;
use Facades\App\Domains\Transformers\EmailTransformer;
use Illuminate\Support\Facades\Log;
use Facades\App\Domains\Sources\EmailSource as EmailSourceFacade;

class EmailBoxSource extends EmailSource
{
Expand All @@ -22,43 +21,7 @@ public function handle(Source $source): void
$this->source = $source;

foreach ($mails as $mailDto) {
$this->mailDto = $mailDto;

$this->content = $this->mailDto->getContent();

$this->documentSubject = $this->mailDto->subject;

$this->meta_data = $this->mailDto->toArray();

$this->transformers = $source->transformers;

Log::info('[LaraChain] - Running Email Source');

try {
Log::info('Do something!');
$baseSource = EmailTransformer::transform(baseSource: $this);
foreach ($source->transformers as $transformerChainLink) {
Log::info("[LaraChain] - Source has Transformers let's figure out which one to run", [
'type' => $transformerChainLink->type->name,
]);

$class = '\\App\\Domains\\Transformers\\'.$transformerChainLink->type->name;
if (class_exists($class)) {
$facade = '\\Facades\\App\\Domains\\Transformers\\'.$transformerChainLink->type->name;
$baseSource = $facade::transform($this);
} else {
Log::info('[LaraChain] - No Class found ', [
'class' => $class,
]);
}
}

$this->batchTransformedSource($baseSource, $source);
} catch (\Exception $e) {
Log::error('[LaraChain] - Error running Email Source', [
'error' => $e->getMessage(),
]);
}
EmailSourceFacade::setMailDto($mailDto)->handle($source);
}

}
Expand Down
Loading
Loading