Skip to content

Commit

Permalink
report being built in the ui
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 9, 2024
1 parent 82104f5 commit 5dd42f9
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 19 deletions.
1 change: 1 addition & 0 deletions Modules/LlmDriver/app/Functions/ReportingTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function handle(

$report = Report::firstOrCreate([
'chat_id' => $message->getChat()->id,
'message_id' => $message->id,
'reference_collection_id' => $message->getReferenceCollection()?->id,
'user_id' => $message->getChat()->user_id,
], [
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function toArray(Request $request): array
'body_markdown' => str($this->body)->markdown(),
'meta_data' => $this->meta_data,
'tools' => $this->tools,
'report' => new ReportResource($this->report),
'diff_for_humans' => $this->created_at->diffForHumans(),
'prompt_histories' => PromptHistoryResource::collection($this->prompt_histories),
'prompt_histories_plain' => PromptHistoryResource::collection($this->prompt_histories),
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Resources/ReportResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ReportResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'user_id' => $this->user_id,
'chat_id' => $this->chat_id,
'type' => $this->type,
'reference_collection_id' => new CollectionResource($this->reference_collection),
'sections' => SectionResource::collection($this->sections),
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/SectionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SectionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
7 changes: 7 additions & 0 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\HasDrivers;
Expand Down Expand Up @@ -84,6 +86,11 @@ public function compressMessage($message): array|string|null

}

public function report(): HasOne
{
return $this->hasOne(Report::class);
}

/**
* Return the chat that the message belongs to.
*/
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Report extends Model
Expand All @@ -24,11 +25,22 @@ public function user(): BelongsTo
return $this->belongsTo(User::class);
}

public function message(): BelongsTo
{
return $this->belongsTo(Message::class);
}


public function chat(): BelongsTo
{
return $this->belongsTo(Chat::class);
}

public function sections(): HasMany
{
return $this->hasMany(Section::class);
}

public function reference_collection(): BelongsTo
{
return $this->belongsTo(Collection::class, 'reference_collection_id');
Expand Down
1 change: 1 addition & 0 deletions database/factories/ReportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function definition(): array
'user_id' => User::factory(),
'chat_id' => Chat::factory(),
'reference_collection_id' => Collection::factory(),
'message_id' => null,
'type' => \App\Domains\Reporting\ReportTypeEnum::RFP,

];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('reports', function (Blueprint $table) {
$table->foreignIdFor(\App\Models\Message::class)->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('reports', function (Blueprint $table) {
//
});
}
};
59 changes: 41 additions & 18 deletions resources/js/Pages/Chat/ChatMessageV2.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import History from './Components/History.vue'
import Clipboard from "@/Components/ClipboardButton.vue";
import {useForm} from "@inertiajs/vue3";
import {onMounted, ref} from "vue";
import Report from "@/Pages/Chat/Components/Report.vue";
const props = defineProps({
message: Object
Expand Down Expand Up @@ -48,6 +49,9 @@ const reuse = (prompt) => {
<Tab as="div" v-slot="{ selected }">
<div :class="{ 'underline': selected }" class="hover:cursor-pointer m4-2">Prompt History</div>
</Tab>
<Tab as="div" v-slot="{ selected }">
<div :class="{ 'underline': selected }" class="hover:cursor-pointer m4-2">Report</div>
</Tab>
</TabList>
<TabPanels v-auto-animate>
<TabPanel>
Expand Down Expand Up @@ -91,41 +95,60 @@ const reuse = (prompt) => {
</div>
</div>
</TabPanel>
<TabPanel>
<div class="w-fullp-4 mt-2 shadow-lg rounded-md">
<div>
<div class="overflow-x-auto">
<Report :message="message" />
</div>
</div>
</div>
</TabPanel>
</TabPanels>
</TabGroup>
</div>
<div v-else
class="flex-col rounded-md shadow-lg p-4 border-neutral border mb-4">
<div class="justify-end flex text-xs text-gray-500">
<div class="justify-end flex text-xs text-gray-500 -mb-5">
#{{ message.id}}
</div>

<div class="grow leading-loose prose" v-html="message.body_markdown">
</div>

<div class="w-full flex justify-end gap-2 items-center">
<button type="button" class="btn btn-ghost" @click="reuse(message.body)">
<span>Reuse Prompt</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
</button>
</div>
<div class="flex justify-start text-sm gap-2 items-center">
<div v-if="message.meta_data?.persona" class="flex justify-start gap-2 items-center">
<div class="flex justify-between items-center -mb-2">
<div class="flex justify-start text-sm gap-2 items-center w-3/4">
<div v-if="message.meta_data?.persona" class="flex justify-start gap-2 items-center">
<span class="text-secondary">
Persona being used: </span>
<span class="font-bold">{{message.meta_data.persona}}</span>
</div>
<div v-if="message.meta_data?.filter" class="flex justify-start gap-2 items-center">
<span class="font-bold">{{message.meta_data.persona}}</span>
</div>
<div v-if="message.meta_data?.filter" class="flex justify-start gap-2 items-center">
<span class="text-secondary">
Filter being used: </span>
<span class="font-bold">{{message.meta_data.filter.name}}</span>
</div>
<span class="font-bold">{{message.meta_data.filter.name}}</span>
</div>

<div v-if="message.meta_data?.date_range" class="flex justify-start gap-2 items-center">
<div v-if="message.meta_data?.date_range" class="flex justify-start gap-2 items-center">
<span class="text-secondary">
Date Range used: </span>
<span class="font-bold">{{message.meta_data.date_range}}</span>
<span class="font-bold">{{message.meta_data.date_range}}</span>
</div>

<div v-if="message.meta_data?.tool" class="flex justify-start gap-2 items-center">
<span class="text-secondary">
Tool used: </span>
<span class="font-bold">{{message.meta_data.tool}}</span>
</div>
</div>

<div class="w-full flex justify-end gap-2 items-center">
<button type="button" class="btn btn-ghost" @click="reuse(message.body)">
<span>Reuse Prompt</span>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3" />
</svg>
</button>
</div>
</div>
</div>
Expand Down
28 changes: 28 additions & 0 deletions resources/js/Pages/Chat/Components/Report.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div class="p-10">
<h2>Reporting sections surfaced from the collection</h2>
<div v-if="message.report?.sections.length === 0">
No sections read more <a href="https://docs.larallama.io/docs/reporting" target="_blank">here</a>
on building reports
</div>
<div v-for="section in message.report?.sections" class="border border-neutral rounded-md p-4 mt-4">
<h3 class="font-bold">Section ID: {{ section.id }} - {{ section.subject }}</h3>
<div class="prose" v-html="section.content"></div>
<div class="flex justify-end gap-2 items-center text-sm text-gray-500">
<div> sort: {{ section.sort_order + 1 }}</div>
</div>
</div>
</div>
</template>
<script setup>
import {computed} from "vue";
import Clipboard from "@/Components/ClipboardButton.vue";
const props = defineProps({
message: Object
})
const allPrompt = computed(() => {
return props.message.prompt_histories_plain.map(prompt => prompt.prompt_plain).join('\n\n')
})
</script>
7 changes: 7 additions & 0 deletions tests/Feature/Models/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Collection;
use App\Models\Filter;
use App\Models\Message;
use App\Models\Report;
use App\Models\User;
use Facades\LlmLaraHub\LlmDriver\Orchestrate;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand All @@ -25,7 +26,13 @@ class MessageTest extends TestCase
public function test_factory(): void
{
$model = Message::factory()->create();

$report = Report::factory()->create([
'message_id' => $model->id,
]);

$this->assertNotNull($model->body);
$this->assertNotNull($model->report->id);
$this->assertNotNull($model->chat->id);
$this->assertInstanceOf(MetaDataDto::class, $model->meta_data);
$this->assertNotNull($model->meta_data->date_range);
Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/Models/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Feature\Models;

use App\Models\Message;
use App\Models\Report;
use App\Models\Section;
use Tests\TestCase;

class ReportTest extends TestCase
Expand All @@ -12,12 +14,21 @@ class ReportTest extends TestCase
*/
public function test_model(): void
{
$model = Report::factory()->create();
$model = Report::factory()->create([
'message_id' => Message::factory(),
]);

$section = Section::factory()->create([
'report_id' => $model->id,
]);

$this->assertNotNull($model->user_id);
$this->assertNotNull($model->chat_id);
$this->assertNotNull($model->type);
$this->assertNotNull($model->message->id);
$this->assertNotNull($model->user->id);
$this->assertNotNull($model->reference_collection->id);
$this->assertNotNull($model->sections->first()->id);
$this->assertNotNull($section->report->id);
}
}

0 comments on commit 5dd42f9

Please sign in to comment.