From 5095f83c4168b163e97645c890ef5d6604e18a9d Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Fri, 29 Mar 2024 14:53:28 -0400 Subject: [PATCH] start adding claude --- app/LlmDriver/ClaudeClient.php | 66 ++++++++++++++++++++++++++++++ tests/Feature/ClaudeClientTest.php | 52 +++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 app/LlmDriver/ClaudeClient.php create mode 100644 tests/Feature/ClaudeClientTest.php diff --git a/app/LlmDriver/ClaudeClient.php b/app/LlmDriver/ClaudeClient.php new file mode 100644 index 00000000..6bf9fe98 --- /dev/null +++ b/app/LlmDriver/ClaudeClient.php @@ -0,0 +1,66 @@ +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"); + } +} diff --git a/tests/Feature/ClaudeClientTest.php b/tests/Feature/ClaudeClientTest.php new file mode 100644 index 00000000..e7c15d6d --- /dev/null +++ b/tests/Feature/ClaudeClientTest.php @@ -0,0 +1,52 @@ +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); + + } +}