Skip to content

Commit 15adc0b

Browse files
committed
working on JSON left as is not easy answer to this one
1 parent 52f2c92 commit 15adc0b

17 files changed

+297
-90
lines changed

Modules/LlmDriver/app/BaseClient.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function setFormatJson(): self
3535

3636
public function modifyPayload(array $payload): array
3737
{
38+
$payload = $this->addJsonFormat($payload);
39+
3840
return $payload;
3941
}
4042

@@ -62,12 +64,6 @@ protected function messagesToArray(array $messages): array
6264

6365
public function addJsonFormat(array $payload): array
6466
{
65-
if ($this->formatJson) {
66-
$payload['response_format'] = [
67-
'type' => 'json_object',
68-
];
69-
}
70-
7167
return $payload;
7268
}
7369

Modules/LlmDriver/app/Functions/ReportingTool.php

+9-50
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
namespace LlmLaraHub\LlmDriver\Functions;
44

5-
use App\Domains\Messages\RoleEnum;
65
use App\Domains\Prompts\ReportBuildingFindRequirementsPrompt;
7-
use App\Domains\Prompts\ReportingSummaryPrompt;
86
use App\Domains\Reporting\ReportTypeEnum;
97
use App\Domains\Reporting\StatusEnum;
108
use App\Jobs\MakeReportSectionsJob;
9+
use App\Jobs\ReportingToolSummarizeReportJob;
1110
use App\Jobs\ReportMakeEntriesJob;
1211
use App\Models\Message;
1312
use App\Models\Report;
1413
use Illuminate\Bus\Batch;
1514
use Illuminate\Support\Collection;
1615
use Illuminate\Support\Facades\Bus;
1716
use Illuminate\Support\Facades\Log;
18-
use LlmLaraHub\LlmDriver\LlmDriverFacade;
19-
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
2017
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;
2118
use LlmLaraHub\LlmDriver\ToolsHelper;
2219

@@ -32,8 +29,6 @@ class ReportingTool extends FunctionContract
3229

3330
protected array $results = [];
3431

35-
protected array $promptHistory = [];
36-
3732
protected array $sectionJobs = [];
3833

3934
public function handle(
@@ -74,45 +69,26 @@ public function handle(
7469
])->name(sprintf('Reporting Entities Report Id %s', $report->id))
7570
->allowFailures()
7671
->finally(function (Batch $batch) use ($report) {
77-
$report->update([
78-
'status_entries_generation' => StatusEnum::Complete,
79-
]);
72+
Bus::batch([
73+
new ReportingToolSummarizeReportJob($report),
74+
])->name(sprintf('Reporting Tool Summarize Report Id %s', $report->id))
75+
->allowFailures()
76+
->dispatch();
8077
})
8178
->dispatch();
8279

8380
})
8481
->dispatch();
8582

86-
notify_ui($message->getChat(), 'Building Summary');
87-
88-
$response = $this->summarizeReport($report);
89-
9083
$report->update([
9184
'status_sections_generation' => StatusEnum::Running,
9285
]);
9386

94-
$assistantMessage = $message->getChat()->addInput(
95-
message: $response->content,
96-
role: RoleEnum::Assistant,
97-
systemPrompt: $message->getChat()->getChatable()->systemPrompt(),
98-
show_in_thread: true,
99-
meta_data: $message->meta_data,
100-
tools: $message->tools
101-
);
102-
103-
$this->savePromptHistory($assistantMessage,
104-
implode("\n", $this->promptHistory));
105-
106-
$report->message_id = $assistantMessage->id;
107-
$report->save();
108-
109-
notify_ui($message->getChat(), 'Building Solutions list');
110-
notify_ui_report($report, 'Building Solutions list');
111-
notify_ui_complete($report->getChat());
87+
notify_ui($report->getChat(), 'Running');
11288

11389
return FunctionResponse::from([
114-
'content' => $response->content,
115-
'prompt' => implode('\n', $this->promptHistory),
90+
'content' => 'Building report and Sections and then summarizing',
91+
'prompt' => '',
11692
'requires_followup' => false,
11793
'documentChunks' => collect([]),
11894
'save_to_message' => false,
@@ -159,23 +135,6 @@ protected function buildUpSections(Collection $documents, Report $report, Messag
159135
}
160136
}
161137

162-
protected function summarizeReport(Report $report): CompletionResponse
163-
{
164-
$sectionContent = $report->refresh()->sections->pluck('content')->toArray();
165-
$sectionContent = implode("\n", $sectionContent);
166-
167-
$prompt = ReportingSummaryPrompt::prompt($sectionContent);
168-
169-
$this->promptHistory = [$prompt];
170-
171-
/** @var CompletionResponse $response */
172-
$response = LlmDriverFacade::driver(
173-
$report->getChatable()->getDriver()
174-
)->completion($prompt);
175-
176-
return $response;
177-
}
178-
179138
/**
180139
* @return PropertyDto[]
181140
*/

Modules/LlmDriver/app/Functions/ReportingToolMakeSections.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Models\Document;
66
use App\Models\Report;
77
use App\Models\Section;
8+
use Illuminate\Support\Arr;
89
use Illuminate\Support\Facades\Log;
910
use LlmLaraHub\LlmDriver\LlmDriverFacade;
1011

@@ -22,6 +23,9 @@ public function handle(
2223

2324
protected function poolPrompt(array $prompts, Report $report, Document $document): void
2425
{
26+
/**
27+
* @NOTE if Format JSON not good enough will try this again
28+
*/
2529
$dto = FunctionDto::from([
2630
'name' => 'reporting_json',
2731
'description' => 'JSON Summary of the report',
@@ -44,12 +48,12 @@ protected function poolPrompt(array $prompts, Report $report, Document $document
4448
]),
4549
]);
4650

47-
Log::info('LlmDriver::ClaudeClient::poolPrompt', [
51+
Log::info('LlmDriver::Reporting::poolPrompt', [
4852
'driver' => $report->getDriver(),
49-
'dto' => $dto,
53+
'prompts' => $prompts,
5054
]);
55+
5156
$results = LlmDriverFacade::driver($report->getDriver())
52-
->setForceTool($dto)
5357
->completionPool($prompts);
5458

5559
foreach ($results as $resultIndex => $result) {
@@ -69,9 +73,10 @@ protected function makeSectionFromContent(
6973
notify_ui_report($report, 'Building Requirements list');
7074

7175
$contentDecoded = json_decode($content, true);
76+
$contentDecoded = Arr::wrap($contentDecoded);
7277
foreach ($contentDecoded as $sectionIndex => $sectionText) {
73-
$title = data_get($sectionText, 'title', 'NOT TITLE GIVEN');
74-
$contentBody = data_get($sectionText, 'content', 'NOT CONTENT GIVEN');
78+
$title = data_get($sectionText, 'title', 'NO TITLE GIVEN');
79+
$contentBody = data_get($sectionText, 'content', 'NO CONTENT GIVEN');
7580
Section::updateOrCreate([
7681
'document_id' => $document->id,
7782
'report_id' => $report->id,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace LlmLaraHub\LlmDriver\Functions\Reports;
4+
5+
use App\Domains\Messages\RoleEnum;
6+
use App\Domains\Prompts\ReportingSummaryPrompt;
7+
use App\Models\Report;
8+
use LlmLaraHub\LlmDriver\LlmDriverFacade;
9+
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
10+
use LlmLaraHub\LlmDriver\ToolsHelper;
11+
12+
class SummarizeReport
13+
{
14+
use ToolsHelper;
15+
16+
protected array $promptHistory = [];
17+
18+
public function handle(Report $report)
19+
{
20+
$message = $report->message;
21+
22+
notify_ui($message->getChat(), 'Building Summary');
23+
24+
$response = $this->summarizeReport($report);
25+
26+
$assistantMessage = $message->getChat()->addInput(
27+
message: $response->content,
28+
role: RoleEnum::Assistant,
29+
systemPrompt: $message->getChat()->getChatable()->systemPrompt(),
30+
show_in_thread: true,
31+
meta_data: $message->meta_data,
32+
tools: $message->tools
33+
);
34+
35+
$this->savePromptHistory($assistantMessage,
36+
implode("\n", $this->promptHistory));
37+
38+
$report->message_id = $assistantMessage->id;
39+
$report->save();
40+
41+
notify_ui($message->getChat(), 'Building Solutions list');
42+
notify_ui_report($report, 'Building Solutions list');
43+
notify_ui_complete($report->getChat());
44+
}
45+
46+
protected function summarizeReport(Report $report): CompletionResponse
47+
{
48+
$sectionContent = $report->refresh()->sections->pluck('content')->toArray();
49+
$sectionContent = implode("\n", $sectionContent);
50+
51+
$prompt = ReportingSummaryPrompt::prompt($sectionContent);
52+
53+
$this->promptHistory = [$prompt];
54+
55+
/** @var CompletionResponse $response */
56+
$response = LlmDriverFacade::driver(
57+
$report->getChatable()->getDriver()
58+
)->completion($prompt);
59+
60+
return $response;
61+
}
62+
}

Modules/LlmDriver/app/Helpers/CreateReferencesTrait.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ protected function saveDocumentReference(
1111
Message $model,
1212
Collection $documentChunks
1313
): void {
14-
//put_fixture("document_chunks.json", $documentChunks->toArray());
15-
//add each one to a batch job or do the work here.
1614
foreach ($documentChunks as $documentChunk) {
1715
$model->message_document_references()->create([
1816
'document_chunk_id' => $documentChunk->id,

Modules/LlmDriver/app/OllamaClient.php

+27-9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function embedData(string $prompt): EmbeddingsResponseDto
3333
]);
3434
}
3535

36+
public function addJsonFormat(array $payload): array
37+
{
38+
//@NOTE Just too hard if it is an array of objects
39+
//$payload['format'] = 'json';
40+
return $payload;
41+
}
42+
3643
/**
3744
* This is to get functions out of the llm
3845
* if none are returned your system
@@ -89,13 +96,15 @@ public function chat(array $messages): CompletionResponse
8996

9097
$messages = $this->remapMessages($messages);
9198

92-
put_fixture('messages_llama3.json', $messages);
93-
94-
$response = $this->getClient()->post('/chat', [
99+
$payload = [
95100
'model' => $this->getConfig('ollama')['models']['completion_model'],
96101
'messages' => $messages,
97102
'stream' => false,
98-
]);
103+
];
104+
105+
$payload = $this->modifyPayload($payload);
106+
107+
$response = $this->getClient()->post('/chat', $payload);
99108

100109
$results = $response->json()['message']['content'];
101110

@@ -123,15 +132,24 @@ public function completionPool(array $prompts, int $temperature = 0): array
123132
$baseUrl
124133
) {
125134
foreach ($prompts as $prompt) {
135+
$payload = [
136+
'model' => $model,
137+
'prompt' => $prompt,
138+
'stream' => false,
139+
];
140+
141+
$payload = $this->modifyPayload($payload);
142+
143+
Log::info('Ollama Request', [
144+
'prompt' => $prompt,
145+
'payload' => $payload,
146+
]);
147+
126148
$pool->withHeaders([
127149
'content-type' => 'application/json',
128150
])->timeout(300)
129151
->baseUrl($baseUrl)
130-
->post('/generate', [
131-
'model' => $model,
132-
'prompt' => $prompt,
133-
'stream' => false,
134-
]);
152+
->post('/generate', $payload);
135153
}
136154
});
137155

Modules/LlmDriver/app/OpenAiClient.php

+14
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function completionPool(array $prompts, int $temperature = 0): array
107107
}
108108

109109
$responses = Http::pool(function (Pool $pool) use ($prompts, $token) {
110+
110111
foreach ($prompts as $prompt) {
111112
$payload = [
112113
'model' => $this->getConfig('openai')['models']['completion_model'],
@@ -272,6 +273,19 @@ public function modifyPayload(array $payload): array
272273
return $payload;
273274
}
274275

276+
public function addJsonFormat(array $payload): array
277+
{
278+
// @NOTE the results are not great if you want an array of objects
279+
280+
// if ($this->formatJson) {
281+
// $payload['response_format'] = [
282+
// 'type' => 'json_object',
283+
// ];
284+
// }
285+
286+
return $payload;
287+
}
288+
275289
/**
276290
* This is to get functions out of the llm
277291
* if none are returned your system

app/Domains/Prompts/ReportBuildingFindRequirementsPrompt.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function prompt(string $context, string $userPrompt, string $colle
1919
2020
### FORMAT ###
2121
Output in JSON format as an Array of Objects with keys: title (string), content (string).
22-
NO SURROUNDING TEXT JUST VALID JSON! START WITH [ and END WITH ]
22+
NO SURROUNDING TEXT JUST VALID JSON! START WITH [ and END WITH ] even if only one item found.
2323
2424
### User Prompt ###
2525
$userPrompt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Report;
6+
use Facades\LlmLaraHub\LlmDriver\Functions\Reports\SummarizeReport;
7+
use Illuminate\Bus\Batchable;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
use Illuminate\Queue\InteractsWithQueue;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class ReportingToolSummarizeReportJob implements ShouldQueue
15+
{
16+
use Batchable;
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
19+
/**
20+
* Create a new job instance.
21+
*/
22+
public function __construct(public Report $report)
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Execute the job.
29+
*/
30+
public function handle(): void
31+
{
32+
if ($this->batch()->cancelled()) {
33+
// Determine if the batch has been cancelled...
34+
35+
return;
36+
}
37+
38+
SummarizeReport::handle($this->report);
39+
}
40+
}

0 commit comments

Comments
 (0)