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

Commit 322011b

Browse files
authored
fix: adopt to lib changes and bring in pipeline (#15)
1 parent 2004ec6 commit 322011b

File tree

11 files changed

+156
-44
lines changed

11 files changed

+156
-44
lines changed

.github/workflows/pipeline.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: pipeline
2+
on: pull_request
3+
4+
jobs:
5+
pipeline:
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
php: ['8.2', '8.3']
10+
dependencies: ['lowest', 'highest']
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Setup PHP
16+
uses: shivammathur/setup-php@v2
17+
with:
18+
php-version: ${{ matrix.php }}
19+
20+
- name: Install Composer
21+
uses: "ramsey/composer-install@v3"
22+
with:
23+
dependency-versions: "${{ matrix.dependencies }}"
24+
25+
- name: Composer Validation
26+
run: composer validate --strict
27+
28+
- name: Install PHP Dependencies
29+
run: composer install --no-scripts
30+
31+
- name: Code Style PHP
32+
run: vendor/bin/php-cs-fixer fix --dry-run
33+
34+
- name: PHPStan
35+
run: vendor/bin/phpstan analyse

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
22
composer.lock
3+
.php-cs-fixer.cache

.php-cs-fixer.dist.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$finder = (new PhpCsFixer\Finder())
4+
->in(__DIR__)
5+
;
6+
7+
return (new PhpCsFixer\Config())
8+
->setRules([
9+
'@Symfony' => true,
10+
])
11+
->setFinder($finder)
12+
;

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
qa:
2+
composer update --prefer-stable
3+
vendor/bin/php-cs-fixer fix
4+
vendor/bin/phpstan
5+
6+
qa-lowest:
7+
composer update --prefer-lowest
8+
vendor/bin/php-cs-fixer fix
9+
vendor/bin/phpstan

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@
2222
}
2323
},
2424
"minimum-stability": "dev",
25-
"prefer-stable": true
25+
"prefer-stable": true,
26+
"require-dev": {
27+
"php-cs-fixer/shim": "^3.64",
28+
"phpstan/phpstan": "^1.12"
29+
}
2630
}

phpstan.dist.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src/
5+
excludePaths:
6+
analyse:
7+
- src/DependencyInjection/Configuration.php

src/DataCollector.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
namespace PhpLlm\LlmChainBundle;
66

7+
use PhpLlm\LlmChain\ToolBox\Metadata;
78
use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
89
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
910
use Symfony\Component\HttpFoundation\Request;
1011
use Symfony\Component\HttpFoundation\Response;
1112

13+
/**
14+
* @phpstan-import-type LlmCallData from TraceableLanguageModel
15+
* @phpstan-import-type ToolCallData from TraceableToolBox
16+
*/
1217
final class DataCollector extends AbstractDataCollector
1318
{
1419
/**
@@ -22,7 +27,7 @@ final class DataCollector extends AbstractDataCollector
2227
public function __construct(
2328
#[AutowireIterator('llm_chain.traceable_llm')]
2429
iterable $llms,
25-
private TraceableToolRegistry $toolRegistry,
30+
private TraceableToolBox $toolBox,
2631
) {
2732
$this->llms = $llms instanceof \Traversable ? iterator_to_array($llms) : $llms;
2833
}
@@ -35,9 +40,9 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
3540
}
3641

3742
$this->data = [
38-
'tools' => $this->toolRegistry->getMap(),
43+
'tools' => $this->toolBox->getMap(),
3944
'llm_calls' => $llmCalls,
40-
'tool_calls' => $this->toolRegistry->calls,
45+
'tool_calls' => $this->toolBox->calls,
4146
];
4247
}
4348

@@ -46,7 +51,9 @@ public static function getTemplate(): string
4651
return '@LlmChain/data_collector.html.twig';
4752
}
4853

49-
54+
/**
55+
* @return list<LlmCallData>
56+
*/
5057
public function getLlmCalls(): array
5158
{
5259
return $this->data['llm_calls'] ?? [];
@@ -57,11 +64,17 @@ public function getLlmCallCount(): int
5764
return array_sum(array_map('count', $this->data['llm_calls'] ?? []));
5865
}
5966

67+
/**
68+
* @return Metadata[]
69+
*/
6070
public function getTools(): array
6171
{
6272
return $this->data['tools'] ?? [];
6373
}
6474

75+
/**
76+
* @return list<ToolCallData>
77+
*/
6578
public function getToolCalls(): array
6679
{
6780
return $this->data['tool_calls'] ?? [];

src/DependencyInjection/LlmChainExtension.php

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use PhpLlm\LlmChain\ToolBox\AsTool;
1919
use PhpLlm\LlmChainBundle\DataCollector;
2020
use PhpLlm\LlmChainBundle\TraceableLanguageModel;
21-
use PhpLlm\LlmChainBundle\TraceableToolRegistry;
21+
use PhpLlm\LlmChainBundle\TraceableToolBox;
2222
use Symfony\Component\Config\FileLocator;
2323
use Symfony\Component\DependencyInjection\ChildDefinition;
2424
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -37,33 +37,33 @@ public function load(array $configs, ContainerBuilder $container): void
3737
$configuration = new Configuration();
3838
$config = $this->processConfiguration($configuration, $configs);
3939

40-
foreach ($config['runtimes'] as $name => $runtime) {
41-
$this->processRuntimeConfig($name, $runtime, $container);
40+
foreach ($config['runtimes'] as $runtimeName => $runtime) {
41+
$this->processRuntimeConfig($runtimeName, $runtime, $container);
4242
}
43-
if (1 === count($config['runtimes'])) {
44-
$container->setAlias(Runtime::class, 'llm_chain.runtime.'.$name);
43+
if (1 === count($config['runtimes']) && isset($runtimeName)) {
44+
$container->setAlias(Runtime::class, 'llm_chain.runtime.'.$runtimeName);
4545
}
4646

47-
foreach ($config['llms'] as $name => $llm) {
48-
$this->processLlmConfig($name, $llm, $container);
47+
foreach ($config['llms'] as $llmName => $llm) {
48+
$this->processLlmConfig($llmName, $llm, $container);
4949
}
50-
if (1 === count($config['llms'])) {
51-
$container->setAlias(LanguageModel::class, 'llm_chain.llm.'.$name);
50+
if (1 === count($config['llms']) && isset($llmName)) {
51+
$container->setAlias(LanguageModel::class, 'llm_chain.llm.'.$llmName);
5252
}
5353

54-
foreach ($config['embeddings'] as $name => $embeddings) {
55-
$this->processEmbeddingsConfig($name, $embeddings, $container);
54+
foreach ($config['embeddings'] as $embeddingsName => $embeddings) {
55+
$this->processEmbeddingsConfig($embeddingsName, $embeddings, $container);
5656
}
57-
if (1 === count($config['embeddings'])) {
58-
$container->setAlias(EmbeddingModel::class, 'llm_chain.embeddings.'.$name);
57+
if (1 === count($config['embeddings']) && isset($embeddingsName)) {
58+
$container->setAlias(EmbeddingModel::class, 'llm_chain.embeddings.'.$embeddingsName);
5959
}
6060

61-
foreach ($config['stores'] as $name => $store) {
62-
$this->processStoreConfig($name, $store, $container);
61+
foreach ($config['stores'] as $storeName => $store) {
62+
$this->processStoreConfig($storeName, $store, $container);
6363
}
64-
if (1 === count($config['stores'])) {
65-
$container->setAlias(VectorStoreInterface::class, 'llm_chain.store.'.$name);
66-
$container->setAlias(StoreInterface::class, 'llm_chain.store.'.$name);
64+
if (1 === count($config['stores']) && isset($storeName)) {
65+
$container->setAlias(VectorStoreInterface::class, 'llm_chain.store.'.$storeName);
66+
$container->setAlias(StoreInterface::class, 'llm_chain.store.'.$storeName);
6767
}
6868

6969
$container->registerAttributeForAutoconfiguration(AsTool::class, static function (ChildDefinition $definition, AsTool $attribute): void {
@@ -76,10 +76,13 @@ public function load(array $configs, ContainerBuilder $container): void
7676

7777
if (false === $container->getParameter('kernel.debug')) {
7878
$container->removeDefinition(DataCollector::class);
79-
$container->removeDefinition(TraceableToolRegistry::class);
79+
$container->removeDefinition(TraceableToolBox::class);
8080
}
8181
}
8282

83+
/**
84+
* @param array<string, mixed> $runtime
85+
*/
8386
private function processRuntimeConfig(string $name, array $runtime, ContainerBuilder $container): void
8487
{
8588
if ('openai' === $runtime['type']) {
@@ -104,6 +107,9 @@ private function processRuntimeConfig(string $name, array $runtime, ContainerBui
104107
}
105108
}
106109

110+
/**
111+
* @param array<string, mixed> $llm
112+
*/
107113
private function processLlmConfig(string $name, array $llm, ContainerBuilder $container): void
108114
{
109115
$runtime = isset($llm['runtime']) ? 'llm_chain.runtime.'.$llm['runtime'] : Runtime::class;
@@ -123,7 +129,10 @@ private function processLlmConfig(string $name, array $llm, ContainerBuilder $co
123129
}
124130
}
125131

126-
private function processEmbeddingsConfig(string $name, mixed $embeddings, ContainerBuilder $container): void
132+
/**
133+
* @param array<string, mixed> $embeddings
134+
*/
135+
private function processEmbeddingsConfig(string $name, array $embeddings, ContainerBuilder $container): void
127136
{
128137
$runtime = isset($embeddings['runtime']) ? 'llm_chain.runtime.'.$embeddings['runtime'] : Runtime::class;
129138

@@ -133,7 +142,10 @@ private function processEmbeddingsConfig(string $name, mixed $embeddings, Contai
133142
$container->setDefinition('llm_chain.embeddings.'.$name, $definition);
134143
}
135144

136-
private function processStoreConfig(string $name, mixed $stores, ContainerBuilder $container): void
145+
/**
146+
* @param array<string, mixed> $stores
147+
*/
148+
private function processStoreConfig(string $name, array $stores, ContainerBuilder $container): void
137149
{
138150
if ('chroma-db' === $stores['engine']) {
139151
$definition = new ChildDefinition(ChromaDbStore::class);

src/Resources/config/services.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
use PhpLlm\LlmChain\OpenAI\Runtime;
1212
use PhpLlm\LlmChain\OpenAI\Runtime\Azure as AzureRuntime;
1313
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI as OpenAIRuntime;
14-
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
15-
use PhpLlm\LlmChain\ToolBox\Registry;
16-
use PhpLlm\LlmChain\ToolBox\RegistryInterface;
17-
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1814
use PhpLlm\LlmChain\Store\Azure\SearchStore as AzureSearchStore;
1915
use PhpLlm\LlmChain\Store\ChromaDb\Store as ChromaDbStore;
16+
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
17+
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
18+
use PhpLlm\LlmChain\ToolBox\ToolBox;
19+
use PhpLlm\LlmChain\ToolBox\ToolBoxInterface;
2020
use PhpLlm\LlmChainBundle\DataCollector;
21-
use PhpLlm\LlmChainBundle\TraceableToolRegistry;
21+
use PhpLlm\LlmChainBundle\TraceableToolBox;
2222

2323
return static function (ContainerConfigurator $container) {
2424
$container->services()
@@ -73,17 +73,17 @@
7373
])
7474

7575
// tools
76-
->set(Registry::class)
76+
->set(ToolBox::class)
7777
->args([
7878
'$tools' => tagged_iterator('llm_chain.tool'),
7979
])
80-
->alias(RegistryInterface::class, Registry::class)
80+
->alias(ToolBoxInterface::class, ToolBox::class)
8181
->set(ToolAnalyzer::class)
8282
->set(ParameterAnalyzer::class)
8383

8484
// profiler
8585
->set(DataCollector::class)
86-
->set(TraceableToolRegistry::class)
87-
->decorate(Registry::class)
86+
->set(TraceableToolBox::class)
87+
->decorate(ToolBox::class)
8888
;
8989
};

src/TraceableLanguageModel.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88
use PhpLlm\LlmChain\Message\MessageBag;
99
use PhpLlm\LlmChain\Response\Response;
1010

11+
/**
12+
* @phpstan-type LlmCallData array{
13+
* messages: MessageBag,
14+
* options: array<string, mixed>,
15+
* response: Response,
16+
* }
17+
*/
1118
final class TraceableLanguageModel implements LanguageModel
1219
{
20+
/**
21+
* @var list<LlmCallData>
22+
*/
1323
public array $calls = [];
1424

1525
public function __construct(
@@ -31,14 +41,14 @@ public function call(MessageBag $messages, array $options = []): Response
3141
return $response;
3242
}
3343

34-
public function hasToolSupport(): bool
44+
public function supportsToolCalling(): bool
3545
{
36-
return $this->llm->hasToolSupport();
46+
return $this->llm->supportsToolCalling();
3747
}
3848

39-
public function hasStructuredOutputSupport(): bool
49+
public function supportsStructuredOutput(): bool
4050
{
41-
return $this->llm->hasStructuredOutputSupport();
51+
return $this->llm->supportsStructuredOutput();
4252
}
4353

4454
public function getName(): string

0 commit comments

Comments
 (0)