Skip to content

Commit

Permalink
start adding claude
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 29, 2024
1 parent b296b4f commit 5095f83
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/LlmDriver/ClaudeClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\LlmDriver;

use App\LlmDriver\Requests\MessageInDto;
use App\LlmDriver\Responses\CompletionResponse;
use App\LlmDriver\Responses\EmbeddingsResponseDto;
use Illuminate\Support\Facades\Log;

class ClaudeClient
{
protected string $driver = 'claude';

public function embedData(string $data): EmbeddingsResponseDto
{
if (! app()->environment('testing')) {
sleep(2);
}
Log::info('LlmDriver::MockClient::embedData');

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

return EmbeddingsResponseDto::from([
'embedding' => data_get($data, 'data.0.embedding'),
'token_count' => 1000,
]);
}

/**
* @param MessageInDto[] $messages
*/
public function chat(array $messages): CompletionResponse
{
if (! app()->environment('testing')) {
sleep(2);
}

Log::info('LlmDriver::MockClient::completion');

$data = <<<'EOD'
Voluptate irure cillum dolor anim officia reprehenderit dolor. Eiusmod veniam nostrud consectetur incididunt proident id. Anim adipisicing pariatur amet duis Lorem sunt veniam veniam est. Deserunt ea aliquip cillum pariatur consectetur. Dolor in reprehenderit adipisicing consectetur cupidatat ad cupidatat reprehenderit. Nostrud mollit voluptate aliqua anim pariatur excepteur eiusmod velit quis exercitation tempor quis excepteur.
EOD;

return new CompletionResponse($data);
}

public function completion(string $prompt): CompletionResponse
{
if (! app()->environment('testing')) {
sleep(2);
}

Log::info('LlmDriver::MockClient::completion');

$data = <<<'EOD'
Voluptate irure cillum dolor anim officia reprehenderit dolor. Eiusmod veniam nostrud consectetur incididunt proident id. Anim adipisicing pariatur amet duis Lorem sunt veniam veniam est. Deserunt ea aliquip cillum pariatur consectetur. Dolor in reprehenderit adipisicing consectetur cupidatat ad cupidatat reprehenderit. Nostrud mollit voluptate aliqua anim pariatur excepteur eiusmod velit quis exercitation tempor quis excepteur.
EOD;

return new CompletionResponse($data);
}

protected function getConfig(string $driver): array
{
return config("llmdriver.drivers.$driver");
}
}
52 changes: 52 additions & 0 deletions tests/Feature/ClaudeClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests\Feature;

use App\LlmDriver\ClaudeClient;
use App\LlmDriver\MockClient;
use App\LlmDriver\Requests\MessageInDto;
use App\LlmDriver\Responses\CompletionResponse;
use App\LlmDriver\Responses\EmbeddingsResponseDto;
use Tests\TestCase;

class ClaudeClientTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_embeddings(): void
{

$client = new ClaudeClient();

$results = $client->embedData('test');

$this->assertInstanceOf(EmbeddingsResponseDto::class, $results);

}

public function test_completion(): void
{
$client = new MockClient();

$results = $client->completion('test');

$this->assertInstanceOf(CompletionResponse::class, $results);

}

public function test_Chat(): void
{
$client = new MockClient();

$results = $client->chat([
MessageInDto::from([
'content' => 'test',
'role' => 'user',
]),
]);

$this->assertInstanceOf(CompletionResponse::class, $results);

}
}

0 comments on commit 5095f83

Please sign in to comment.