Skip to content

Commit 54aea8e

Browse files
authored
Merge pull request #40 from LlmLaraHub/source_prompting
Source prompting
2 parents ce275fe + e75649f commit 54aea8e

File tree

74 files changed

+8043
-792
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+8043
-792
lines changed

app/Domains/Chat/MetaDataDto.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function __construct(
1515
public mixed $date_range = '',
1616
public mixed $input = '',
1717
public mixed $driver = '',
18+
public mixed $source = '',
1819
public mixed $reference_collection_id = '',
1920
) {
2021

app/Domains/Documents/Transformers/CSVTransformer.php

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function handle(Document $document): array
2828

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

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

app/Domains/EmailParser/Client.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Client
1818
'Drafts',
1919
];
2020

21-
public function handle(): void
21+
public function handle(int $limit = 10): void
2222
{
2323
$mail = [];
2424

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

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

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

4141
/** @var Message $message */
4242
foreach ($messages as $message) {
43+
//@NOTE the Seen flag made it too hard to
44+
// then have different sources
45+
// check the same email box.
46+
// the Source will track repeats
47+
//$flags = $message->getFlags();
48+
4349
$messageDto = MailDto::from([
4450
'to' => $message->getTo()->toString(),
4551
'from' => $message->getFrom()->toString(),
@@ -63,7 +69,7 @@ public function handle(): void
6369
'slug' => $slug,
6470
]);
6571
$mail[] = new MailBoxParserJob($messageDto);
66-
$message->delete(expunge: true);
72+
$message->addFlag('Seen');
6773
} else {
6874
\Illuminate\Support\Facades\Log::info('Did not find Source with Slug To', [
6975
'to' => $message->getTo()->toString(),

app/Domains/EmailParser/EmailClient.php

+24-26
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function setConfig(array $config): self
3535
* @throws \Webklex\PHPIMAP\Exceptions\MessageNotFoundException
3636
* @throws \Webklex\PHPIMAP\Exceptions\RuntimeException
3737
*/
38-
public function handle(CredentialsDto $credentials, bool $delete = true): array
38+
public function handle(CredentialsDto $credentials,
39+
bool $delete = false,
40+
int $limit = 10): array
3941
{
4042
$mail = [];
4143

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

6567
try {
68+
6669
$client->connect();
6770

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

91-
$messages = $folder->messages()->all()->get();
94+
$messages = $folder->messages()->all()->limit($limit, 0)->get();
9295

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

98101
/** @var Message $message */
99102
foreach ($messages as $message) {
100-
$flags = $message->getFlags();
101-
102-
if (! $flags->contains('Seen')) {
103-
$messageDto = MailDto::from([
104-
'to' => $message->getTo()->toString(),
105-
'from' => $message->getFrom()->toString(),
106-
'body' => $message->getTextBody(),
107-
'subject' => $message->getSubject(),
108-
'date' => $message->getDate()->toString(),
109-
'header' => $message->getHeader()->raw,
110-
]);
111-
112-
$mail[] = $messageDto;
113-
114-
if ($delete) {
115-
$message->delete(expunge: true);
116-
} else {
117-
$message->addFlag('Seen');
118-
}
119-
} else {
120-
Log::info('[LaraChain] - Flag Seen', [
121-
'flags' => $flags->toArray(),
122-
]);
123-
}
103+
//@NOTE the Seen flag made it too hard to
104+
// then have different sources
105+
// check the same email box.
106+
// the Source will track repeats
107+
//$flags = $message->getFlags();
108+
109+
$messageDto = MailDto::from([
110+
'to' => $message->getTo()->toString(),
111+
'from' => $message->getFrom()->toString(),
112+
'body' => $message->getTextBody(),
113+
'subject' => $message->getSubject(),
114+
'date' => $message->getDate()->toString(),
115+
'header' => $message->getHeader()->raw,
116+
'email_message' => $message,
117+
]);
118+
119+
$mail[] = $messageDto;
120+
121+
$message->addFlag('Seen');
124122
}
125123

126124
}

app/Domains/EmailParser/MailDto.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Domains\EmailParser;
44

55
use Spatie\LaravelData\Data;
6+
use Webklex\PHPIMAP\Message;
67

78
class MailDto extends Data
89
{
@@ -12,7 +13,8 @@ public function __construct(
1213
public ?string $to,
1314
public ?string $body,
1415
public ?string $header,
15-
public ?string $date
16+
public ?string $date,
17+
public ?Message $email_message = null
1618
) {
1719
}
1820

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Domains\Prompts;
4+
5+
use Illuminate\Support\Facades\Log;
6+
7+
class EventPagePrompt
8+
{
9+
public static function prompt(string $context): string
10+
{
11+
Log::info('[LaraChain] - EventPagePrompt');
12+
13+
return <<<PROMPT
14+
<ROLE>
15+
You are an AI assistant tasked with extracting event data from website content.
16+
17+
<INSTRUCTIONS>
18+
1. Analyze the provided website HTML content below the <CONTENT> tag.
19+
2. Look for information about events within the content.
20+
3. If no event data is found, respond with a single word: false
21+
4. If event data is found, extract the following information for each event:
22+
- Event Title
23+
- Start Date
24+
- End Date
25+
- Location
26+
- Description
27+
- Any other relevant data
28+
5. Format the extracted data as a JSON array according to the specifications below.
29+
30+
<OUTPUT_FORMAT>
31+
If events are found, return a JSON array with the following structure:
32+
33+
[
34+
{
35+
"title": "Event Title",
36+
"startDate": "Start Date",
37+
"endDate": "End Date",
38+
"location": "Location",
39+
"description": "Description",
40+
"additionalInfo": "Any other relevant data"
41+
},
42+
{
43+
"title": "Event Title",
44+
"startDate": "Start Date",
45+
"endDate": "End Date",
46+
"location": "Location",
47+
"description": "Description",
48+
"additionalInfo": "Any other relevant data"
49+
}
50+
]
51+
52+
If no events are found, return an empty JSON array: []
53+
54+
<CONTENT>
55+
$context
56+
</CONTENT>
57+
58+
Respond only with the JSON array or 'false' if no events are found. Do not include any explanations or additional text in your response.
59+
60+
PROMPT;
61+
}
62+
}

app/Domains/Prompts/SpecificTopic.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Domains\Prompts;
4+
5+
use Illuminate\Support\Facades\Log;
6+
7+
class SpecificTopic
8+
{
9+
public static function prompt(string $context): string
10+
{
11+
12+
Log::info('[LaraChain] - SpecificTopic');
13+
14+
return <<<'PROMPT'
15+
<ROLE>
16+
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
17+
18+
<TASKS>
19+
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
20+
21+
<Format>
22+
On a non false response, Summary and original message as Markdown.
23+
On a false response just the word false,
24+
25+
<EXAMPLE>
26+
I would like to hire you to build an awesome application for me with DailyAi
27+
"You have an email from Teddy asking you to use DailAi to automate his business.
28+
29+
I would like to sell you property in Alaska
30+
False
31+
<END EXAMPLES>
32+
33+
<CONTENT>
34+
[CONTEXT]
35+
PROMPT;
36+
}
37+
}

app/Domains/Sources/BaseSource.php

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Domains\Prompts\PromptMerge;
66
use App\Domains\UnStructured\StructuredTypeEnum;
7+
use App\Helpers\ChatHelperTrait;
78
use App\Jobs\DocumentProcessingCompleteJob;
89
use App\Jobs\SummarizeDocumentJob;
910
use App\Jobs\VectorlizeDataJob;
@@ -17,12 +18,17 @@
1718
use Illuminate\Support\Facades\Bus;
1819
use Illuminate\Support\Facades\Log;
1920
use LlmLaraHub\LlmDriver\LlmDriverFacade;
21+
use LlmLaraHub\LlmDriver\ToolsHelper;
2022
use LlmLaraHub\TagFunction\Jobs\TagDocumentJob;
2123

2224
abstract class BaseSource
2325
{
26+
use ChatHelperTrait, ToolsHelper;
27+
2428
public string $batchTitle = 'Chunking Source';
2529

30+
public bool $promptPower = true;
31+
2632
public static string $description = 'Sources are ways we get data into the system. They are the core of the system.';
2733

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

216222
return $content;
217223
}
224+
225+
public function getSourceFromSlug(string $slug): ?Source
226+
{
227+
$source = Source::where('type', $this->sourceTypeEnum)
228+
->slug($slug)
229+
->first();
230+
231+
if ($source) {
232+
return $source;
233+
}
234+
235+
return null;
236+
}
218237
}

app/Domains/Sources/EmailBoxSource.php

+2-39
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use App\Domains\EmailParser\CredentialsDto;
66
use App\Models\Source;
77
use Facades\App\Domains\EmailParser\EmailClient;
8-
use Facades\App\Domains\Transformers\EmailTransformer;
9-
use Illuminate\Support\Facades\Log;
8+
use Facades\App\Domains\Sources\EmailSource as EmailSourceFacade;
109

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

2423
foreach ($mails as $mailDto) {
25-
$this->mailDto = $mailDto;
26-
27-
$this->content = $this->mailDto->getContent();
28-
29-
$this->documentSubject = $this->mailDto->subject;
30-
31-
$this->meta_data = $this->mailDto->toArray();
32-
33-
$this->transformers = $source->transformers;
34-
35-
Log::info('[LaraChain] - Running Email Source');
36-
37-
try {
38-
Log::info('Do something!');
39-
$baseSource = EmailTransformer::transform(baseSource: $this);
40-
foreach ($source->transformers as $transformerChainLink) {
41-
Log::info("[LaraChain] - Source has Transformers let's figure out which one to run", [
42-
'type' => $transformerChainLink->type->name,
43-
]);
44-
45-
$class = '\\App\\Domains\\Transformers\\'.$transformerChainLink->type->name;
46-
if (class_exists($class)) {
47-
$facade = '\\Facades\\App\\Domains\\Transformers\\'.$transformerChainLink->type->name;
48-
$baseSource = $facade::transform($this);
49-
} else {
50-
Log::info('[LaraChain] - No Class found ', [
51-
'class' => $class,
52-
]);
53-
}
54-
}
55-
56-
$this->batchTransformedSource($baseSource, $source);
57-
} catch (\Exception $e) {
58-
Log::error('[LaraChain] - Error running Email Source', [
59-
'error' => $e->getMessage(),
60-
]);
61-
}
24+
EmailSourceFacade::setMailDto($mailDto)->handle($source);
6225
}
6326

6427
}

0 commit comments

Comments
 (0)