Skip to content

Commit

Permalink
api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 16, 2024
1 parent f143c9a commit aa8e06c
Show file tree
Hide file tree
Showing 15 changed files with 379 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Functions\Chat;
use LlmLaraHub\LlmDriver\Functions\CreateDocument;
use LlmLaraHub\LlmDriver\Functions\CreateEventTool;
use LlmLaraHub\LlmDriver\Functions\FunctionContract;
use LlmLaraHub\LlmDriver\Functions\FunctionDto;
use LlmLaraHub\LlmDriver\Functions\GatherInfoTool;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function getFunctions(): array
new GatherInfoTool(),
new GetWebSiteFromUrlTool(),
new SearchTheWeb(),
new CreateEventTool(),
//new CreateDocument(),
new SatisfyToolsRequired(),
//new Chat(),
Expand Down
2 changes: 2 additions & 0 deletions Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function chat(array $messages): CompletionResponse

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

put_fixture('claude_payload_with_tools.json', $payload);

$results = $this->getClient()->post('/messages', $payload);

if (! $results->ok()) {
Expand Down
96 changes: 96 additions & 0 deletions Modules/LlmDriver/app/Functions/CreateEventTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace LlmLaraHub\LlmDriver\Functions;

use App\Models\Event;
use App\Models\Message;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;
use LlmLaraHub\LlmDriver\ToolsHelper;

class CreateEventTool extends FunctionContract
{
use ToolsHelper;

public bool $showInUi = false;

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

protected string $name = 'create_event_tool';

protected string $description = 'If the user needs to create an event this tool help';

public function handle(
Message $message): FunctionResponse
{
Log::info('[LaraChain] CreateEventTool called');

$args = $message->meta_data->args;

$assigned_to_assistant = data_get($args, 'assigned_to_assistant', false);
$description = data_get($args, 'description', null);
$start_date = data_get($args, 'start_date', null);
$start_time = data_get($args, 'start_time', null);
$end_date = data_get($args, 'end_date', null);
$end_time = data_get($args, 'end_time', null);
$location = data_get($args, 'location', null);
$type = data_get($args, 'type', 'event');
$title = data_get($args, 'title', null);
$all_day = data_get($args, 'all_day', false);

if (! $title || ! $start_date) {
throw new \Exception('No title found');
}

if ($start_date == null && $start_date != '') {
$start_date = str($start_date)->remove('\\')->toString();
} else {
$start_date = null;
}

if ($end_date && $end_date !== '') {
$end_date = str($end_date)->remove('\\')->toString();
} else {
$end_date = null;
}

$event = Event::create([
'title' => $title,
'description' => $description,
'start_date' => $start_date,
'start_time' => $start_time,
'end_date' => $end_date,
'end_time' => $end_time,
'location' => $location,
'type' => $type,
'assigned_to_id' => null,
'assigned_to_assistant' => $assigned_to_assistant,
'all_day' => $all_day,
'collection_id' => $message->getChatable()->id,
]);

return FunctionResponse::from([
'content' => $event->title,
'prompt' => $message->getContent(),
'requires_followup' => false,
'documentChunks' => collect([]),
'save_to_message' => false,
]);
}

/**
* @return PropertyDto[]
*/
protected function getProperties(): array
{
return [];
}

public function runAsBatch(): bool
{
return false;
}
}
1 change: 1 addition & 0 deletions Modules/LlmDriver/app/Functions/PropertyDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function __construct(
public string $description,
public string $type = 'string',
public array $enum = [],
public array $properties = [],
public string $default = '',
public bool $required = false,
) {
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 @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryClient;
use LlmLaraHub\LlmDriver\Functions\CreateEventTool;
use LlmLaraHub\LlmDriver\Functions\GatherInfoTool;
use LlmLaraHub\LlmDriver\Functions\GetWebSiteFromUrlTool;
use LlmLaraHub\LlmDriver\Functions\ReportingTool;
Expand Down Expand Up @@ -93,6 +94,10 @@ public function boot(): void
return new SatisfyToolsRequired();
});

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

}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Domains/Orchestration/OrchestrateVersionTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public function sourceOrchestrate(Chat $chat, string $prompt): Message
foreach ($response->tool_calls as $tool_call) {
Log::info('[LaraChain] - Tool Call '.$count, [
'tool_call' => $tool_call->name,
'tool_count' => count($response->tool_calls),
]);

$message = $chat->addInput(
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/ApiChromeExtensionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ public function createSource(Collection $collection)
$validated = request()->validate([
'url' => 'required|string',
'recurring' => 'required|string',
'title' => 'required|string',
'force' => 'required|boolean',
'prompt' => 'required|string',
'content' => 'required|string',
]);

$source = Source::create([
'title' => str($validated['url'])->after('://')->toString(),
'title' => $validated['title'],
'details' => $validated['prompt'],
'recurring' => data_get($validated, 'recurring', 'not'),
'active' => 1,
Expand Down
8 changes: 6 additions & 2 deletions app/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class Event extends Model

protected $casts = [
'type' => EventTypes::class,
'start_date' => 'datetime',
'end_date' => 'datetime',
'start_date' => 'date',
'start_time' => 'timestamp',
'all_day' => 'boolean',
'assigned_to_assistant' => 'boolean',
'end_date' => 'date',
'end_time' => 'timestamp',
];

public function collection(): BelongsTo
Expand Down
8 changes: 6 additions & 2 deletions database/factories/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ public function definition(): array
return [
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph,
'start_date' => $this->faker->dateTime,
'end_date' => $this->faker->dateTime,
'start_date' => $this->faker->date,
'start_time' => $this->faker->time,
'end_date' => $this->faker->date,
'end_time' => $this->faker->time,
'location' => $this->faker->sentence,
'type' => \App\Domains\Events\EventTypes::Event,
'assigned_to_id' => User::factory(),
'assigned_to_assistant' => false,
'all_day' => false,
'collection_id' => Collection::factory(),
];
}
Expand Down
8 changes: 6 additions & 2 deletions database/migrations/2024_08_13_235332_create_events_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ public function up(): void
$table->id();
$table->string('title');
$table->longText('description')->nullable();
$table->dateTime('start_date')->nullable();
$table->dateTime('end_date')->nullable();
$table->date('start_date')->nullable();
$table->time('start_time')->nullable();
$table->date('end_date')->nullable();
$table->time('end_time')->nullable();
$table->string('location')->nullable();
$table->boolean('assigned_to_assistant')->default(false);
$table->boolean('all_day')->default(false);
$table->string('type')
->default(\App\Domains\Events\EventTypes::Event->value);
$table->foreignIdFor(\App\Models\User::class, 'assigned_to_id')->nullable();
Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/CreateEventToolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Feature;

use App\Domains\Chat\MetaDataDto;
use App\Models\Message;
use LlmLaraHub\LlmDriver\Functions\CreateEventTool;
use Tests\TestCase;

class CreateEventToolTest extends TestCase
{
public function test_makes_event(): void
{
$data = get_fixture('create_event_tool.json');
$message = Message::factory()->create([
'meta_data' => MetaDataDto::from([
'args' => $data['args'],
]),
]);
$this->assertDatabaseCount('events', 0);
(new CreateEventTool())->handle($message);
$this->assertDatabaseCount('events', 1);

}
}
Loading

0 comments on commit aa8e06c

Please sign in to comment.