Skip to content

Commit

Permalink
ok all connected
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Sep 17, 2023
1 parent 564d529 commit dcd9d69
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 75 deletions.
13 changes: 5 additions & 8 deletions app/Domains/LlmFunctions/GetExistingTags/GetExistingTags.php
Expand Up @@ -4,15 +4,12 @@

use App\Domains\LlmFunctions\Dto\RoleTypeEnum;
use App\Domains\LlmFunctions\LlmFunctionContract;
use Facades\App\Domains\Tagging\TagModelDescription;
use App\Models\Message;
use App\OpenAi\Dtos\FunctionCallDto;
use Illuminate\Support\Facades\DB;

class GetExistingTags extends LlmFunctionContract
{


public function handle(FunctionCallDto $functionCallDto): Message
{

Expand All @@ -25,18 +22,18 @@ public function handle(FunctionCallDto $functionCallDto): Message
*/
$results = DB::select($functionCallDto->arguments['query']);

$results = collect($results)->map(function($item) {
return (array) $item;
})->map(function ($item){
$results = collect($results)->map(function ($item) {
return (array) $item;
})->map(function ($item) {
$order = array_keys($item);
$item = collect($item)->only($order)->toArray();

return collect($item)->map(function ($value, $key) {
return "{$key}: {$value}";
})->implode(', ');
})->implode("\n");


$message = sprintf("Results of the query of tags table %s %s",
$message = sprintf('Results of the query of tags table %s %s',
$functionCallDto->arguments['query'],
$results
);
Expand Down
39 changes: 29 additions & 10 deletions app/Domains/LlmFunctions/TagArticle/TagArticle.php
Expand Up @@ -4,29 +4,48 @@

use App\Domains\LlmFunctions\Dto\RoleTypeEnum;
use App\Domains\LlmFunctions\LlmFunctionContract;
use Facades\App\Domains\Tagging\TagModelDescription;
use App\Models\Message;
use App\Models\Tag;
use App\OpenAi\Dtos\FunctionCallDto;

class TagArticle extends LlmFunctionContract
{


public function handle(FunctionCallDto $functionCallDto): Message
{
/**
* @NOTE
* For now I assume it is the tags table
* I can do more later like all or Todos etc
*/
$tags = TagModelDescription::getTagsTableInfo();

$existingTags = [];
$newTags = [];

$message = $functionCallDto->message;

foreach (data_get($functionCallDto->arguments, 'tags', []) as $tag) {
if ($tag_id = data_get($tag, 'id')) {
$tagModel = Tag::findOrFail($tag_id);
$existingTags[] = $tagModel->label;
} else {
$tagModel = Tag::create([
'label' => data_get($tag, 'label'),
'active' => true,
]);
$newTags[] = $tagModel->label;
}

$message->tags()->attach([
$tagModel->id,
]);
}

$content = sprintf('Existing Tags added %s and new tags added %s',
implode(',', $existingTags),
implode(',', $newTags)
);

return Message::create(
[
'parent_id' => $functionCallDto->message->id,
'role' => RoleTypeEnum::Function,
'user_id' => $functionCallDto->message->user_id,
'content' => $tags,
'content' => $content,
'name' => $functionCallDto->function_name,
]
);
Expand Down
13 changes: 5 additions & 8 deletions app/Domains/Tagging/TagModelDescription.php
Expand Up @@ -8,26 +8,23 @@ class TagModelDescription
{
/**
* Get column names for a table.
*
* @param string $tableName
* @return array
*/
public function getColumnNames(string $tableName): array {
public function getColumnNames(string $tableName): array
{
return Schema::getColumnListing($tableName);
}

/**
* Get info for the tags table.
*
* @return array
*/
public function getTagsTableInfo(): array {
public function getTagsTableInfo(): array
{
$tableName = 'tags';
$columnNames = $this->getColumnNames($tableName);

return [
'table_name' => $tableName,
'column_names' => $columnNames
'column_names' => $columnNames,
];
}
}
23 changes: 13 additions & 10 deletions app/Jobs/MailBoxParserJob.php
Expand Up @@ -44,13 +44,6 @@ public function handle(): void
'active' => 1,
]);

/**
* @TODO
* The emails can have triggers like #tldr or #get_content
* Since this is my most common use case I will just assume this
*/
$function = LlmFunction::whereLabel('get_content_from_url')->first();

$content = $this->mailDto->body;

/**
Expand All @@ -73,9 +66,19 @@ public function handle(): void
$tag1->id, $tag2->id,
]);

$message->llm_functions()->attach([
$function->id,
]);
/**
* @NOTE
* These are the functions I want to use
*/
$functions = ['get_content_from_url', 'add_tags_to_article', 'get_existing_tags'];

foreach ($functions as $function) {
$functionModel = LlmFunction::whereLabel('get_content_from_url')->first();

$message->llm_functions()->attach([
$functionModel->id,
]);
}

MessageCreatedJob::dispatch($message);

Expand Down
3 changes: 2 additions & 1 deletion app/Models/Tag.php
Expand Up @@ -8,6 +8,7 @@

/**
* @property string $label
* @property int $id
* @property bool $active
*/
class Tag extends Model
Expand All @@ -17,7 +18,7 @@ class Tag extends Model
protected $guarded = [];

protected $casts = [
'active' => "boolean"
'active' => 'boolean',
];

public function messages(): BelongsToMany
Expand Down
12 changes: 10 additions & 2 deletions app/helpers.php
@@ -1,10 +1,11 @@
<?php

use Facades\App\Domains\LlmFunctions\GetExistingTags\GetExistingTags;
use App\Models\Message;
use App\OpenAi\Dtos\FunctionCallDto;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoice;
use Facades\App\Domains\LlmFunctions\GetContentFromUrl\GetContentFromUrl;
use Facades\App\Domains\LlmFunctions\GetExistingTags\GetExistingTags;
use Facades\App\Domains\LlmFunctions\TagArticle\TagArticle;
use Facades\App\Domains\LlmFunctions\TextToImage\TextToImage;
use Facades\App\Domains\Scheduling\TaskRepository;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -37,7 +38,6 @@ function content_to_voice(
}
}


if (! function_exists('get_existing_tags')) {
function get_existing_tags(
FunctionCallDto $functionCallDto
Expand All @@ -46,6 +46,14 @@ function get_existing_tags(
}
}

if (! function_exists('add_tags_to_article')) {
function add_tags_to_article(
FunctionCallDto $functionCallDto
): Message {
return TagArticle::handle($functionCallDto);
}
}

if (! function_exists('get_content_from_url')) {
function get_content_from_url(
FunctionCallDto $functionCallDto
Expand Down
9 changes: 4 additions & 5 deletions database/seeders/AddFunctionTagArticle.php
Expand Up @@ -3,7 +3,6 @@
namespace Database\Seeders;

use App\Models\LlmFunction;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class AddFunctionTagArticle extends Seeder
Expand All @@ -13,11 +12,11 @@ class AddFunctionTagArticle extends Seeder
*/
public function run(): void
{
$parameters = get_fixture_v2('get_tags_function_parameters.json', false);
$parameters = get_fixture_v2('tag_article_function_parameters.json', false);
LlmFunction::create([
'label' => "get_existing_tags",
'description' => "Use this function to get existing tags. Input should be a fully formed MySQL query.",
'parameters' => $parameters
'label' => 'add_tags_to_article',
'description' => 'Use this function to add tags to an article.',
'parameters' => $parameters,
]);
}
}
22 changes: 22 additions & 0 deletions database/seeders/GetExistingTagsSeeder.php
@@ -0,0 +1,22 @@
<?php

namespace Database\Seeders;

use App\Models\LlmFunction;
use Illuminate\Database\Seeder;

class GetExistingTagsSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$parameters = get_fixture_v2('get_tags_function_parameters.json', false);
LlmFunction::create([
'label' => 'get_existing_tags',
'description' => 'Use this function to get existing tags. Input should be a fully formed MySQL query.',
'parameters' => $parameters,
]);
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Expand Up @@ -18,7 +18,7 @@
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="RAPID_API_TOKEN" value="foobar"/>
<!-- <env name="OPENAI_MOCK" value="true"/>-->
<env name="OPENAI_MOCK" value="true"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="OPENAI_MOCK" value="true"/>
Expand Down
2 changes: 0 additions & 2 deletions tests/Feature/GetContentFromUrlTest.php
Expand Up @@ -38,6 +38,4 @@ public function test_get_content()
$this->assertEquals(RoleTypeEnum::Function, $message->role);
$this->assertEquals('some_function_name', $message->name);
}


}
16 changes: 8 additions & 8 deletions tests/Feature/GetExistingTagsTest.php
Expand Up @@ -2,21 +2,21 @@

namespace Tests\Feature;

use App\Models\Tag;
use Facades\App\Domains\LlmFunctions\GetExistingTags\GetExistingTags;
use App\Models\Message;
use App\Models\Tag;
use App\OpenAi\Dtos\FunctionCallDto;
use Database\Seeders\AddFunctionTagArticle;
use Database\Seeders\GetExistingTagsSeeder;
use Facades\App\Domains\LlmFunctions\GetExistingTags\GetExistingTags;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GetExistingTagsTest extends TestCase
{
use RefreshDatabase;

public function test_query() {
$this->seed(AddFunctionTagArticle::class);
public function test_query()
{
$this->seed(GetExistingTagsSeeder::class);

$message = Message::factory()->create();

Expand All @@ -25,10 +25,10 @@ public function test_query() {
$tag2 = Tag::factory()->create();
$dto = FunctionCallDto::from([
'arguments' => json_encode([
'query' => "SELECT id, label, active, created_at, updated_at FROM tags"
'query' => 'SELECT id, label, active, created_at, updated_at FROM tags',
]),
'function_name' => 'get_existing_tags',
'message' => $message
'message' => $message,
]);

/** @var Message $messageCreated */
Expand Down
3 changes: 3 additions & 0 deletions tests/Feature/MailBoxParserJobTest.php
Expand Up @@ -5,6 +5,7 @@
use App\Domains\EmailParser\MailDto;
use App\Jobs\MailBoxParserJob;
use App\Jobs\MessageCreatedJob;
use App\Models\Message;
use App\Models\User;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
Expand All @@ -27,6 +28,8 @@ public function test_larger_message()
$job = new MailBoxParserJob($dto);
$job->handle();
$this->assertDatabaseCount('messages', 1);
$message = Message::first();
$this->assertCount(3, $message->llm_functions);
Queue::assertPushed(MessageCreatedJob::class);
}
}

0 comments on commit dcd9d69

Please sign in to comment.