File tree Expand file tree Collapse file tree 6 files changed +154
-9
lines changed Expand file tree Collapse file tree 6 files changed +154
-9
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace App \LlmDriver ;
4
4
5
+ use App \LlmDriver \Responses \CompletionResponse ;
5
6
use App \LlmDriver \Responses \EmbeddingsResponseDto ;
6
7
use Illuminate \Support \Facades \Log ;
7
8
8
9
abstract class BaseClient
9
10
{
11
+ protected string $ driver = 'mock ' ;
12
+
10
13
public function embedData (string $ data ): EmbeddingsResponseDto
11
14
{
12
15
@@ -19,4 +22,20 @@ public function embedData(string $data): EmbeddingsResponseDto
19
22
1000 ,
20
23
);
21
24
}
25
+
26
+ public function completion (string $ prompt ): CompletionResponse
27
+ {
28
+ Log::info ('LlmDriver::MockClient::completion ' );
29
+
30
+ $ data = <<<'EOD'
31
+ 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.
32
+ EOD;
33
+
34
+ return new CompletionResponse ($ data );
35
+ }
36
+
37
+ protected function getConfig (string $ driver ): array
38
+ {
39
+ return config ("llmdriver.drivers. $ driver " );
40
+ }
22
41
}
Original file line number Diff line number Diff line change 7
7
8
8
class MockClient extends BaseClient
9
9
{
10
- public function completion (string $ prompt ): CompletionResponse
11
- {
12
- Log::info ('LlmDriver::MockClient::completion ' );
13
10
14
- $ data = <<<'EOD'
15
- 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.
16
- EOD;
17
-
18
- return new CompletionResponse ($ data );
19
- }
20
11
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace App \LlmDriver ;
4
+
5
+ use App \LlmDriver \Responses \CompletionResponse ;
6
+ use App \LlmDriver \Responses \EmbeddingsResponseDto ;
7
+ use Illuminate \Support \Facades \Log ;
8
+ use OpenAI \Laravel \Facades \OpenAI ;
9
+
10
+ class OpenAiClient extends BaseClient
11
+ {
12
+ protected string $ driver = 'openai ' ;
13
+
14
+ public function embedData (string $ data ): EmbeddingsResponseDto
15
+ {
16
+
17
+ $ response = OpenAI::embeddings ()->create ([
18
+ 'model ' => $ this ->getConfig ('openai ' )['embedding_model ' ],
19
+ 'input ' => $ data ,
20
+ ]);
21
+
22
+ $ results = [];
23
+
24
+ foreach ($ response ->embeddings as $ embedding ) {
25
+ $ embedding ->object ; // 'embedding'
26
+ $ results = $ embedding ->embedding ; // [0.018990106880664825, -0.0073809814639389515, ...]
27
+ $ embedding ->index ; // 0
28
+ }
29
+
30
+ return new EmbeddingsResponseDto (
31
+ $ results ,
32
+ $ response ->usage ->totalTokens ,
33
+ );
34
+ }
35
+
36
+ public function completion (string $ prompt , int $ temperature = 0 ): CompletionResponse
37
+ {
38
+ $ response = OpenAI::completions ()->create ([
39
+ 'model ' => $ this ->getConfig ('openai ' )['completion_model ' ],
40
+ 'prompt ' => $ prompt ,
41
+ 'temperature ' => 0
42
+ ]);
43
+
44
+ $ results = null ;
45
+
46
+ foreach ($ response ->choices as $ result ) {
47
+ $ results = $ result ->text ; // '\n\nThis is a test'
48
+ }
49
+
50
+ return new CompletionResponse ($ results );
51
+ }
52
+
53
+ }
Original file line number Diff line number Diff line change 10
10
'openai ' => [
11
11
'api_key ' => env ('OPENAI_API_KEY ' ),
12
12
'api_url ' => env ('OPENAI_API_URL ' , 'https://api.openai.com/v1/engines/davinci-codex/completions ' ),
13
+ 'embedding_model ' => env ('OPENAI_EMBEDDING_MODEL ' , 'text-embedding-3-large ' ),
14
+ 'completion_model ' => env ('OPENAI_COMPLETION_MODEL ' , 'gpt-4-turbo-preview ' ),
13
15
],
14
16
'azure ' => [
15
17
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Illuminate \Database \Migrations \Migration ;
4
+ use Illuminate \Database \Schema \Blueprint ;
5
+ use Illuminate \Support \Facades \Schema ;
6
+
7
+ return new class extends Migration
8
+ {
9
+ /**
10
+ * Run the migrations.
11
+ */
12
+ public function up (): void
13
+ {
14
+ Schema::table ('document_chunks ' , function (Blueprint $ table ) {
15
+ $ table ->vector ('embedding ' , 3072 )->nullable ()->change ();
16
+ });
17
+ }
18
+
19
+ /**
20
+ * Reverse the migrations.
21
+ */
22
+ public function down (): void
23
+ {
24
+ //
25
+ }
26
+ };
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Tests \Feature ;
4
+
5
+ use Illuminate \Foundation \Testing \RefreshDatabase ;
6
+ use Illuminate \Foundation \Testing \WithFaker ;
7
+ use Tests \TestCase ;
8
+
9
+ use App \LlmDriver \Responses \CompletionResponse ;
10
+ use App \LlmDriver \Responses \EmbeddingsResponseDto ;
11
+ use Illuminate \Support \Facades \Log ;
12
+ use OpenAI \Laravel \Facades \OpenAI ;
13
+ use OpenAI \Responses \Completions \CreateResponse as CompletionsCreateResponse ;
14
+ use OpenAI \Responses \Embeddings \CreateResponse ;
15
+
16
+ class OpenAiClientTest extends TestCase
17
+ {
18
+ /**
19
+ * A basic feature test example.
20
+ */
21
+ public function test_openai_client (): void
22
+ {
23
+ OpenAI::fake ([
24
+ CreateResponse::fake ([
25
+ 'embeddings ' => [
26
+ [
27
+ 'embedding ' => 'awesome! ' ,
28
+ ],
29
+ ],
30
+ ]),
31
+ ]);
32
+
33
+ $ openaiClient = new \App \LlmDriver \OpenAiClient ();
34
+ $ response = $ openaiClient ->embedData ('test ' );
35
+ $ this ->assertInstanceOf (EmbeddingsResponseDto::class, $ response );
36
+ }
37
+
38
+ public function test_completion (): void
39
+ {
40
+ OpenAI::fake ([
41
+ CompletionsCreateResponse::fake ([
42
+ 'choices ' => [
43
+ [
44
+ 'choice ' => 'awesome! ' ,
45
+ ],
46
+ ],
47
+ ]),
48
+ ]);
49
+
50
+ $ openaiClient = new \App \LlmDriver \OpenAiClient ();
51
+ $ response = $ openaiClient ->completion ('test ' );
52
+ $ this ->assertInstanceOf (CompletionResponse::class, $ response );
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments