Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Responses/Chat/CreateResponseToolCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,39 @@

final class CreateResponseToolCall
{
/**
* @param array<string, array<string,string>>|null $extraContent
*/
private function __construct(
public readonly string $id,
public readonly string $type,
public readonly CreateResponseToolCallFunction $function,
public readonly ?array $extraContent = null,
) {}

/**
* @param array{id: string, type?: string, function: array{name: string, arguments: string}} $attributes
* @param array{id: string, type?: string, function: array{name: string, arguments: string}, extra_content?: array<string, array<string,string>>|null} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['id'],
$attributes['type'] ?? 'function',
CreateResponseToolCallFunction::from($attributes['function']),
$attributes['extra_content'] ?? null,
);
}

/**
* @return array{id: string, type: string, function: array{name: string, arguments: string}}
* @return array{id: string, type: string, function: array{name: string, arguments: string}, extra_content?: array<string, array<string,string>>}
*/
public function toArray(): array
{
return [
return array_filter([
'id' => $this->id,
'type' => $this->type,
'function' => $this->function->toArray(),
];
'extra_content' => $this->extraContent,
], fn (mixed $value): bool => ! is_null($value));
}
}
10 changes: 8 additions & 2 deletions src/Responses/Chat/CreateStreamedResponseToolCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

final class CreateStreamedResponseToolCall
{
/**
* @param array<string, array<string,string>>|null $extraContent
*/
private function __construct(
public readonly ?int $index,
public readonly ?string $id,
public readonly ?string $type,
public readonly CreateStreamedResponseToolCallFunction $function,
public readonly ?array $extraContent,
) {}

/**
* @param array{index?: int, id?: string, type?: string, function: array{name?: string, arguments: string}} $attributes
* @param array{index?: int, id?: string, type?: string, function: array{name?: string, arguments: string}, extra_content?: array<string, array<string,string>>|null} $attributes
*/
public static function from(array $attributes): self
{
Expand All @@ -23,11 +27,12 @@ public static function from(array $attributes): self
$attributes['id'] ?? null,
$attributes['type'] ?? null,
CreateStreamedResponseToolCallFunction::from($attributes['function']),
$attributes['extra_content'] ?? null,
);
}

/**
* @return array{index?: int, id?: string, type?: string, function?: array{name?: string, arguments: string}}
* @return array{index?: int, id?: string, type?: string, function?: array{name?: string, arguments: string}, extra_content?: array<string, array<string,string>>}
*/
public function toArray(): array
{
Expand All @@ -36,6 +41,7 @@ public function toArray(): array
'id' => $this->id,
'type' => $this->type,
'function' => $this->function->toArray(),
'extra_content' => $this->extraContent,
], fn (mixed $value): bool => ! is_null($value));
}
}
44 changes: 44 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,50 @@ function chatCompletionWithToolCalls(): array
];
}

/**
* @return array<string, mixed>
*/
function chatCompletionWithToolCallsAndExtraContent(): array
{
return [
'id' => 'chatcmpl-123',
'object' => 'chat.completion',
'created' => 1699333252,
'model' => 'gpt-3.5-turbo-0613',
'choices' => [
[
'index' => 0,
'message' => [
'role' => 'assistant',
'content' => null,
'tool_calls' => [
[
'id' => 'call_trlgKnhMpYSC7CFXKw3CceUZ',
'type' => 'function',
'function' => [
'name' => 'get_current_weather',
'arguments' => "{\n \"location\": \"Boston, MA\"\n}",
],
'extra_content' => [
'google' => [
'thought_signature' => 'trlgKnhMpYSC7CFXKw3CceUZ',
],
],
],
],
],
'logprobs' => null,
'finish_reason' => 'tool_calls',
],
],
'usage' => [
'prompt_tokens' => 71,
'completion_tokens' => 17,
'total_tokens' => 88,
],
];
}

/**
* @return array<string, mixed>
*/
Expand Down
28 changes: 27 additions & 1 deletion tests/Responses/Chat/CreateResponseMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,26 @@
->content->toBeNull()
->toolCalls->toBeArray()
->toolCalls->toHaveCount(1)
->toolCalls->each->toBeInstanceOf(CreateResponseToolCall::class);
->toolCalls->each->toBeInstanceOf(CreateResponseToolCall::class)
->and($result->toolCalls[0])
->extraContent->toBeNull();
});

test('from tool calls response with extra content', function () {
$result = CreateResponseMessage::from(chatCompletionWithToolCallsAndExtraContent()['choices'][0]['message']);

expect($result)
->role->toBe('assistant')
->content->toBeNull()
->toolCalls->toBeArray()
->toolCalls->toHaveCount(1)
->toolCalls->each->toBeInstanceOf(CreateResponseToolCall::class)
->and($result->toolCalls[0])
->extraContent->toBeArray()
->extraContent->toHaveCount(1)
->extraContent->toHaveKey('google')
->extraContent->google->toHaveKey('thought_signature')
->extraContent->google->thought_signature->toBe('trlgKnhMpYSC7CFXKw3CceUZ');
});

test('from annotations response', function () {
Expand Down Expand Up @@ -94,6 +113,13 @@
->toBe(chatCompletionWithToolCalls()['choices'][0]['message']);
});

test('to array from tool calls response with extra content', function () {
$result = CreateResponseMessage::from(chatCompletionWithToolCallsAndExtraContent()['choices'][0]['message']);

expect($result->toArray())
->toBe(chatCompletionWithToolCallsAndExtraContent()['choices'][0]['message']);
});

test('to array from annotations response', function () {
$result = CreateResponseMessage::from(chatCompletionWithAnnotations()['choices'][0]['message']);

Expand Down