Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"candidates": [
{
"content": {
"parts": [
{
"text": "First text"
},
{
"executableCode": {
"language": "PYTHON",
"code": "print('Hello, World!')"
}
},
{
"codeExecutionResult": {
"outcome": "OUTCOME_DEADLINE_EXCEEDED",
"output": "An error occurred during code execution."
}
},
{
"text": "Last text"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
]
}
31 changes: 31 additions & 0 deletions fixtures/Bridge/VertexAi/code_execution_outcome_failed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"candidates": [
{
"content": {
"parts": [
{
"text": "First text"
},
{
"executableCode": {
"language": "PYTHON",
"code": "print('Hello, World!')"
}
},
{
"codeExecutionResult": {
"outcome": "OUTCOME_FAILED",
"output": "An error occurred during code execution."
}
},
{
"text": "Last text"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
]
}
37 changes: 37 additions & 0 deletions fixtures/Bridge/VertexAi/code_execution_outcome_ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"candidates": [
{
"content": {
"parts": [
{
"text": "First text"
},
{
"executableCode": {
"language": "PYTHON",
"code": "print('Hello, World!')"
}
},
{
"codeExecutionResult": {
"outcome": "OUTCOME_OK",
"output": "Hello, World!"
}
},
{
"text": "Second text\n"
},
{
"text": "Third text\n"
},
{
"text": "Fourth text"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
]
}
62 changes: 54 additions & 8 deletions src/platform/src/Bridge/VertexAi/Gemini/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
*/
final readonly class ResultConverter implements ResultConverterInterface
{
public const OUTCOME_OK = 'OUTCOME_OK';
public const OUTCOME_FAILED = 'OUTCOME_FAILED';
public const OUTCOME_DEADLINE_EXCEEDED = 'OUTCOME_DEADLINE_EXCEEDED';

public function supports(BaseModel $model): bool
{
return $model instanceof Model;
Expand Down Expand Up @@ -119,21 +123,44 @@ private function convertStream(HttpResponse $result): \Generator
* text?: string
* }[]
* }
* } $choices
* } $choice
*/
private function convertChoice(array $choices): ToolCallResult|TextResult
private function convertChoice(array $choice): ToolCallResult|TextResult
{
$content = $choices['content']['parts'][0] ?? [];
$contentParts = $choice['content']['parts'];

if (1 === \count($contentParts)) {
$contentPart = $contentParts[0];

if (isset($contentPart['functionCall'])) {
return new ToolCallResult($this->convertToolCall($contentPart['functionCall']));
}

if (isset($contentPart['text'])) {
return new TextResult($contentPart['text']);
}

throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finishReason']));
}

$content = '';
$successfulCodeExecutionDetected = false;
foreach ($contentParts as $contentPart) {
if ($this->isSuccessfulCodeExecution($contentPart)) {
$successfulCodeExecutionDetected = true;
continue;
}

if (isset($content['functionCall'])) {
return new ToolCallResult($this->convertToolCall($content['functionCall']));
if ($successfulCodeExecutionDetected) {
$content .= $contentPart['text'];
}
}

if (isset($content['text'])) {
return new TextResult($content['text']);
if ('' !== $content) {
return new TextResult($content);
}

throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choices['finishReason']));
throw new RuntimeException('Code execution failed.');
}

/**
Expand All @@ -146,4 +173,23 @@ private function convertToolCall(array $toolCall): ToolCall
{
return new ToolCall($toolCall['name'], $toolCall['name'], $toolCall['args']);
}

/**
* @param array{
* codeExecutionResult?: array{
* outcome: self::OUTCOME_*,
* output: string
* }
* } $contentPart
*/
private function isSuccessfulCodeExecution(array $contentPart): bool
{
if (!isset($contentPart['codeExecutionResult'])) {
return false;
}

$result = $contentPart['codeExecutionResult'];

return self::OUTCOME_OK === $result['outcome'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,54 @@ public function testItConvertsAResponseToAVectorResult()
$result = $resultConverter->convert(new RawHttpResult($response));

// Assert

$this->assertInstanceOf(TextResult::class, $result);
$this->assertSame('Hello, world!', $result->getContent());
}

public function testItReturnsAggregatedTextOnSuccess()
{
$response = $this->createStub(ResponseInterface::class);
$responseContent = file_get_contents(\dirname(__DIR__, 6).'/fixtures/Bridge/VertexAi/code_execution_outcome_ok.json');

$response
->method('toArray')
->willReturn(json_decode($responseContent, true));

$converter = new ResultConverter();

$result = $converter->convert(new RawHttpResult($response));
$this->assertInstanceOf(TextResult::class, $result);

$this->assertEquals("Second text\nThird text\nFourth text", $result->getContent());
}

public function testItThrowsExceptionOnFailure()
{
$response = $this->createStub(ResponseInterface::class);
$responseContent = file_get_contents(\dirname(__DIR__, 6).'/fixtures/Bridge/VertexAi/code_execution_outcome_failed.json');

$response
->method('toArray')
->willReturn(json_decode($responseContent, true));

$converter = new ResultConverter();

$this->expectException(\RuntimeException::class);
$converter->convert(new RawHttpResult($response));
}

public function testItThrowsExceptionOnTimeout()
{
$response = $this->createStub(ResponseInterface::class);
$responseContent = file_get_contents(\dirname(__DIR__, 6).'/fixtures/Bridge/VertexAi/code_execution_outcome_deadline_exceeded.json');

$response
->method('toArray')
->willReturn(json_decode($responseContent, true));

$converter = new ResultConverter();

$this->expectException(\RuntimeException::class);
$converter->convert(new RawHttpResult($response));
}
}