Skip to content

Commit 81beb5c

Browse files
authored
feat: update for 0.16 - gemini & fault tolerant toolbox (#67)
1 parent ddc3a22 commit 81beb5c

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ llm_chain:
2424
name: 'GPT'
2525
```
2626
27-
### Advanced Example with Anthropic, Azure and multiple chains
27+
### Advanced Example with Anthropic, Azure, Google and multiple chains
2828
```yaml
2929
# config/packages/llm_chain.yaml
3030
llm_chain:
@@ -38,6 +38,8 @@ llm_chain:
3838
deployment: '%env(AZURE_OPENAI_GPT)%'
3939
api_key: '%env(AZURE_OPENAI_KEY)%'
4040
api_version: '%env(AZURE_GPT_VERSION)%'
41+
google:
42+
api_key: '%env(GOOGLE_API_KEY)%'
4143
chain:
4244
rag:
4345
platform: 'llm_chain.platform.azure.gpt_deployment'
@@ -54,6 +56,7 @@ llm_chain:
5456
name: 'Claude'
5557
tools: # If undefined, all tools are injected into the chain, use "tools: false" to disable tools.
5658
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia'
59+
fault_tolerant_toolbox: false # Disables fault tolerant toolbox, default is true
5760
store:
5861
# also azure_search, mongodb and pinecone are supported as store type
5962
chroma_db:
@@ -112,6 +115,8 @@ services:
112115
PhpLlm\LlmChain\Chain\ToolBox\Tool\SerpApi:
113116
$apiKey: '%env(SERP_API_KEY)%'
114117
PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch: ~
118+
PhpLlm\LlmChain\Chain\ToolBox\Tool\Tavily:
119+
$apiKey: '%env(TAVILY_API_KEY)%'
115120
PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia: ~
116121
PhpLlm\LlmChain\Chain\ToolBox\Tool\YouTubeTranscriber: ~
117122
```

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
],
1212
"require": {
1313
"php": ">=8.2",
14-
"php-llm/llm-chain": "^0.15",
14+
"php-llm/llm-chain": "^0.16",
1515
"symfony/config": "^6.4 || ^7.0",
1616
"symfony/dependency-injection": "^6.4 || ^7.0",
1717
"symfony/framework-bundle": "^6.4 || ^7.0",
1818
"symfony/string": "^6.4 || ^7.0"
1919
},
2020
"require-dev": {
21-
"php-cs-fixer/shim": "^3.68",
21+
"php-cs-fixer/shim": "^3.69",
2222
"phpstan/phpstan": "^2.1",
2323
"phpunit/phpunit": "^11.5"
2424
},

src/DependencyInjection/Configuration.php

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function getConfigTreeBuilder(): TreeBuilder
3838
->end()
3939
->end()
4040
->end()
41+
->arrayNode('google')
42+
->children()
43+
->scalarNode('api_key')->isRequired()->end()
44+
->end()
45+
->end()
4146
->arrayNode('openai')
4247
->children()
4348
->scalarNode('api_key')->isRequired()->end()
@@ -93,6 +98,7 @@ public function getConfigTreeBuilder(): TreeBuilder
9398
->end()
9499
->end()
95100
->end()
101+
->booleanNode('fault_tolerant_toolbox')->defaultTrue()->end()
96102
->end()
97103
->end()
98104
->end()

src/DependencyInjection/LlmChainExtension.php

+40-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use PhpLlm\LlmChain\Bridge\Azure\OpenAI\PlatformFactory as AzureOpenAIPlatformFactory;
1010
use PhpLlm\LlmChain\Bridge\Azure\Store\SearchStore as AzureSearchStore;
1111
use PhpLlm\LlmChain\Bridge\ChromaDB\Store as ChromaDBStore;
12+
use PhpLlm\LlmChain\Bridge\Google\Gemini;
13+
use PhpLlm\LlmChain\Bridge\Google\PlatformFactory as GooglePlatformFactory;
1214
use PhpLlm\LlmChain\Bridge\Meta\Llama;
1315
use PhpLlm\LlmChain\Bridge\MongoDB\Store as MongoDBStore;
1416
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
@@ -23,6 +25,7 @@
2325
use PhpLlm\LlmChain\Chain\StructuredOutput\ChainProcessor as StructureOutputProcessor;
2426
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
2527
use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor as ToolProcessor;
28+
use PhpLlm\LlmChain\Chain\ToolBox\FaultTolerantToolBox;
2629
use PhpLlm\LlmChain\ChainInterface;
2730
use PhpLlm\LlmChain\Embedder;
2831
use PhpLlm\LlmChain\Model\EmbeddingsModel;
@@ -132,16 +135,22 @@ public function load(array $configs, ContainerBuilder $container): void
132135
*/
133136
private function processPlatformConfig(string $type, array $platform, ContainerBuilder $container): void
134137
{
135-
if ('openai' === $type) {
136-
$platformId = 'llm_chain.platform.openai';
138+
if ('anthropic' === $type) {
139+
$platformId = 'llm_chain.platform.anthropic';
137140
$definition = (new Definition(Platform::class))
138-
->setFactory(OpenAIPlatformFactory::class.'::create')
141+
->setFactory(AnthropicPlatformFactory::class.'::create')
139142
->setAutowired(true)
140143
->setLazy(true)
141144
->addTag('proxy', ['interface' => PlatformInterface::class])
142-
->setArguments(['$apiKey' => $platform['api_key']])
145+
->setArguments([
146+
'$apiKey' => $platform['api_key'],
147+
])
143148
->addTag('llm_chain.platform');
144149

150+
if (isset($platform['version'])) {
151+
$definition->replaceArgument('$version', $platform['version']);
152+
}
153+
145154
$container->setDefinition($platformId, $definition);
146155

147156
return;
@@ -169,21 +178,30 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
169178
return;
170179
}
171180

172-
if ('anthropic' === $type) {
173-
$platformId = 'llm_chain.platform.anthropic';
181+
if ('google' === $type) {
182+
$platformId = 'llm_chain.platform.google';
174183
$definition = (new Definition(Platform::class))
175-
->setFactory(AnthropicPlatformFactory::class.'::create')
184+
->setFactory(GooglePlatformFactory::class.'::create')
176185
->setAutowired(true)
177186
->setLazy(true)
178187
->addTag('proxy', ['interface' => PlatformInterface::class])
179-
->setArguments([
180-
'$apiKey' => $platform['api_key'],
181-
])
188+
->setArguments(['$apiKey' => $platform['api_key']])
182189
->addTag('llm_chain.platform');
183190

184-
if (isset($platform['version'])) {
185-
$definition->replaceArgument('$version', $platform['version']);
186-
}
191+
$container->setDefinition($platformId, $definition);
192+
193+
return;
194+
}
195+
196+
if ('openai' === $type) {
197+
$platformId = 'llm_chain.platform.openai';
198+
$definition = (new Definition(Platform::class))
199+
->setFactory(OpenAIPlatformFactory::class.'::create')
200+
->setAutowired(true)
201+
->setLazy(true)
202+
->addTag('proxy', ['interface' => PlatformInterface::class])
203+
->setArguments(['$apiKey' => $platform['api_key']])
204+
->addTag('llm_chain.platform');
187205

188206
$container->setDefinition($platformId, $definition);
189207

@@ -205,6 +223,7 @@ private function processChainConfig(string $name, array $config, ContainerBuilde
205223
'gpt' => GPT::class,
206224
'claude' => Claude::class,
207225
'llama' => Llama::class,
226+
'gemini' => Gemini::class,
208227
default => throw new \InvalidArgumentException(sprintf('Model "%s" is not supported.', $modelName)),
209228
};
210229
$llmDefinition = new Definition($llmClass);
@@ -235,6 +254,14 @@ private function processChainConfig(string $name, array $config, ContainerBuilde
235254
->replaceArgument('$tools', $tools);
236255
$container->setDefinition('llm_chain.toolbox.'.$name, $toolboxDefinition);
237256

257+
if ($config['fault_tolerant_toolbox']) {
258+
$faultTolerantToolboxDefinition = (new Definition('llm_chain.fault_tolerant_toolbox.'.$name))
259+
->setClass(FaultTolerantToolBox::class)
260+
->setAutowired(true)
261+
->setDecoratedService('llm_chain.toolbox.'.$name);
262+
$container->setDefinition('llm_chain.fault_tolerant_toolbox.'.$name, $faultTolerantToolboxDefinition);
263+
}
264+
238265
if ($container->getParameter('kernel.debug')) {
239266
$traceableToolboxDefinition = (new Definition('llm_chain.traceable_toolbox.'.$name))
240267
->setClass(TraceableToolBox::class)

0 commit comments

Comments
 (0)