Skip to content

Commit d7ecb4d

Browse files
authored
refactor: gpt stream conversion (#218)
1 parent 706497c commit d7ecb4d

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

src/Bridge/OpenAI/GPT/ResponseConverter.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function supports(Model $model, array|string|object $input): bool
3232
public function convert(HttpResponse $response, array $options = []): LlmResponse
3333
{
3434
if ($options['stream'] ?? false) {
35-
return $this->convertStream($response);
35+
return new StreamResponse($this->convertStream($response));
3636
}
3737

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

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

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

68-
private function convertStream(HttpResponse $response): StreamResponse
69-
{
70-
$stream = $this->streamResponse($response);
71-
72-
return new StreamResponse($this->convertStreamContent($stream));
73-
}
74-
75-
private function streamResponse(HttpResponse $response): \Generator
68+
private function convertStream(HttpResponse $response): \Generator
7669
{
70+
$toolCalls = [];
7771
foreach ((new EventSourceHttpClient())->stream($response) as $chunk) {
7872
if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) {
7973
continue;
8074
}
8175

8276
try {
8377
$data = $chunk->getArrayData();
84-
85-
yield $data;
8678
} catch (JsonException) {
8779
// try catch only needed for Symfony 6.4
8880
continue;
8981
}
90-
}
91-
}
9282

93-
private function streamIsToolCall(\Generator $response): bool
94-
{
95-
$data = $response->current();
83+
if ($this->streamIsToolCall($data)) {
84+
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
85+
}
9686

97-
return isset($data['choices'][0]['delta']['tool_calls']);
87+
if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
88+
yield new ToolCallResponse(...\array_map($this->convertToolCall(...), $toolCalls));
89+
}
90+
91+
if (!isset($data['choices'][0]['delta']['content'])) {
92+
continue;
93+
}
94+
95+
yield $data['choices'][0]['delta']['content'];
96+
}
9897
}
9998

10099
/**
@@ -126,24 +125,12 @@ private function convertStreamToToolCalls(array $toolCalls, array $data): array
126125
return $toolCalls;
127126
}
128127

129-
private function convertStreamContent(\Generator $generator): \Generator
128+
/**
129+
* @param array<string, mixed> $data
130+
*/
131+
private function streamIsToolCall(array $data): bool
130132
{
131-
$toolCalls = [];
132-
foreach ($generator as $data) {
133-
if ($this->streamIsToolCall($generator)) {
134-
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
135-
}
136-
137-
if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
138-
yield new ToolCallResponse(...\array_map([$this, 'convertToolCall'], $toolCalls));
139-
}
140-
141-
if (!isset($data['choices'][0]['delta']['content'])) {
142-
continue;
143-
}
144-
145-
yield $data['choices'][0]['delta']['content'];
146-
}
133+
return isset($data['choices'][0]['delta']['tool_calls']);
147134
}
148135

149136
/**

0 commit comments

Comments
 (0)