Skip to content

Commit

Permalink
this has the audience working
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 20, 2024
1 parent ac1517a commit 991a7c4
Show file tree
Hide file tree
Showing 19 changed files with 550 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function chat(array $messages): CompletionResponse
$model = $this->getConfig('claude')['models']['completion_model'];
$maxTokens = $this->getConfig('claude')['max_tokens'];

Log::info('LlmDriver::Claude::completion');
Log::info('LlmDriver::Claude::chat');

/**
* I need to iterate over each item
Expand All @@ -42,6 +42,8 @@ public function chat(array $messages): CompletionResponse
*/
$messages = $this->remapMessages($messages);

put_fixture('after_mapping.json', $messages);

$results = $this->getClient()->post('/messages', [
'model' => $model,
'system' => 'Return a markdown response.',
Expand All @@ -52,6 +54,7 @@ public function chat(array $messages): CompletionResponse
if (! $results->ok()) {
$error = $results->json()['error']['type'];
$message = $results->json()['error']['message'];
put_fixture('claude_error.json', $results->json());
Log::error('Claude API Error ', [
'type' => $error,
'message' => $message,
Expand Down Expand Up @@ -303,6 +306,8 @@ protected function remapMessages(array $messages): array
$item->role = 'assistant';
}

$item->content = str($item->content)->replaceEnd("\n", '')->trim()->toString();

return $item->toArray();
})
->values();
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use App\Domains\Messages\RoleEnum;
use App\Events\ChatUiUpdateEvent;
use App\Http\Resources\AudienceResource;
use App\Http\Resources\ChatResource;
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\Audience;
use App\Models\Chat;
use App\Models\Collection;
use App\Models\Filter;
Expand Down Expand Up @@ -47,6 +49,7 @@ public function showCollectionChat(Collection $collection, Chat $chat)
'chats' => ChatResource::collection($collection->chats()->latest()->paginate(20)),
'filters' => FilterResource::collection($collection->filters),
'personas' => PersonaResource::collection(Persona::all()),
'audiences' => AudienceResource::collection(Audience::all()),
'system_prompt' => $collection->systemPrompt(),
'settings' => [
'supports_functions' => LlmDriverFacade::driver($chat->getDriver())->hasFunctions(),
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Controllers/StyleGuideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Http\Controllers;

use App\Http\Resources\AudienceResource;
use App\Http\Resources\PersonaResource;
use App\Models\Audience;
use App\Models\Persona;
use App\Models\Setting;
use Illuminate\Http\Request;
Expand All @@ -15,9 +17,38 @@ public function show()

return inertia('StyleGuide/Show', [
'personas' => PersonaResource::collection(Persona::all()),
'audiences' => AudienceResource::collection(Audience::all()),
]);
}

public function updateAudience(Request $request, Audience $audience)
{
$validated = $request->validate([
'name' => 'string|required',
'content' => 'string|required',
]);

$audience->update($validated);

request()->session()->flash('flash.banner', 'Audience updated');

return back();
}

public function createAudience(Request $request)
{
$validated = $request->validate([
'name' => 'string|required',
'content' => 'string|required',
]);

Audience::create($validated);

request()->session()->flash('flash.banner', 'Audience Created');

return back();
}

public function updatePersona(Request $request, Persona $persona)
{
$validated = $request->validate([
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Resources/AudienceResource.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 AudienceResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
31 changes: 31 additions & 0 deletions app/Models/Audience.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Audience extends Model
{
use HasFactory;

protected $guarded = [];

public function wrapPromptInAudience(string $prompt): string
{

$title = $this->name;
$audience = $this->content;

return <<<PROMPT
$prompt
**AUDIENCE INFO**
Name of Audience: $title
Details ofAudience:
$audience
**AUDIENCE INFO**
PROMPT;

}
}
31 changes: 17 additions & 14 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\DB;
use LlmLaraHub\LlmDriver\HasDrivers;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use OpenAI\Laravel\Facades\OpenAI;
Expand Down Expand Up @@ -87,22 +88,24 @@ public function addInput(string $message,
bool $show_in_thread = true): Message
{

if ($systemPrompt) {
$this->createSystemMessageIfNeeded($systemPrompt);
}
return DB::transaction(function () use ($message, $role, $systemPrompt, $show_in_thread) {

$message = $this->messages()->create(
[
'body' => $message,
'role' => $role,
'in_out' => ($role === RoleEnum::User) ? true : false,
'created_at' => now(),
'updated_at' => now(),
'chat_id' => $this->id,
'is_chat_ignored' => ! $show_in_thread,
]);
if ($systemPrompt) {
$this->createSystemMessageIfNeeded($systemPrompt);
}

return $this->messages()->create(
[
'body' => $message,
'role' => $role,
'in_out' => ($role === RoleEnum::User) ? true : false,
'created_at' => now(),
'updated_at' => now(),
'chat_id' => $this->id,
'is_chat_ignored' => ! $show_in_thread,
]);
});

return $message;
}

/**
Expand Down
24 changes: 24 additions & 0 deletions database/factories/AudienceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Audience>
*/
class AudienceFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => $this->faker->name,
'content' => $this->faker->text,
];
}
}
29 changes: 29 additions & 0 deletions database/migrations/2024_06_20_201901_create_audiences_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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::create('audiences', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->longText('content');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('audiences');
}
};
4 changes: 2 additions & 2 deletions resources/js/Pages/Chat/ChatBaloon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ const props = defineProps({
</TabGroup>
</div>
<div v-else
class="bg-base-100 flex rounded-md shadow-lg shadow-inner-custom p-4 prose "
class="bg-base-100 flex rounded-md shadow-lg shadow-inner-custom p-4 "
:class="message.from_ai ? 'rounded-tr-none' : 'flex-row-reverse'">
<div class="grow"
<div class="grow leading-loose"
:class="message.from_ai ? 'rounded-tr-none' : 'rounded-tl-none'"
v-html="message.body_markdown">
</div>
Expand Down
15 changes: 13 additions & 2 deletions resources/js/Pages/Chat/ChatInputThreaded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const persona = (persona) => {
form.persona = persona?.id;
}
const audience = (audience) => {
form.input = form.input + "\n" + audience?.content;
toast.info('Audience added to your prompt!', {
position: 'bottom-right'
});
}
const getting_results = ref(false)
onMounted(() => {
Expand Down Expand Up @@ -130,7 +138,8 @@ const setQuestion = (question) => {
disabled:opacity-40
bg-transparent block w-full border-0 py-1.5 ring-inset
ring-secondary placeholder:text-gray-400 ring-2
focus:ring-pink-500 sm:text-sm sm:leading-6"
text-xl
focus:ring-pink-500 placeholder:text-xl"
v-model="form.input" placeholder="Chat about your Collection"/>
<span
Expand Down Expand Up @@ -190,7 +199,9 @@ const setQuestion = (question) => {
:collection="chat.collection"></Filters>
<StyleGuide
@persona="persona"
:collection="chat.collection"></StyleGuide>
@audience="audience"
:collection="chat.collection">
</StyleGuide>
</div>
</div>
Expand Down
28 changes: 26 additions & 2 deletions resources/js/Pages/Collection/Components/StyleGuide.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<script setup>
import {useToast} from "vue-toastification";
import {computed} from "vue";
import {computed, ref} from "vue";
import {usePage} from "@inertiajs/vue3";
const toast = useToast();
const emits = defineEmits(['style-guide-persona'])
const emits = defineEmits(['style-guide-persona', 'persona', 'audience', 'style-guide-audience'])
const props = defineProps({
collection: Object,
})
const audiences = computed(() => {
return usePage().props.audiences?.data;
})
const personas = computed(() => {
return usePage().props.personas?.data;
})
Expand All @@ -19,6 +24,13 @@ const getPersona = (filter) => {
emits('persona', filter)
}
const openAudience = ref(null);
const getAudience = (filter) => {
emits('audience', filter)
openAudience.value.removeAttribute('open')
}
</script>
<template>
Expand All @@ -34,6 +46,18 @@ const getPersona = (filter) => {
</ul>
</details>
<!-- how do I remove the open attribute from details after they click li -->
<details ref="openAudience" class="dropdown dropdown-top">
<summary class="m-1 btn btn-neutral">
Audiences
</summary>
<ul class="p-2 shadow menu dropdown-content z-[1] bg-base-100 rounded-box w-48 ">
<li v-for="audience in audiences" :key="audience.id">
<button type="button" @click="getAudience(audience)">{{ audience.name }}</button>
</li>
</ul>
</details>
</template>
<style scoped>
Expand Down
Loading

0 comments on commit 991a7c4

Please sign in to comment.