Skip to content

Commit

Permalink
ready to merge
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Sep 9, 2024
1 parent fda9025 commit 43fa5b9
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 62 deletions.
2 changes: 0 additions & 2 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ public function modifyPayload(array $payload, bool $noTools = false): array
$payload['tools'] = $this->getFunctions();
}


if ($this->system) {
$payload['system'] = $this->system;
}


$payload = $this->addJsonFormat($payload);

return $payload;
Expand Down
26 changes: 13 additions & 13 deletions Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,20 @@ public function remapMessages(array $messages, bool $userLast = false): array
return true;
})
->transform(function (MessageInDto $item) {
/**
* @NOTE
* Claude does not like to end a certain way
*/
$item->content = str(
cleanString($item->content)
)->replaceEnd("\n", '')->trim()->toString();

if (empty($item->content)) {
$item->content = 'See content in thread.';
}
/**
* @NOTE
* Claude does not like to end a certain way
*/
$item->content = str(
cleanString($item->content)
)->replaceEnd("\n", '')->trim()->toString();

if (empty($item->content)) {
$item->content = 'See content in thread.';
}

return $item->toArray();
});
return $item->toArray();
});

/**
* Claude needs me to not use the role tool
Expand Down
6 changes: 1 addition & 5 deletions Modules/LlmDriver/app/Functions/CreateTasksTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace LlmLaraHub\LlmDriver\Functions;

use App\Models\Message;
use App\Models\Project;
use App\Models\Task;
use App\Models\User;
use Facades\App\Domains\Sources\WebSearch\GetPage;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;
use LlmLaraHub\LlmDriver\ToolsHelper;

class CreateTasksTool extends FunctionContract
{
Expand All @@ -26,7 +23,6 @@ class CreateTasksTool extends FunctionContract
ToolTypes::ChatCompletion,
];


public function handle(
Message $message): FunctionResponse
{
Expand All @@ -50,7 +46,7 @@ public function handle(
'details' => $details,
'due_date' => $due_date,
'assistant' => $assistant,
'user_id' => ($user_id !== "" && User::whereId($user_id)->exists()) ? $user_id : null,
'user_id' => ($user_id !== '' && User::whereId($user_id)->exists()) ? $user_id : null,
]);
}

Expand Down
11 changes: 1 addition & 10 deletions Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace LlmLaraHub\LlmDriver\Tests\Feature;

use App\Domains\Chat\DateRangesEnum;
use App\Domains\Chat\MetaDataDto;
use App\Models\Chat;
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Filter;
use App\Models\Message;
use App\Models\Project;
use Illuminate\Support\Facades\File;
use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryFacade;
use LlmLaraHub\LlmDriver\DistanceQuery\Drivers\PostGres;
use LlmLaraHub\LlmDriver\Functions\CreateTasksTool;
use Pgvector\Laravel\Vector;
use Tests\TestCase;

class CreateTasksToolTest extends TestCase
Expand All @@ -38,7 +29,7 @@ public function test_generates_tasks(): void
'chat_id' => $chat->id,
'args' => [
'tasks' => $data,
]
],
]);

$this->assertDatabaseCount('tasks', 0);
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Projects/KickOffProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class KickOffProject
{
public function handle(Project $project)
{
$chat = $project->chats?->first();
$chat = $project->chats()->first();

$chat->updateQuietly([
'chat_status' => UiStatusEnum::InProgress,
Expand Down
1 change: 0 additions & 1 deletion app/Domains/Projects/Orchestrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Domains\Messages\RoleEnum;
use App\Models\Chat;
use App\Models\Project;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Functions\ToolTypes;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
Expand Down
7 changes: 3 additions & 4 deletions app/Domains/Projects/Prompts/CampaignPromptTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class CampaignPromptTemplate
{

public static function systemPrompt(): string {
public static function systemPrompt(): string
{
$now = now()->toISOString();

return <<<PROMPT
Expand Down Expand Up @@ -40,7 +40,7 @@ public static function systemPrompt(): string {
public static function prompt(): string
{

return <<<PROMPT
return <<<'PROMPT'
## Unique Selling Proposition (USP)
[What makes your product/service unique? Why should your target audience choose you over competitors?]
Expand All @@ -67,6 +67,5 @@ public static function prompt(): string

PROMPT;


}
}
14 changes: 7 additions & 7 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace App\Http\Controllers;

use App\Domains\Chat\UiStatusEnum;
use Facades\App\Domains\Projects\KickOffProject;
use App\Domains\Projects\Prompts\CampaignPromptTemplate;
use App\Domains\Projects\StatusEnum;
use App\Http\Resources\ChatResource;
use App\Http\Resources\ProjectResource;
use App\Http\Resources\MessageResource;
use App\Http\Resources\ProjectResource;
use App\Http\Resources\ProjectResourceShow;
use App\Models\Chat;
use App\Models\Project;
use Illuminate\Http\Request;
use Facades\App\Domains\Projects\KickOffProject;
use Facades\App\Domains\Projects\Orchestrate;

class ProjectController extends Controller
Expand All @@ -36,12 +35,12 @@ public function create()
return inertia('Projects/Create', [
'content_start' => [
[
'key' => "Campaign Template",
'key' => 'Campaign Template',
'content' => CampaignPromptTemplate::prompt(),
'system_prompt' => CampaignPromptTemplate::systemPrompt(),
]
],
],
'statuses' => StatusEnum::selectOptions()
'statuses' => StatusEnum::selectOptions(),
]);
}

Expand Down Expand Up @@ -87,7 +86,8 @@ public function show(Project $project)
]);
}

public function showWithChat(Project $project, Chat$chat) {
public function showWithChat(Project $project, Chat $chat)
{

return inertia('Projects/Show', [
'project' => new ProjectResourceShow($project),
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class MessageResource extends JsonResource
public function toArray(Request $request): array
{
$user = [];
if($this->user_id) {
if ($this->user_id) {
$user = new UserResource($this->user);
}

return [
'id' => $this->id,
'role' => $this->role->value,
Expand Down
10 changes: 6 additions & 4 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Chat extends Model implements HasDrivers

public function getDriver(): string
{
if($this->chat_driver) {
if ($this->chat_driver) {
return $this->chat_driver->value;
}

Expand Down Expand Up @@ -71,9 +71,10 @@ public function getChatable(): HasDrivers

public function getEmbeddingDriver(): string
{
if($this->embedding_driver) {
if ($this->embedding_driver) {
return $this->embedding_driver->value;
}

return $this->chatable->getEmbeddingDriver();
}

Expand Down Expand Up @@ -144,7 +145,7 @@ public function addInputWithTools(
string $message,
mixed $tool_id,
mixed $tool_name,
mixed $tool_args) : Message
mixed $tool_args): Message
{

return DB::transaction(function () use ($message, $tool_id, $tool_name, $tool_args) {
Expand Down Expand Up @@ -192,7 +193,8 @@ public function getAiResponse($input)
return $this->getChatResponse();
}

public function getMessageThread(int $limit = 5): array {
public function getMessageThread(int $limit = 5): array
{
return $this->getChatResponse($limit);
}

Expand Down
4 changes: 1 addition & 3 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ public function getFromAiAttribute(): bool
return $this->role !== RoleEnum::User;
}

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


public function scopeNotSystem(Builder $query)
{
return $query->where('role', '!=', RoleEnum::System->value);
}


public function scopeNotTool(Builder $query)
{
return $query->where('role', '!=', RoleEnum::Tool->value);
Expand Down
2 changes: 0 additions & 2 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use LlmLaraHub\LlmDriver\HasDrivers;
use LlmLaraHub\LlmDriver\HasDriversTrait;

class Project extends Model implements HasDrivers
{
Expand All @@ -34,7 +33,6 @@ public function tasks(): HasMany
return $this->hasMany(Task::class);
}


public function chats(): MorphMany
{
return $this->morphMany(Chat::class, 'chatable');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public function up(): void
{
Schema::table('chats', function (Blueprint $table) {
$table->string("chat_status")
$table->string('chat_status')
->nullable()
->default(\App\Domains\Chat\UiStatusEnum::NotStarted->value);
});
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/Http/Controllers/ProjectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function test_update(): void

$this->actingAs($user)->put(
route('projects.update', $project), [
"name" => "Test Campaign 2",
'name' => 'Test Campaign 2',
'start_date' => '2023-01-01',
'system_prompt' => 'Test Description',
'end_date' => '2023-01-01',
Expand All @@ -154,5 +154,4 @@ public function test_destroy(): void
)->assertSessionHasNoErrors()
->assertStatus(302);
}

}
9 changes: 5 additions & 4 deletions tests/Feature/Models/ChatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ public function test_factory(): void
$this->assertNotNull($collection->chats()->first()->id);
}

public function test_default_driver() {
public function test_default_driver()
{
$project = Project::factory()->create();

$chat = Chat::factory()->create([
'chatable_id' => $project->id,
"chatable_type" => Project::class,
"chat_driver" => DriversEnum::Claude,
"embedding_driver" => DriversEnum::Ollama,
'chatable_type' => Project::class,
'chat_driver' => DriversEnum::Claude,
'embedding_driver' => DriversEnum::Ollama,
]);

$this->assertEquals(DriversEnum::Claude->value, $chat->getDriver());
Expand Down
3 changes: 1 addition & 2 deletions tests/Feature/ProjectOrchestrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ public function test_tools(): void
CompletionResponse::from($response)
);


LlmDriverFacade::shouldReceive('driver->chat')
->once()
->andReturn(
CompletionResponse::from($response)
);

(new Orchestrate())->handle($chat, 'Test Prompt', "System Prompt");
(new Orchestrate())->handle($chat, 'Test Prompt', 'System Prompt');

$this->assertDatabaseCount('messages', 4);
$this->assertDatabaseCount('tasks', 5);
Expand Down

0 comments on commit 43fa5b9

Please sign in to comment.