Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit d2d826f

Browse files
committed
[FIX] Replace toArray methods
1 parent c6ac0e7 commit d2d826f

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

src/Bridge/Anthropic/ModelHandler.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Bridge\Anthropic;
66

7+
use http\Exception\RuntimeException;
78
use PhpLlm\LlmChain\Model\Message\MessageBag;
89
use PhpLlm\LlmChain\Model\Model;
910
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
@@ -14,6 +15,10 @@
1415
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1516
use Symfony\Component\HttpClient\EventSourceHttpClient;
1617
use Symfony\Component\HttpClient\Exception\JsonException;
18+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
19+
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
20+
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1722
use Symfony\Contracts\HttpClient\HttpClientInterface;
1823
use Symfony\Contracts\HttpClient\ResponseInterface;
1924
use Webmozart\Assert\Assert;
@@ -55,13 +60,27 @@ public function request(Model $model, object|array|string $input, array $options
5560
]);
5661
}
5762

63+
/**
64+
* @throws TransportExceptionInterface
65+
* @throws ServerExceptionInterface
66+
* @throws RedirectionExceptionInterface
67+
* @throws ClientExceptionInterface
68+
*/
5869
public function convert(ResponseInterface $response, array $options = []): LlmResponse
5970
{
6071
if ($options['stream'] ?? false) {
6172
return new StreamResponse($this->convertStream($response));
6273
}
6374

64-
$data = $response->toArray();
75+
$data = json_decode($response->getContent(false), true);
76+
77+
if (!isset($data['content']) || !count($data['content'])) {
78+
throw new RuntimeException('Response does not contain any content');
79+
}
80+
81+
if (!isset($data['content'][0]['text'])) {
82+
throw new RuntimeException('Response content does not contain any text');
83+
}
6584

6685
return new TextResponse($data['content'][0]['text']);
6786
}

src/Bridge/Ollama/LlamaModelHandler.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Bridge\Ollama;
66

7+
use http\Exception\RuntimeException;
78
use PhpLlm\LlmChain\Bridge\Meta\Llama;
89
use PhpLlm\LlmChain\Model\Message\MessageBag;
910
use PhpLlm\LlmChain\Model\Model;
@@ -41,7 +42,15 @@ public function request(Model $model, object|array|string $input, array $options
4142

4243
public function convert(ResponseInterface $response, array $options = []): LlmResponse
4344
{
44-
$data = $response->toArray();
45+
$data = json_decode($response->getContent(false), true);
46+
47+
if (!isset($data['message'])) {
48+
throw new RuntimeException('Response does not contain message');
49+
}
50+
51+
if (!isset($data['message']['content'])) {
52+
throw new RuntimeException('Message does not contain content');
53+
}
4554

4655
return new TextResponse($data['message']['content']);
4756
}

src/Bridge/OpenAI/Embeddings/ResponseConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
88
use PhpLlm\LlmChain\Document\Vector;
9+
use PhpLlm\LlmChain\Exception\RuntimeException;
910
use PhpLlm\LlmChain\Model\Model;
1011
use PhpLlm\LlmChain\Model\Response\VectorResponse;
1112
use PhpLlm\LlmChain\Platform\ResponseConverter as PlatformResponseConverter;
@@ -20,7 +21,11 @@ public function supports(Model $model, array|string|object $input): bool
2021

2122
public function convert(ResponseInterface $response, array $options = []): VectorResponse
2223
{
23-
$data = $response->toArray();
24+
$data = json_decode($response->getContent(false), true);
25+
26+
if (!isset($data['data'])) {
27+
throw new RuntimeException('Response does not contain data');
28+
}
2429

2530
return new VectorResponse(
2631
...\array_map(

src/Bridge/OpenAI/GPT/ResponseConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function convert(HttpResponse $response, array $options = []): LlmRespons
3333
return $this->convertStream($response);
3434
}
3535

36-
$data = $response->toArray();
36+
$data = json_decode($response->getContent(false), true);
3737

3838
if (!isset($data['choices'])) {
3939
throw new RuntimeException('Response does not contain choices');

src/Bridge/Replicate/LlamaResponseConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Bridge\Replicate;
66

7+
use http\Exception\RuntimeException;
78
use PhpLlm\LlmChain\Bridge\Meta\Llama;
89
use PhpLlm\LlmChain\Model\Message\MessageBag;
910
use PhpLlm\LlmChain\Model\Model;
@@ -21,7 +22,11 @@ public function supports(Model $model, object|array|string $input): bool
2122

2223
public function convert(HttpResponse $response, array $options = []): LlmResponse
2324
{
24-
$data = $response->toArray();
25+
$data = json_decode($response->getContent(false), true);
26+
27+
if (!isset($data['output'])) {
28+
throw new RuntimeException('Response does not contain output');
29+
}
2530

2631
return new TextResponse(implode('', $data['output']));
2732
}

src/Bridge/Voyage/ModelHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Bridge\Voyage;
66

7+
use http\Exception\RuntimeException;
78
use PhpLlm\LlmChain\Document\Vector;
89
use PhpLlm\LlmChain\Model\Model;
910
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
@@ -39,7 +40,11 @@ public function request(Model $model, object|string|array $input, array $options
3940

4041
public function convert(ResponseInterface $response, array $options = []): LlmResponse
4142
{
42-
$response = $response->toArray();
43+
$response = json_decode($response->getContent(false), true);
44+
45+
if (!isset($data['embedding']) || !isset($data['data'])) {
46+
throw new RuntimeException('Response does not contain embedding data');
47+
}
4348

4449
$vectors = array_map(fn (array $data) => new Vector($data['embedding']), $response['data']);
4550

tests/Bridge/OpenAI/Embeddings/ResponseConverterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function itConvertsAResponseToAVectorResponse(): void
2020
{
2121
$response = $this->createStub(ResponseInterface::class);
2222
$response
23-
->method('toArray')
24-
->willReturn(\json_decode($this->getEmbeddingStub(), true));
23+
->method('getContent')
24+
->willReturn($this->getEmbeddingStub());
2525

2626
$vectorResponse = (new ResponseConverter())->convert($response);
2727
$convertedContent = $vectorResponse->getContent();

0 commit comments

Comments
 (0)