Skip to content

Commit

Permalink
Will come back to this
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Sep 9, 2024
1 parent 36046e4 commit 736be0e
Show file tree
Hide file tree
Showing 49 changed files with 1,376 additions and 96 deletions.
10 changes: 10 additions & 0 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use LlmLaraHub\LlmDriver\Functions\Chat;
use LlmLaraHub\LlmDriver\Functions\CreateDocument;
use LlmLaraHub\LlmDriver\Functions\CreateEventTool;
use LlmLaraHub\LlmDriver\Functions\CreateTasksTool;
use LlmLaraHub\LlmDriver\Functions\FunctionContract;
use LlmLaraHub\LlmDriver\Functions\FunctionDto;
use LlmLaraHub\LlmDriver\Functions\GatherInfoTool;
Expand All @@ -29,6 +30,8 @@ abstract class BaseClient

protected int $poolSize = 3;

protected ?string $system = null;

protected bool $limitByShowInUi = false;

protected ToolTypes $toolType = ToolTypes::NoFunction;
Expand Down Expand Up @@ -66,6 +69,7 @@ public function getFunctions(): array
new CreateEventTool(),
//new CreateDocument(),
new SatisfyToolsRequired(),
new CreateTasksTool(),
//new Chat(),
]
);
Expand Down Expand Up @@ -117,6 +121,12 @@ 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
35 changes: 20 additions & 15 deletions Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Setting;
use Illuminate\Http\Client\Pool;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -345,11 +346,17 @@ public function remapMessages(array $messages, bool $userLast = false): array
/**
* Claude needs to not start with a system message
*/
$messages = collect($messages)->transform(function ($item) {
if ($item->role === 'system') {
$item->role = 'assistant';
}
$messages = collect($messages)
->filter(function ($item) {
if ($item->role === 'system') {
$this->system = $item->content;

return false;
}

return true;
})
->transform(function (MessageInDto $item) {
/**
* @NOTE
* Claude does not like to end a certain way
Expand Down Expand Up @@ -378,11 +385,6 @@ public function remapMessages(array $messages, bool $userLast = false): array
$toolId = data_get($item, 'tool_id', 'toolu_'.Str::random(32));
$tool = data_get($item, 'tool', 'unknown_tool');
$args = data_get($item, 'args', '{}');
Log::info('Claude Tool Found', [
'tool' => $tool,
'tool_id' => $toolId,
'args' => $args,
]);

$content = $item['content'];

Expand Down Expand Up @@ -452,12 +454,15 @@ public function remapMessages(array $messages, bool $userLast = false): array

}

$lastMessage = end($newMessagesArray);
if ($lastMessage['role'] !== 'user') {
$newMessagesArray[] = [
'role' => 'user',
'content' => 'Using the surrounding context to continue this response thread',
];
if ($userLast) {
$last = Arr::last($newMessagesArray);

if ($last['role'] === 'assistant') {
$newMessagesArray[] = [
'role' => 'user',
'content' => 'Using the surrounding context to continue this response thread',
];
}
}

return $newMessagesArray;
Expand Down
113 changes: 113 additions & 0 deletions Modules/LlmDriver/app/Functions/CreateTasksTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace LlmLaraHub\LlmDriver\Functions;

use App\Models\Message;
use App\Models\Project;
use App\Models\Task;
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
{
protected string $name = 'create_tasks_tool';

protected string $description = 'If the Campaign needs to have tasks created or the users prompt requires it you can use this tool to make multiple tasks';

public bool $showInUi = true;

public array $toolTypes = [
ToolTypes::Source,
ToolTypes::Output,
ToolTypes::Chat,
ToolTypes::ChatCompletion,
];


public function handle(
Message $message,
array $args = []): FunctionResponse
{
Log::info('TaskTool called');

foreach (data_get($args, 'tasks', []) as $taskArg) {
$name = data_get($taskArg, 'name', null);
$details = data_get($taskArg, 'details', null);
$due_date = data_get($taskArg, 'due_date', null);
$assistant = data_get($taskArg, 'assistant', false);
$user_id = data_get($taskArg, 'user_id', null);

$project = $message->chat->getChatable();

Task::updateOrCreate([
'name' => $name,
'project_id' => $project->id,
],
[
'details' => $details,
'due_date' => $due_date,
'assistant' => $assistant,
'user_id' => $user_id,
]);
}

return FunctionResponse::from([
'content' => json_encode($args),
]);
}

/**
* @return PropertyDto[]
*/
protected function getProperties(): array
{
return [
new PropertyDto(
name: 'tasks',
description: 'Array of task objects',
type: 'array',
required: true,
properties: [
new PropertyDto(
name: 'items',
description: 'Task object',
type: 'object',
required: true,
properties: [
new PropertyDto(
name: 'name',
description: 'Name of the task',
type: 'string',
required: true
),
new PropertyDto(
name: 'details',
description: 'Detailed info of the task',
type: 'string',
required: true
),
new PropertyDto(
name: 'due_date',
description: 'Due date if any format "Y-m-d"',
type: 'string',
required: true
),
new PropertyDto(
name: 'assistant',
description: 'Should the assistant be assigned this true or false',
type: 'string',
),
new PropertyDto(
name: 'user_id',
description: 'User id if assigned to a user',
type: 'string',
),
]
),
]
),
];
}
}
2 changes: 2 additions & 0 deletions Modules/LlmDriver/app/LlmDriverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LlmLaraHub\LlmDriver;

use LlmLaraHub\LlmDriver\Functions\CreateTasksTool;
use LlmLaraHub\LlmDriver\Functions\GatherInfoTool;
use LlmLaraHub\LlmDriver\Functions\ReportingTool;
use LlmLaraHub\LlmDriver\Functions\SearchAndSummarize;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function getFunctions(): array
(new StandardsChecker())->getFunction(),
(new ReportingTool())->getFunction(),
(new GatherInfoTool())->getFunction(),
(new CreateTasksTool())->getFunction(),
];
}

Expand Down
5 changes: 5 additions & 0 deletions Modules/LlmDriver/app/LlmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\ServiceProvider;
use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryClient;
use LlmLaraHub\LlmDriver\Functions\CreateEventTool;
use LlmLaraHub\LlmDriver\Functions\CreateTasksTool;
use LlmLaraHub\LlmDriver\Functions\GatherInfoTool;
use LlmLaraHub\LlmDriver\Functions\GetWebSiteFromUrlTool;
use LlmLaraHub\LlmDriver\Functions\ReportingTool;
Expand Down Expand Up @@ -98,6 +99,10 @@ public function boot(): void
return new CreateEventTool();
});

$this->app->bind('create_tasks_tool', function () {
return new CreateTasksTool();
});

}

/**
Expand Down
56 changes: 56 additions & 0 deletions Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

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
{
/**
* A basic feature test example.
*/
public function test_generates_tasks(): void
{
$project = Project::factory()->create();
$chat = Chat::factory()->create([
'chatable_id' => $project->id,
'chatable_type' => Project::class,
]);

$message = Message::factory()->create([
'chat_id' => $chat->id,
]);


$data = get_fixture('claude_chat_response.json');

$data = data_get($data, 'tool_calls.1.arguments.tasks');

$this->assertDatabaseCount('tasks', 0);


(new CreateTasksTool())->handle($message, [
'tasks' => $data,
]);


$this->assertDatabaseCount('tasks', 5);

$this->assertCount(5, $project->refresh()->tasks);

}

}
2 changes: 2 additions & 0 deletions app/Domains/Chat/UiStatusEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
enum UiStatusEnum: string
{
case Complete = 'complete';
case InProgress = 'in_progress';
case NotStarted = 'not_started';
}
29 changes: 13 additions & 16 deletions app/Domains/Projects/KickOffProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@

namespace App\Domains\Projects;

use App\Domains\Campaigns\CampaignKickOffPrompt;
use App\Domains\Campaigns\ChatStatusEnum;
use App\Models\Campaign;
use Facades\App\Services\LlmServices\Orchestration\Orchestrate;
use App\Domains\Chat\UiStatusEnum;
use App\Models\Project;
use Facades\App\Domains\Projects\Orchestrate;

class KickOffProject
{
public function handle(Campaign $campaign)
public function handle(Project $project)
{
$campaign->updateQuietly([
'chat_status' => ChatStatusEnum::InProgress,
]);

$campaign->messages()->delete();
$chat = $project->chats?->first();

$campaign->tasks()->delete();
$chat->updateQuietly([
'chat_status' => UiStatusEnum::InProgress,
]);

$campaignContext = $campaign->getContext();
$chat->messages()->delete();

$prompt = CampaignKickOffPrompt::getPrompt($campaignContext);
$project->tasks()->delete();

Orchestrate::handle($campaign, $prompt);
Orchestrate::handle($chat, $project->content, $project->system_prompt);

$campaign->updateQuietly([
'chat_status' => ChatStatusEnum::Complete,
$chat->updateQuietly([
'chat_status' => UiStatusEnum::Complete,
]);
}
}
Loading

0 comments on commit 736be0e

Please sign in to comment.