Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Sep 2, 2023
1 parent d52c8e3 commit 70c54ea
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 90 deletions.
6 changes: 3 additions & 3 deletions app/Console/Commands/ContentToVoiceCommand.php
Expand Up @@ -27,9 +27,9 @@ class ContentToVoiceCommand extends Command
public function handle()
{
$text = $this->argument('text');
$this->info("Going to convert " . $text);
$this->info("Keep an eye on the logs");
$this->info('Going to convert '.$text);
$this->info('Keep an eye on the logs');
$results = ContentToVoiceClient::handle($text);
$this->info("Url: " . $results);
$this->info('Url: '.$results);
}
}
3 changes: 1 addition & 2 deletions app/Domains/LlmFunctions/ContentToVoice/ContentToVoice.php
Expand Up @@ -3,13 +3,12 @@
namespace App\Domains\LlmFunctions\ContentToVoice;

use App\Domains\LlmFunctions\Dto\RoleTypeEnum;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoiceClient;
use App\Models\Message;
use App\OpenAi\Dtos\FunctionCallDto;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoiceClient;

class ContentToVoice extends \App\Domains\LlmFunctions\LlmFunctionContract
{

public function handle(FunctionCallDto $functionCallDto): Message
{
$content = data_get($functionCallDto->arguments, 'content', null);
Expand Down
21 changes: 11 additions & 10 deletions app/Domains/LlmFunctions/ContentToVoice/ContentToVoiceClient.php
Expand Up @@ -20,15 +20,15 @@ public function handle(string $longContent): string
->timeout(60)
->retry(3, 1500)
->post('https://large-text-to-speech.p.rapidapi.com/tts', [
'text' => $longContent
'text' => $longContent,
]);

/**
* Now it has to keep trying till it is done or broke
* tests/fixtures/content_to_voice_response_first.json
*/
logger("Results from voice to text", [
$response->json()
logger('Results from voice to text', [
$response->json(),
]);

$status = $response->json()['status'];
Expand All @@ -42,15 +42,16 @@ public function handle(string $longContent): string
$url = data_get($response, 'url', null);
}

if($url === null) {
throw new \Exception("Could not convert to audio");
if ($url === null) {
throw new \Exception('Could not convert to audio');
}

return $url;
}

protected function getStatus(string $job_id) : array {
logger("Checking status of voice conversion");
protected function getStatus(string $job_id): array
{
logger('Checking status of voice conversion');
$token = config('services.rapid.api');
if (! $token) {
throw new \Exception('Missing token');
Expand All @@ -63,10 +64,10 @@ protected function getStatus(string $job_id) : array {
->timeout(60)
->retry(3, 1500)
->get('https://large-text-to-speech.p.rapidapi.com/tts', [
'id' => $job_id
]);
'id' => $job_id,
]);

logger("Status " . $response->json()['status']);
logger('Status '.$response->json()['status']);

return $response->json();

Expand Down
8 changes: 4 additions & 4 deletions app/Domains/Message/MessageRepository.php
Expand Up @@ -30,7 +30,7 @@ public function handle(Message $message): Message

$prompts = $this->createPrompt();

put_fixture("prompts_used.json", $prompts);
put_fixture('prompts_used.json', $prompts);

$this->messageBuilder->setMessages($prompts);

Expand All @@ -52,7 +52,7 @@ protected function createPrompt(): MessagesDto
'content' => $this->parent_message->content,
]);

logger("Latest id " . $this->parent_message->id);
logger('Latest id '.$this->parent_message->id);

$messages = Message::query()
->where('parent_id', $this->parent_message->id)->latest()
Expand All @@ -68,7 +68,7 @@ protected function createPrompt(): MessagesDto
$prompts[] = MessageDto::from([
'role' => $message->role->value,
'content' => $message->content,
"name" => $message->name
'name' => $message->name,
]);
} elseif ($this->callWasResultOfFunctionCall($message)) {
$prompts[] = MessageDto::from([
Expand All @@ -83,7 +83,7 @@ protected function createPrompt(): MessagesDto
}
}

put_fixture("prompts_before.json", $prompts);
put_fixture('prompts_before.json', $prompts);

return MessagesDto::from([
'messages' => $prompts,
Expand Down
4 changes: 2 additions & 2 deletions app/Domains/Scraping/RapidScrapeClient.php
Expand Up @@ -9,8 +9,8 @@ class RapidScrapeClient
public function handle(string $url): string
{

if(config('services.rapid.mock') && !app()->environment('testing')) {
return "Perfection is Achieved Not When There Is Nothing More to Add, But When There Is Nothing Left to Take Away - Antoine de Saint-Exuper";
if (config('services.rapid.mock') && ! app()->environment('testing')) {
return 'Perfection is Achieved Not When There Is Nothing More to Add, But When There Is Nothing Left to Take Away - Antoine de Saint-Exuper';
}

$token = config('services.rapid.api');
Expand Down
8 changes: 4 additions & 4 deletions app/Jobs/MessageCreatedJob.php
Expand Up @@ -35,17 +35,17 @@ public function handle(): void
/** @var Message $message */
$message = MessageRepository::handle($this->message);

logger("Message role and function", [
'role' => $message->role->name,
'function' => $message->function_call
logger('Message role and function', [
'role' => $message->role->name,
'function' => $message->function_call,
]);

/**
* @NOTE
* Do a follow up since it was a function
* so we can get
*/
if($message->role === RoleTypeEnum::Function) {
if ($message->role === RoleTypeEnum::Function) {
MessageCreatedJob::dispatch($this->message);
}
MessageStatusEvent::dispatch($this->message);
Expand Down
6 changes: 3 additions & 3 deletions app/OpenAi/Dtos/MessageDto.php
Expand Up @@ -21,15 +21,15 @@ public function toArray(): array
'content' => $this->content,
];

if($this->role instanceof RoleTypeEnum) {
if ($this->role instanceof RoleTypeEnum) {
$results['role'] = $this->role->value;
}

if($this->function) {
if ($this->function) {
$results['function'] = $this->function;
}

if($this->name) {
if ($this->name) {
$results['name'] = $this->name;
}

Expand Down
1 change: 0 additions & 1 deletion app/Tools/GetSiteWrapper.php
Expand Up @@ -9,7 +9,6 @@ class GetSiteWrapper
public function handle(string $url): string|null
{


return RapidScrapeClient::handle($url);
}
}
2 changes: 1 addition & 1 deletion app/helpers.php
@@ -1,8 +1,8 @@
<?php

use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoice;
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\Scheduling\TaskRepository;
use Illuminate\Support\Arr;
Expand Down
8 changes: 3 additions & 5 deletions tests/Feature/ContentToVoiceClientTest.php
Expand Up @@ -8,21 +8,19 @@

class ContentToVoiceClientTest extends TestCase
{

public function test_results()
{
Http::preventStrayRequests();
$content = get_fixture_v2('content_to_voice_response_first.json');
$url = get_fixture_v2('content_to_voice_complete.json');
Http::fake([
'large-text-to-speech.p.rapidapi.com/*' =>
Http::sequence()
'large-text-to-speech.p.rapidapi.com/*' => Http::sequence()
->push($content)
->push($url)
->push($url),
]);
$url = 'Some long amount of text here';
$results = ContentToVoiceClient::handle($url);
Http::assertSentCount(2);
$this->assertEquals('https://s3.eu-central-1.amazonaws.com/tts-download/0964685040735b7f5890626d1082a918.wav?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAZ3CYNLHHVKA7D7Z4%2F20230902%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20230902T130513Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=280d75663ebd371d9a48e85551613d0d40e14661fb84152806c5287d5199fd9a', $results);
$this->assertEquals('https://s3.eu-central-1.amazonaws.com/tts-download/0964685040735b7f5890626d1082a918.wav?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAZ3CYNLHHVKA7D7Z4%2F20230902%2Feu-central-1%2Fs3%2Faws4_request&X-Amz-Date=20230902T130513Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=280d75663ebd371d9a48e85551613d0d40e14661fb84152806c5287d5199fd9a', $results);
}
}
12 changes: 5 additions & 7 deletions tests/Feature/ContentToVoiceTest.php
Expand Up @@ -3,22 +3,20 @@
namespace Tests\Feature;

use App\Domains\LlmFunctions\Dto\RoleTypeEnum;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoice;
use App\Models\Message;
use App\Models\User;
use App\OpenAi\Dtos\FunctionCallDto;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoice;
use Facades\App\Domains\LlmFunctions\ContentToVoice\ContentToVoiceClient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class ContentToVoiceTest extends TestCase
{

public function test_gets_content() {
ContentToVoiceClient::shouldReceive("handle")
public function test_gets_content()
{
ContentToVoiceClient::shouldReceive('handle')
->once()
->andReturn("Some url");
->andReturn('Some url');

User::factory()->create();

Expand Down
24 changes: 12 additions & 12 deletions tests/Feature/MessageBuilderTest.php
Expand Up @@ -39,22 +39,22 @@ public function test_builds_second_level()
[
'role' => 'system',
'content' => 'This is a podcast transcript about X, Y, and Z.',
'token_count' => 13
'token_count' => 13,
],
[
'role' => 'user',
'content' => 'Can you summarize this podcast transcript?',
'token_count' => 7
'token_count' => 7,
],
[
'role' => 'assistant',
'content' => '[insert summary from Play 1]',
'token_count' => 8
'token_count' => 8,
],
[
'role' => 'user',
'content' => 'Can you pull out some key talking points from this summary?',
'token_count' => 12
'token_count' => 12,
],
],
$messageBuilder->getMessages()
Expand All @@ -73,12 +73,12 @@ public function test_with_count()
[
'role' => 'system',
'content' => 'This is a podcast transcript about X, Y, and Z.',
'token_count' => 13
'token_count' => 13,
],
[
'role' => 'user',
'content' => 'Can you summarize this podcast transcript?',
'token_count' => 7
'token_count' => 7,
],
],
$messageBuilder->getMessagesWithCount()
Expand Down Expand Up @@ -121,32 +121,32 @@ public function test_builds_third_level()
[
'role' => 'system',
'content' => 'This is a podcast transcript about X, Y, and Z.',
'token_count' => 13
'token_count' => 13,
],
[
'role' => 'user',
'content' => 'Can you summarize this podcast transcript?',
'token_count' => 7
'token_count' => 7,
],
[
'role' => 'assistant',
'content' => '[insert summary from Play 1]',
'token_count' => 8
'token_count' => 8,
],
[
'role' => 'user',
'content' => 'Can you pull out some key talking points from this summary?',
'token_count' => 12
'token_count' => 12,
],
[
'role' => 'assistant',
'content' => '[insert key talking points from Play 2]',
'token_count' => 10
'token_count' => 10,
],
[
'role' => 'user',
'content' => 'Can you convert these talking points into a paragraph suitable for a LinkedIn post?',
'token_count' => 15
'token_count' => 15,
],
],
$messageBuilder->getMessages()
Expand Down
19 changes: 9 additions & 10 deletions tests/Feature/MessageDtoTest.php
Expand Up @@ -4,28 +4,27 @@

use App\Domains\LlmFunctions\Dto\RoleTypeEnum;
use App\OpenAi\Dtos\MessageDto;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class MessageDtoTest extends TestCase
{

public function test_not_null() {
public function test_not_null()
{
$dto = MessageDto::from([
'content' => "foo",
'role' => RoleTypeEnum::User
'content' => 'foo',
'role' => RoleTypeEnum::User,
]);

$this->assertArrayNotHasKey('function', $dto->toArray());
$this->assertArrayNotHasKey('name', $dto->toArray());
}

public function test_there() {
public function test_there()
{
$dto = MessageDto::from([
'content' => "foo",
'function' => "foo",
'role' => RoleTypeEnum::User
'content' => 'foo',
'function' => 'foo',
'role' => RoleTypeEnum::User,
]);

$this->assertArrayHasKey('function', $dto->toArray());
Expand Down

0 comments on commit 70c54ea

Please sign in to comment.