Skip to content

Commit

Permalink
feat: add openai compatibility support for google gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
TyperEJ committed Nov 21, 2024
1 parent 4a565d1 commit 72a83ad
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Responses/Chat/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
* @param array<int, CreateResponseChoice> $choices
*/
private function __construct(
public readonly string $id,
public readonly ?string $id,
public readonly string $object,
public readonly int $created,
public readonly string $model,
public readonly ?string $systemFingerprint,
public readonly array $choices,
public readonly CreateResponseUsage $usage,
public readonly ?CreateResponseUsage $usage,
private readonly MetaInformation $meta,
) {}

Expand All @@ -50,13 +50,13 @@ public static function from(array $attributes, MetaInformation $meta): self
), $attributes['choices']);

return new self(
$attributes['id'],
$attributes['id'] ?? null,
$attributes['object'],
$attributes['created'],
$attributes['model'],
$attributes['system_fingerprint'] ?? null,
$choices,
CreateResponseUsage::from($attributes['usage']),
isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null,
$meta,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Responses/Chat/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class CreateStreamedResponse implements ResponseContract
* @param array<int, CreateStreamedResponseChoice> $choices
*/
private function __construct(
public readonly string $id,
public readonly ?string $id,
public readonly string $object,
public readonly int $created,
public readonly string $model,
Expand All @@ -44,7 +44,7 @@ public static function from(array $attributes): self
), $attributes['choices']);

return new self(
$attributes['id'],
$attributes['id'] ?? null,
$attributes['object'],
$attributes['created'],
$attributes['model'],
Expand Down
4 changes: 2 additions & 2 deletions src/Responses/Embeddings/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class CreateResponse implements ResponseContract, ResponseHasMetaInformati
private function __construct(
public readonly string $object,
public readonly array $embeddings,
public readonly CreateResponseUsage $usage,
public readonly ?CreateResponseUsage $usage,
private readonly MetaInformation $meta,
) {}

Expand All @@ -48,7 +48,7 @@ public static function from(array $attributes, MetaInformation $meta): self
return new self(
$attributes['object'],
$embeddings,
CreateResponseUsage::from($attributes['usage']),
isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null,
$meta,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Responses/Embeddings/CreateResponseEmbedding.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class CreateResponseEmbedding
*/
private function __construct(
public readonly string $object,
public readonly int $index,
public readonly ?int $index,
public readonly array $embedding,
) {}

Expand All @@ -22,7 +22,7 @@ public static function from(array $attributes): self
{
return new self(
$attributes['object'],
$attributes['index'],
$attributes['index'] ?? null,
$attributes['embedding'],
);
}
Expand Down
76 changes: 76 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,64 @@ function chatCompletion(): array
];
}

/**
* @return array<string, mixed>
*/
function chatCompletionWithoutId(): array
{
return [
'object' => 'chat.completion',
'created' => 1677652288,
'model' => 'gpt-3.5-turbo',
'choices' => [
[
'index' => 0,
'message' => [
'role' => 'assistant',
'content' => "\n\nHello there, how may I assist you today?",
],
'finish_reason' => 'stop',
],
],
'usage' => [
'prompt_tokens' => 9,
'completion_tokens' => 12,
'total_tokens' => 21,
'prompt_tokens_details' => [
'cached_tokens' => 5,
],
'completion_tokens_details' => [
'reasoning_tokens' => 0,
'accepted_prediction_tokens' => 0,
'rejected_prediction_tokens' => 0,
],
],
];
}

/**
* @return array<string, mixed>
*/
function chatCompletionWithoutUsage(): array
{
return [
'id' => 'chatcmpl-123',
'object' => 'chat.completion',
'created' => 1677652288,
'model' => 'gpt-3.5-turbo',
'choices' => [
[
'index' => 0,
'message' => [
'role' => 'assistant',
'content' => "\n\nHello there, how may I assist you today?",
],
'finish_reason' => 'stop',
],
],
];
}

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -195,6 +253,24 @@ function chatCompletionStreamFirstChunk(): array
];
}

function chatCompletionStreamFirstChunkWithoutId(): array
{
return [
'object' => 'chat.completion.chunk',
'created' => 1679432086,
'model' => 'gpt-4-0314',
'choices' => [
[
'index' => 0,
'delta' => [
'role' => 'assistant',
],
'finish_reason' => null,
],
],
];
}

function chatCompletionStreamContentChunk(): array
{
return [
Expand Down
29 changes: 29 additions & 0 deletions tests/Fixtures/Embedding.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ function embedding(): array
];
}

/**
* @return array<string, mixed>
*/
function embeddingWithoutIndex(): array
{
return [
'object' => 'embedding',
'embedding' => [
-0.008906792,
-0.013743395,
0.009874112,
],
];
}

/**
* @return array<string, mixed>
*/
Expand All @@ -33,3 +48,17 @@ function embeddingList(): array
],
];
}

/**
* @return array<string, mixed>
*/
function embeddingListWithoutUsage(): array
{
return [
'object' => 'list',
'data' => [
embedding(),
embedding(),
],
];
}
32 changes: 32 additions & 0 deletions tests/Responses/Chat/CreateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('from without id', function () {
$completion = CreateResponse::from(chatCompletionWithoutId(), meta());

expect($completion)
->toBeInstanceOf(CreateResponse::class)
->id->toBeNull()
->object->toBe('chat.completion')
->created->toBe(1677652288)
->model->toBe('gpt-3.5-turbo')
->systemFingerprint->toBeNull()
->choices->toBeArray()->toHaveCount(1)
->choices->each->toBeInstanceOf(CreateResponseChoice::class)
->usage->toBeInstanceOf(CreateResponseUsage::class)
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('from without usage', function () {
$completion = CreateResponse::from(chatCompletionWithoutUsage(), meta());

expect($completion)
->toBeInstanceOf(CreateResponse::class)
->id->toBe('chatcmpl-123')
->object->toBe('chat.completion')
->created->toBe(1677652288)
->model->toBe('gpt-3.5-turbo')
->systemFingerprint->toBeNull()
->choices->toBeArray()->toHaveCount(1)
->choices->each->toBeInstanceOf(CreateResponseChoice::class)
->usage->toBeNull()
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('from with system fingerprint', function () {
$completion = CreateResponse::from(chatCompletionWithSystemFingerprint(), meta());

Expand Down
13 changes: 13 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
->choices->each->toBeInstanceOf(CreateStreamedResponseChoice::class);
});

test('from without id', function () {
$completion = CreateStreamedResponse::from(chatCompletionStreamFirstChunkWithoutId());

expect($completion)
->toBeInstanceOf(CreateStreamedResponse::class)
->id->toBeNull()
->object->toBe('chat.completion.chunk')
->created->toBe(1679432086)
->model->toBe('gpt-4-0314')
->choices->toBeArray()->toHaveCount(1)
->choices->each->toBeInstanceOf(CreateStreamedResponseChoice::class);
});

test('from usage chunk', function () {
$completion = CreateStreamedResponse::from(chatCompletionStreamUsageChunk());

Expand Down
14 changes: 14 additions & 0 deletions tests/Responses/Embeddings/CreateResponse.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OpenAI\Responses\Embeddings\CreateResponse;
use OpenAI\Responses\Embeddings\CreateResponseUsage;
use OpenAI\Responses\Embeddings\CreateResponseEmbedding;
use OpenAI\Responses\Meta\MetaInformation;

Expand All @@ -12,6 +13,19 @@
->object->toBe('list')
->embeddings->toBeArray()->toHaveCount(2)
->embeddings->each->toBeInstanceOf(CreateResponseEmbedding::class)
->usage->toBeInstanceOf(CreateResponseUsage::class)
->meta()->toBeInstanceOf(MetaInformation::class);
});

test('from without usage', function () {
$response = CreateResponse::from(embeddingListWithoutUsage(), meta());

expect($response)
->toBeInstanceOf(CreateResponse::class)
->object->toBe('list')
->embeddings->toBeArray()->toHaveCount(2)
->embeddings->each->toBeInstanceOf(CreateResponseEmbedding::class)
->usage->toBeNull()
->meta()->toBeInstanceOf(MetaInformation::class);
});

Expand Down
13 changes: 13 additions & 0 deletions tests/Responses/Embeddings/CreateResponseEmbedding.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
]);
});

test('from without index', function () {
$result = CreateResponseEmbedding::from(embeddingWithoutIndex());

expect($result)
->object->toBe('embedding')
->index->toBeNull()
->embedding->toBeArray()->toBe([
-0.008906792,
-0.013743395,
0.009874112,
]);
});

test('to array', function () {
$result = CreateResponseEmbedding::from(embedding());

Expand Down

0 comments on commit 72a83ad

Please sign in to comment.