Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;

$embeddings = new Embeddings($platform, Embeddings::TEXT_3_SMALL);

$vectors = $platform->request($embeddings, $textInput)->getContent();
$vectors = $platform->request($embeddings, $textInput)->asVectors();

dump($vectors[0]->getData()); // Array of float values
```
Expand All @@ -703,7 +703,7 @@ foreach ($inputs as $input) {
}

foreach ($responses as $response) {
echo $response->getContent().PHP_EOL;
echo $response->asText().PHP_EOL;
}
```

Expand Down Expand Up @@ -822,7 +822,7 @@ $response = $platform->request($model, $image, [
'task' => Task::OBJECT_DETECTION, // defining a task is mandatory for internal request & response handling
]);

dump($response->getContent());
dump($response->asObject());
```

#### Code Examples
Expand Down Expand Up @@ -867,7 +867,7 @@ $response = $platform->request($model, 'How many continents are there in the wor
'task' => Task::Text2TextGeneration,
]);

echo $response->getContent().PHP_EOL;
echo $response->asText().PHP_EOL;
```

#### Code Examples
Expand Down
2 changes: 1 addition & 1 deletion examples/azure/audio-transcript.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@

$response = $platform->request($model, $file);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/azure/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use PhpLlm\LlmChain\Platform\Bridge\Azure\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -28,6 +27,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/audio-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::AUDIO_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/automatic-speech-recognition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::AUTOMATIC_SPEECH_RECOGNITION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/chat-completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
'task' => Task::CHAT_COMPLETION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/huggingface/feature-extraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\PlatformFactory;
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Task;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -21,6 +20,4 @@
'task' => Task::FEATURE_EXTRACTION,
]);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/fill-mask.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
'task' => Task::FILL_MASK,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::IMAGE_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-segmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::IMAGE_SEGMENTATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/image-to-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::IMAGE_TO_TEXT,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/object-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'task' => Task::OBJECT_DETECTION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/question-answering.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
'task' => Task::QUESTION_ANSWERING,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/sentence-similarity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
'task' => Task::SENTENCE_SIMILARITY,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/summarization.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
'task' => Task::SUMMARIZATION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/table-question-answering.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
'task' => Task::TABLE_QUESTION_ANSWERING,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/text-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
'task' => Task::TEXT_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/text-generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
'task' => Task::TEXT_GENERATION,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/huggingface/text-to-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\PlatformFactory;
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Task;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\Response\BinaryResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -21,6 +20,4 @@
'task' => Task::TEXT_TO_IMAGE,
]);

assert($response instanceof BinaryResponse);

echo $response->toBase64().\PHP_EOL;
echo $response->asBase64().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/token-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
'task' => Task::TOKEN_CLASSIFICATION,
]);

dump($response->getContent());
dump($response->asObject());
2 changes: 1 addition & 1 deletion examples/huggingface/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'tgt_lang' => 'en',
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/huggingface/zero-shot-classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
'candidate_labels' => ['refund', 'legal', 'faq'],
]);

dump($response->getContent());
dump($response->asObject());
5 changes: 1 addition & 4 deletions examples/mistral/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use PhpLlm\LlmChain\Platform\Bridge\Mistral\Embeddings;
use PhpLlm\LlmChain\Platform\Bridge\Mistral\PlatformFactory;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -23,6 +22,4 @@
salt. The goal was to prevent deficiencies and promote better health in the population.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/audio-transcript.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

$response = $platform->request($model, $file);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/openai/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -22,6 +21,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
2 changes: 1 addition & 1 deletion examples/openai/image-output-dall-e-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
],
);

foreach ($response->getContent() as $index => $image) {
foreach ($response->getResponse()->getContent() as $index => $image) {
echo 'Image '.$index.': '.$image->url.\PHP_EOL;
}
2 changes: 1 addition & 1 deletion examples/openai/image-output-dall-e-3.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
options: [
'response_format' => 'url', // Generate response as URL
],
);
)->getResponse();

assert($response instanceof ImageResponse);

Expand Down
2 changes: 1 addition & 1 deletion examples/parallel-chat-gpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@

echo 'Waiting for the responses ...'.\PHP_EOL;
foreach ($responses as $response) {
echo 'Next Letter: '.$response->getContent().\PHP_EOL;
echo 'Next Letter: '.$response->asText().\PHP_EOL;
}
4 changes: 1 addition & 3 deletions examples/parallel-embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Embeddings;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
Expand All @@ -29,6 +28,5 @@

echo 'Waiting for the responses ...'.\PHP_EOL;
foreach ($responses as $response) {
assert($response instanceof VectorResponse);
echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
}
2 changes: 1 addition & 1 deletion examples/transformers/text-generation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
'task' => Task::Text2TextGeneration,
]);

echo $response->getContent().\PHP_EOL;
echo $response->asText().\PHP_EOL;
5 changes: 1 addition & 4 deletions examples/voyage/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use PhpLlm\LlmChain\Platform\Bridge\Voyage\PlatformFactory;
use PhpLlm\LlmChain\Platform\Bridge\Voyage\Voyage;
use PhpLlm\LlmChain\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
Expand All @@ -22,6 +21,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
7 changes: 1 addition & 6 deletions src/Chain/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpLlm\LlmChain\Platform\Message\MessageBagInterface;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\PlatformInterface;
use PhpLlm\LlmChain\Platform\Response\AsyncResponse;
use PhpLlm\LlmChain\Platform\Response\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -69,11 +68,7 @@ public function call(MessageBagInterface $messages, array $options = []): Respon
}

try {
$response = $this->platform->request($model, $messages, $options);

if ($response instanceof AsyncResponse) {
$response = $response->unwrap();
}
$response = $this->platform->request($model, $messages, $options)->getResponse();
} catch (ClientExceptionInterface $e) {
$message = $e->getMessage();
$content = $e->getResponse()->toArray(false);
Expand Down
4 changes: 1 addition & 3 deletions src/Chain/Toolbox/Tool/SimilaritySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\PlatformInterface;
use PhpLlm\LlmChain\Platform\Vector\Vector;
use PhpLlm\LlmChain\Store\Document\VectorDocument;
use PhpLlm\LlmChain\Store\VectorStoreInterface;

Expand All @@ -34,8 +33,7 @@ public function __construct(
*/
public function __invoke(string $searchTerm): string
{
/** @var Vector[] $vectors */
$vectors = $this->platform->request($this->model, $searchTerm)->getContent();
$vectors = $this->platform->request($this->model, $searchTerm)->asVectors();
$this->usedDocuments = $this->vectorStore->query($vectors[0]);

if (0 === \count($this->usedDocuments)) {
Expand Down
9 changes: 3 additions & 6 deletions src/Platform/Bridge/Bedrock/Anthropic/ClaudeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\BedrockModelClient;
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Platform\Response\TextResponse;
use PhpLlm\LlmChain\Platform\Response\ToolCall;
use PhpLlm\LlmChain\Platform\Response\ToolCallResponse;
Expand All @@ -32,7 +31,7 @@ public function supports(Model $model): bool
return $model instanceof Claude;
}

public function request(Model $model, array|string $payload, array $options = []): LlmResponse
public function request(Model $model, array|string $payload, array $options = []): InvokeModelResponse
{
unset($payload['model']);

Expand All @@ -50,12 +49,10 @@ public function request(Model $model, array|string $payload, array $options = []
'body' => json_encode(array_merge($options, $payload), \JSON_THROW_ON_ERROR),
];

$invokeModelResponse = $this->bedrockRuntimeClient->invokeModel(new InvokeModelRequest($request));

return $this->convert($invokeModelResponse);
return $this->bedrockRuntimeClient->invokeModel(new InvokeModelRequest($request));
}

public function convert(InvokeModelResponse $bedrockResponse): LlmResponse
public function convert(InvokeModelResponse $bedrockResponse): ToolCallResponse|TextResponse
{
$data = json_decode($bedrockResponse->getBody(), true, 512, \JSON_THROW_ON_ERROR);

Expand Down
7 changes: 5 additions & 2 deletions src/Platform/Bridge/Bedrock/BedrockModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace PhpLlm\LlmChain\Platform\Bridge\Bedrock;

use AsyncAws\BedrockRuntime\Result\InvokeModelResponse;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Platform\Response\ResponseInterface;

/**
* @author Björn Altmann
Expand All @@ -18,5 +19,7 @@ public function supports(Model $model): bool;
* @param array<mixed>|string $payload
* @param array<string, mixed> $options
*/
public function request(Model $model, array|string $payload, array $options = []): LlmResponse;
public function request(Model $model, array|string $payload, array $options = []): InvokeModelResponse;

public function convert(InvokeModelResponse $bedrockResponse): ResponseInterface;
}
Loading