Skip to content

Commit

Permalink
add personas
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 13, 2024
1 parent 1c2cee6 commit ba615c3
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 28 deletions.
3 changes: 1 addition & 2 deletions Modules/LlmDriver/app/Functions/SummarizeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Domains\Agents\VerifyPromptInputDto;
use App\Domains\Agents\VerifyPromptOutputDto;
use App\Domains\Chat\UiStatusEnum;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Illuminate\Support\Facades\Log;
use Laravel\Pennant\Feature;
Expand Down Expand Up @@ -66,7 +65,7 @@ public function handle(
$this->verify($model, 'Can you summarize this collection of data for me.', $summary);
}

notify_ui($model->getChat(), UiStatusEnum::Complete->name);
notify_ui_complete($model->getChat());

return FunctionResponse::from([
'content' => $this->response,
Expand Down
19 changes: 16 additions & 3 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use App\Http\Resources\CollectionResource;
use App\Http\Resources\FilterResource;
use App\Http\Resources\MessageResource;
use App\Http\Resources\PersonaResource;
use App\Jobs\OrchestrateJob;
use App\Jobs\SimpleSearchAndSummarizeOrchestrateJob;
use App\Models\Chat;
use App\Models\Collection;
use App\Models\Filter;
use App\Models\Persona;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -44,6 +46,7 @@ public function showCollectionChat(Collection $collection, Chat $chat)
'chat' => new ChatResource($chat),
'chats' => ChatResource::collection($collection->chats()->latest()->paginate(20)),
'filters' => FilterResource::collection($collection->filters),
'personas' => PersonaResource::collection(Persona::all()),
'system_prompt' => $collection->systemPrompt(),
'settings' => [
'supports_functions' => LlmDriverFacade::driver($chat->getDriver())->hasFunctions(),
Expand All @@ -59,20 +62,30 @@ public function chat(Chat $chat)
'completion' => 'boolean',
'tool' => ['nullable', 'string'],
'filter' => ['nullable', 'integer'],
'persona' => ['nullable', 'integer'],
]);

try {
Log::info('Request', request()->toArray());

$input = $validated['input'];

$persona = data_get($validated, 'persona', null);

if ($persona) {
$persona = Persona::find($persona);
$input = $persona->wrapPromptInPersona($input);
}

$chat->addInput(
message: $validated['input'],
message: $input,
role: RoleEnum::User,
show_in_thread: true);

$messagesArray = [];

$messagesArray[] = MessageInDto::from([
'content' => $validated['input'],
'content' => $input,
'role' => 'user',
]);

Expand Down Expand Up @@ -110,7 +123,7 @@ public function chat(Chat $chat)
} else {
Log::info('[LaraChain] Simple Search and Summarize added to queue');
$this->batchJob([
new SimpleSearchAndSummarizeOrchestrateJob($validated['input'], $chat, $filter),
new SimpleSearchAndSummarizeOrchestrateJob($input, $chat, $filter),
], $chat, 'simple_search_and_summarize');
}

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Resources/DocumentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function toArray(Request $request): array
'tags_count' => $this->tags->count(),
'children_count' => $this->children->count(),
'parent_id' => $this->parent_id,
'created_at_diff' => $this->created_at->diffForHumans(),
'updated_at_diff' => $this->updated_at->diffForHumans(),
'children' => DocumentResource::collection($this->children),
];
}
Expand Down
18 changes: 18 additions & 0 deletions app/Models/Persona.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ class Persona extends Model
use HasFactory;

protected $guarded = [];

public function wrapPromptInPersona(string $prompt): string
{

$title = $this->name;
$persona = $this->content;
$prompt = <<<PROMPT
$prompt
**IN THE PERSONA OF**
Name of Persona: $title
Example Content of Persona:
$persona
** END PERSONA EXAMPLE **
PROMPT;

return $prompt;
}
}
8 changes: 7 additions & 1 deletion resources/js/Layouts/AppLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,18 @@ const theme = ref('dark')
LaraLlama.io
</a>
</li>
<li>
<li class="hidden sm:flex">
<Link :href="route('dashboard')"
:class="{ 'underline' : route().current('collections.index') }">
Collections
</Link>
</li>
<li class="hidden sm:flex">
<Link :href="route('style_guide.show')"
:class="{ 'underline' : route().current('style_guide.show') }">
Style Guides
</Link>
</li>
</ul>
</div>
<div class="navbar-center hidden lg:flex">
Expand Down
9 changes: 3 additions & 6 deletions resources/js/Layouts/MainMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,11 @@ const logout = () => {
<li>
<details>
<summary>
<template v-if="$page.props.jetstream.managesProfilePhotos">
<img class="h-8 w-8 rounded-full object-cover" :src="$page.props.auth.user.profile_photo_url" :alt="$page.props.auth.user.name">
</template>
<template v-else>
{{ $page.props.auth.user.name }}
</template>
Main Menu
</summary>
<ul class="p-2 z-50">
<li class="flex sm:hidden"><Link :href="route('collections.index')">Collections</Link></li>
<li class="flex sm:hidden"><Link :href="route('style_guide.show')">Style Guides</Link></li>
<li><Link :href="route('profile.show')">Profile</Link></li>
<li><Link :href="route('settings.show')">Settings</Link></li>
<li><a :href="route('horizon.index')">Queue</a></li>
Expand Down
44 changes: 35 additions & 9 deletions resources/js/Pages/Chat/ChatInputThreaded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import axios from "axios";
import { useToast } from "vue-toastification";
import { Switch, SwitchGroup, SwitchLabel } from '@headlessui/vue'
import Filters from "@/Pages/Collection/Components/Filters.vue";
import StyleGuide from "@/Pages/Collection/Components/StyleGuide.vue";
const toast = useToast();
Expand All @@ -26,16 +27,23 @@ const form = useForm({
input: "",
completion: false,
tool: "",
filter: null
filter: null,
persona: null
})
const filterChosen = ref({})
const personaChosen = ref({})
const filter = (filter) => {
filterChosen.value = filter;
form.filter = filter?.id
}
const persona = (persona) => {
personaChosen.value = persona;
form.persona = persona?.id;
}
const getting_results = ref(false)
onMounted(() => {
Expand Down Expand Up @@ -66,13 +74,15 @@ const save = () => {
let completion = form.completion
let filter = form.filter
let tool = form.tool
let persona = form.persona
form.reset();
axios.post(route('chats.messages.create', {
chat: props.chat.id
}), {
input: message,
completion: completion,
tool: tool,
persona: persona,
filter: filter
}).catch(error => {
getting_results.value = false
Expand Down Expand Up @@ -148,12 +158,11 @@ const setQuestion = (question) => {
</svg>
</button>
</div>
<div class="flex justify-between gap-4 items-center ml-2">
<div class="flex justify-start gap-2 items-center">
<div class="
justify-start gap-4 items-center ml-2">
<div class="flex justify-start gap-2 items-center ml-1 mb-2">
<div v-if="filterChosen?.name" class="flex justify-start gap-1 items-center">
<span class="text-gray-600">
<span class="text-secondary">
Filter being used: </span>
<span class="font-bold">{{filterChosen.name}}</span>
<button type="button" @click="filter({})">
Expand All @@ -162,9 +171,26 @@ const setQuestion = (question) => {
</svg>
</button>
</div>
<Filters
@filter="filter"
:collection="chat.collection"></Filters>
<div v-if="personaChosen?.name" class="flex justify-start gap-1 items-center">
<span class="text-secondary">
Persona being used: </span>
<span class="font-bold">{{personaChosen.name}}</span>
<button type="button" @click="persona({})">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4">
<path stroke-linecap="round" stroke-linejoin="round" d="m9.75 9.75 4.5 4.5m0-4.5-4.5 4.5M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
</svg>
</button>
</div>
</div>
<div class="flex justify-start gap-2 items-center">
<Filters
@filter="filter"
:collection="chat.collection"></Filters>
<StyleGuide
@persona="persona"
:collection="chat.collection"></StyleGuide>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Collection/Chat.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ onUnmounted(() => {
:chats="chats"></ChatSideNav>
</div>
<div class="col-span-12 sm:col-span-10">

<div class="overflow-hidden shadow-xl sm:rounded-lg">
<div class="px-3">
<div class="flex justify-between items-center">
Expand Down
5 changes: 4 additions & 1 deletion resources/js/Pages/Collection/Components/Documents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ onUnmounted(() => {
{{ document.type }}
</td>
<td>
<div v-if="document.type === 'Json'" class="truncate w-80">{{ document.subject }}</div>
<div v-if="document.type === 'Json'" class="truncate w-80">
{{ document.subject }}
</div>
<div v-else-if="document.subject">
<div class="truncate max-w-2xl">
<div v-if="!document.link">
Expand Down Expand Up @@ -192,6 +194,7 @@ onUnmounted(() => {
{{ document.file_path }}
</a>
</div>
<div class="text-xs text-secondary">updated: {{ document.updated_at_diff }}</div>
</td>

<td>
Expand Down
41 changes: 41 additions & 0 deletions resources/js/Pages/Collection/Components/StyleGuide.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script setup>
import {useToast} from "vue-toastification";
import {computed} from "vue";
import {usePage} from "@inertiajs/vue3";
const toast = useToast();
const emits = defineEmits(['style-guide-persona'])
const props = defineProps({
collection: Object,
})
const personas = computed(() => {
return usePage().props.personas?.data;
})
const getPersona = (filter) => {
emits('persona', filter)
}
</script>
<template>
<details class="dropdown dropdown-top">
<summary class="m-1 btn btn-neutral">
Personas
</summary>
<ul class="p-2 shadow menu dropdown-content z-[1] bg-base-100 rounded-box w-48 ">
<li v-for="persona in personas" :key="persona.id">
<button type="button" @click="getPersona(persona)">{{ persona.name }}</button>
</li>
<li><button type="button" @click="getPersona({})">Reset</button></li>
</ul>
</details>
</template>
<style scoped>
</style>
10 changes: 10 additions & 0 deletions tests/Feature/Models/PersonaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ public function test_model(): void
$model = \App\Models\Persona::factory()->create();
$this->assertNotNull($model->name);
}

public function test_in_persona_wrapped(): void
{
$input = 'Foo bar';
$model = \App\Models\Persona::factory()->create();
$results = $model->wrapPromptInPersona($input);
$this->assertStringContainsString($input, $results);
$this->assertStringContainsString($model->name, $results);
$this->assertStringContainsString($model->content, $results);
}
}
12 changes: 6 additions & 6 deletions tests/fixtures/latest_content_prompt.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Sequi explicabo dolorem repellat qui. Et quia veritatis eos animi. Inventore quia fugit rerum rerum dolores dolorem quibusdam vel. Delectus non atque velit est cupiditate quasi dolores.
Repellat perspiciatis aspernatur aut nihil veniam. Earum voluptas nulla eum nihil reprehenderit sunt officia. Id nisi accusantium nesciunt aut et animi. Cum non corporis temporibus animi voluptatem accusantium amet.
***below is the context to use in your summary***
Beatae commodi officiis quo dolorem corrupti delectus ab voluptatem natus delectus blanditiis consequatur.
Molestiae repellendus numquam recusandae suscipit quis itaque quo laborum.
Vitae qui culpa magnam nihil voluptas officia impedit.
Vel deleniti sunt deserunt culpa consequatur quae optio nisi reprehenderit ea provident vel at.
Eaque ut dolor nemo temporibus eos nisi laborum fugit consectetur nobis sit consequatur.
Qui exercitationem quisquam rerum et maiores soluta culpa voluptatem.
Quia qui sapiente ut doloribus laudantium voluptatem rem enim sunt qui optio.
Ut animi voluptatem commodi quo autem omnis expedita quo ut illum consectetur non.
Impedit autem aliquid repudiandae et illo provident veniam et laborum sed excepturi velit.
Omnis eum sit cupiditate autem laudantium delectus qui repellendus.

0 comments on commit ba615c3

Please sign in to comment.