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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
],
"require": {
"php": ">=8.2",
"php-llm/llm-chain": "^0.23",
"php-llm/llm-chain": "^0.24",
"symfony/config": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0"
},
"require-dev": {
"php-cs-fixer/shim": "^3.69",
"php-cs-fixer/shim": "^3.78",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^11.5",
"rector/rector": "^2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('embedder')
->arrayNode('indexer')
->normalizeKeys(false)
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
26 changes: 16 additions & 10 deletions src/DependencyInjection/LlmChainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
use PhpLlm\LlmChain\Store\Bridge\ChromaDB\Store as ChromaDBStore;
use PhpLlm\LlmChain\Store\Bridge\MongoDB\Store as MongoDBStore;
use PhpLlm\LlmChain\Store\Bridge\Pinecone\Store as PineconeStore;
use PhpLlm\LlmChain\Store\Embedder;
use PhpLlm\LlmChain\Store\Document\Vectorizer;
use PhpLlm\LlmChain\Store\Indexer;
use PhpLlm\LlmChain\Store\StoreInterface;
use PhpLlm\LlmChain\Store\VectorStoreInterface;
use PhpLlm\LlmChainBundle\Profiler\DataCollector;
Expand Down Expand Up @@ -98,11 +99,11 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setAlias(StoreInterface::class, reset($stores));
}

foreach ($config['embedder'] as $embedderName => $embedder) {
$this->processEmbedderConfig($embedderName, $embedder, $container);
foreach ($config['indexer'] as $indexerName => $indexer) {
$this->processIndexerConfig($indexerName, $indexer, $container);
}
if (1 === count($config['embedder']) && isset($embedderName)) {
$container->setAlias(Embedder::class, 'llm_chain.embedder.'.$embedderName);
if (1 === count($config['indexer']) && isset($indexerName)) {
$container->setAlias(Indexer::class, 'llm_chain.indexer.'.$indexerName);
}

$container->registerAttributeForAutoconfiguration(AsTool::class, static function (ChildDefinition $definition, AsTool $attribute): void {
Expand Down Expand Up @@ -455,7 +456,7 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
/**
* @param array<string, mixed> $config
*/
private function processEmbedderConfig(int|string $name, array $config, ContainerBuilder $container): void
private function processIndexerConfig(int|string $name, array $config, ContainerBuilder $container): void
{
['name' => $modelName, 'version' => $version, 'options' => $options] = $config['model'];

Expand All @@ -472,14 +473,19 @@ private function processEmbedderConfig(int|string $name, array $config, Containe
$modelDefinition->setArgument('$options', $options);
}
$modelDefinition->addTag('llm_chain.model.embeddings_model');
$container->setDefinition('llm_chain.embedder.'.$name.'.model', $modelDefinition);
$container->setDefinition('llm_chain.indexer.'.$name.'.model', $modelDefinition);

$definition = new Definition(Embedder::class, [
'$model' => new Reference('llm_chain.embedder.'.$name.'.model'),
$vectorizerDefinition = new Definition(Vectorizer::class, [
'$platform' => new Reference($config['platform']),
'$model' => new Reference('llm_chain.indexer.'.$name.'.model'),
]);
$container->setDefinition('llm_chain.indexer.'.$name.'.vectorizer', $vectorizerDefinition);

$definition = new Definition(Indexer::class, [
'$vectorizer' => new Reference('llm_chain.indexer.'.$name.'.vectorizer'),
'$store' => new Reference($config['store']),
]);

$container->setDefinition('llm_chain.embedder.'.$name, $definition);
$container->setDefinition('llm_chain.indexer.'.$name, $definition);
}
}
22 changes: 21 additions & 1 deletion src/Profiler/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChainBundle\Profiler;

use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\Tool\Tool;
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
{
$this->data = [
'tools' => $this->defaultToolBox->getTools(),
'platform_calls' => array_merge(...array_map(fn (TraceablePlatform $platform) => $platform->calls, $this->platforms)),
'platform_calls' => array_merge(...array_map($this->awaitCallResults(...), $this->platforms)),
'tool_calls' => array_merge(...array_map(fn (TraceableToolbox $toolbox) => $toolbox->calls, $this->toolboxes)),
];
}
Expand Down Expand Up @@ -79,4 +80,23 @@ public function getToolCalls(): array
{
return $this->data['tool_calls'] ?? [];
}

/**
* @return array{
* model: Model,
* input: array<mixed>|string|object,
* options: array<string, mixed>,
* response: string|iterable<mixed>|object|null
* }[]
*/
private function awaitCallResults(TraceablePlatform $platform): array
{
$calls = $platform->calls;
foreach ($calls as $key => $call) {
$call['response'] = $call['response']->await()->getContent();
$calls[$key] = $call;
}

return $calls;
}
}
8 changes: 4 additions & 4 deletions src/Profiler/TraceablePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
use PhpLlm\LlmChain\Platform\Message\Content\File;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\PlatformInterface;
use PhpLlm\LlmChain\Platform\Response\ResponseInterface;
use PhpLlm\LlmChain\Platform\Response\ResponsePromise;

/**
* @phpstan-type PlatformCallData array{
* model: Model,
* input: array<mixed>|string|object,
* options: array<string, mixed>,
* response: ResponseInterface,
* response: ResponsePromise,
* }
*/
final class TraceablePlatform implements PlatformInterface
Expand All @@ -29,7 +29,7 @@ public function __construct(
) {
}

public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface
public function request(Model $model, array|string|object $input, array $options = []): ResponsePromise
{
$response = $this->platform->request($model, $input, $options);

Expand All @@ -41,7 +41,7 @@ public function request(Model $model, array|string|object $input, array $options
'model' => $model,
'input' => is_object($input) ? clone $input : $input,
'options' => $options,
'response' => $response->getContent(),
'response' => $response,
];

return $response;
Expand Down