diff --git a/composer.json b/composer.json index 2d96f8a..39e1e4f 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index c5e2454..dbcb293 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -182,7 +182,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() - ->arrayNode('embedder') + ->arrayNode('indexer') ->normalizeKeys(false) ->useAttributeAsKey('name') ->arrayPrototype() diff --git a/src/DependencyInjection/LlmChainExtension.php b/src/DependencyInjection/LlmChainExtension.php index 03a8678..fa57275 100644 --- a/src/DependencyInjection/LlmChainExtension.php +++ b/src/DependencyInjection/LlmChainExtension.php @@ -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; @@ -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 { @@ -455,7 +456,7 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde /** * @param array $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']; @@ -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); } } diff --git a/src/Profiler/DataCollector.php b/src/Profiler/DataCollector.php index b95c981..d9b556f 100644 --- a/src/Profiler/DataCollector.php +++ b/src/Profiler/DataCollector.php @@ -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; @@ -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)), ]; } @@ -79,4 +80,23 @@ public function getToolCalls(): array { return $this->data['tool_calls'] ?? []; } + + /** + * @return array{ + * model: Model, + * input: array|string|object, + * options: array, + * response: string|iterable|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; + } } diff --git a/src/Profiler/TraceablePlatform.php b/src/Profiler/TraceablePlatform.php index 482df5a..7eb62b9 100644 --- a/src/Profiler/TraceablePlatform.php +++ b/src/Profiler/TraceablePlatform.php @@ -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|string|object, * options: array, - * response: ResponseInterface, + * response: ResponsePromise, * } */ final class TraceablePlatform implements PlatformInterface @@ -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); @@ -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;