Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
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
57 changes: 22 additions & 35 deletions src/Bridge/OpenAI/GPT/ResponseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function supports(Model $model, array|string|object $input): bool
public function convert(HttpResponse $response, array $options = []): LlmResponse
{
if ($options['stream'] ?? false) {
return $this->convertStream($response);
return new StreamResponse($this->convertStream($response));
}

try {
Expand All @@ -52,7 +52,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
}

/** @var Choice[] $choices */
$choices = \array_map([$this, 'convertChoice'], $data['choices']);
$choices = \array_map($this->convertChoice(...), $data['choices']);

if (1 !== count($choices)) {
return new ChoiceResponse(...$choices);
Expand All @@ -65,36 +65,35 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
return new TextResponse($choices[0]->getContent());
}

private function convertStream(HttpResponse $response): StreamResponse
{
$stream = $this->streamResponse($response);

return new StreamResponse($this->convertStreamContent($stream));
}

private function streamResponse(HttpResponse $response): \Generator
private function convertStream(HttpResponse $response): \Generator
{
$toolCalls = [];
foreach ((new EventSourceHttpClient())->stream($response) as $chunk) {
if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) {
continue;
}

try {
$data = $chunk->getArrayData();

yield $data;
} catch (JsonException) {
// try catch only needed for Symfony 6.4
continue;
}
}
}

private function streamIsToolCall(\Generator $response): bool
{
$data = $response->current();
if ($this->streamIsToolCall($data)) {
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
}

return isset($data['choices'][0]['delta']['tool_calls']);
if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
yield new ToolCallResponse(...\array_map($this->convertToolCall(...), $toolCalls));
}

if (!isset($data['choices'][0]['delta']['content'])) {
continue;
}

yield $data['choices'][0]['delta']['content'];
}
}

/**
Expand Down Expand Up @@ -126,24 +125,12 @@ private function convertStreamToToolCalls(array $toolCalls, array $data): array
return $toolCalls;
}

private function convertStreamContent(\Generator $generator): \Generator
/**
* @param array<string, mixed> $data
*/
private function streamIsToolCall(array $data): bool
{
$toolCalls = [];
foreach ($generator as $data) {
if ($this->streamIsToolCall($generator)) {
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
}

if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
yield new ToolCallResponse(...\array_map([$this, 'convertToolCall'], $toolCalls));
}

if (!isset($data['choices'][0]['delta']['content'])) {
continue;
}

yield $data['choices'][0]['delta']['content'];
}
return isset($data['choices'][0]['delta']['tool_calls']);
}

/**
Expand Down