Skip to content

Commit ca77abb

Browse files
authored
feat: adopt after release 0.8 & 0.9 (#42)
1 parent 1a3d710 commit ca77abb

12 files changed

+614
-442
lines changed

README.md

+60-54
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,61 @@ composer require php-llm/llm-chain-bundle
1010

1111
## Configuration
1212

13+
### Simple Example with OpenAI
14+
1315
```yaml
1416
# config/packages/llm_chain.yaml
1517
llm_chain:
16-
platforms:
17-
azure_gpt:
18-
type: 'azure'
19-
base_url: '%env(AZURE_OPENAI_BASEURL)%'
20-
deployment: '%env(AZURE_OPENAI_GPT)%'
21-
api_key: '%env(AZURE_OPENAI_KEY)%'
22-
version: '%env(AZURE_OPENAI_VERSION)%'
23-
azure_embeddings:
24-
type: 'azure'
25-
base_url: '%env(AZURE_OPENAI_BASEURL)%'
26-
deployment: '%env(AZURE_OPENAI_EMBEDDINGS)%'
27-
api_key: '%env(AZURE_OPENAI_KEY)%'
28-
version: '%env(AZURE_OPENAI_VERSION)%'
18+
platform:
2919
openai:
30-
type: 'openai'
3120
api_key: '%env(OPENAI_API_KEY)%'
32-
llms:
33-
azure_gpt:
34-
platform: 'azure_gpt'
35-
original_gpt:
36-
platform: 'openai'
37-
embeddings:
38-
azure_embeddings:
39-
platform: 'azure_embeddings'
40-
original_embeddings:
41-
platform: 'openai'
42-
stores:
43-
azure_search:
44-
engine: 'azure-search'
45-
api_key: '%env(AZURE_SEARCH_KEY)%'
46-
endpoint: '%env(AZURE_SEARCH_ENDPOINT)%'
47-
index_name: '%env(AZURE_SEARCH_INDEX)%'
48-
api_version: '2024-07-01'
21+
chain:
22+
default:
23+
model:
24+
name: 'GPT'
25+
```
26+
27+
### Advanced Example with Anthropic, Azure and multiple chains
28+
```yaml
29+
# config/packages/llm_chain.yaml
30+
llm_chain:
31+
platform:
32+
anthropic:
33+
api_key: '%env(ANTHROPIC_API_KEY)%'
34+
azure:
35+
# multiple deployments possible
36+
gpt_deployment:
37+
base_url: '%env(AZURE_OPENAI_BASEURL)%'
38+
deployment: '%env(AZURE_OPENAI_GPT)%'
39+
api_key: '%env(AZURE_OPENAI_KEY)%'
40+
api_version: '%env(AZURE_GPT_VERSION)%'
41+
chain:
42+
rag:
43+
platform: 'llm_chain.platform.azure.gpt_deployment'
44+
model:
45+
name: 'GPT'
46+
version: 'gpt-4o-mini'
47+
tools:
48+
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch'
49+
research:
50+
platform: 'llm_chain.platform.anthropic'
51+
model:
52+
name: 'Claude'
53+
tools: # Unconfigured all tools are injected into the chain, use "[]" to have no tools.
54+
- 'PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia'
55+
store:
56+
# also azure_search, mongodb and pinecone are supported as store type
4957
chroma_db:
50-
engine: 'chroma-db'
51-
collection_name: '%env(CHROMA_COLLECTION)%'
52-
mongodb:
53-
engine: 'mongodb'
54-
database_name: '%env(MONGODB_DATABASE)%'
55-
collection_name: '%env(MONGODB_COLLECTION)%'
56-
index_name: '%env(MONGODB_INDEX)%'
57-
vector_field_name: 'vector'
58-
bulk_write: false
59-
pinecone:
60-
engine: 'pinecone'
61-
namespace: 'partition'
62-
filter: { 'key' : 'value' }
63-
top_k: 5
58+
# multiple collections possible per type
59+
default:
60+
collection: 'my_collection'
61+
embedder:
62+
default:
63+
# platform: 'llm_chain.platform.anthropic'
64+
# store: 'llm_chain.store.chroma_db.default'
65+
model:
66+
name: 'Embeddings'
67+
version: 'text-embedding-ada-002'
6468
```
6569
6670
## Usage
@@ -69,12 +73,14 @@ llm_chain:
6973
7074
Use the `Chain` service to leverage GPT:
7175
```php
72-
use PhpLlm\LlmChain\Chat;
76+
use PhpLlm\LlmChain\ChainInterface;
77+
use PhpLlm\LlmChain\Model\Message\Message;
78+
use PhpLlm\LlmChain\Model\Message\MessageBag;
7379
7480
final readonly class MyService
7581
{
7682
public function __construct(
77-
private Chain $chain,
83+
private ChainInterface $chain,
7884
) {
7985
}
8086
@@ -99,18 +105,18 @@ services:
99105
autowire: true
100106
autoconfigure: true
101107
102-
PhpLlm\LlmChain\ToolBox\Tool\Clock: ~
103-
PhpLlm\LlmChain\ToolBox\Tool\OpenMeteo: ~
104-
PhpLlm\LlmChain\ToolBox\Tool\SerpApi:
108+
PhpLlm\LlmChain\Chain\ToolBox\Tool\Clock: ~
109+
PhpLlm\LlmChain\Chain\ToolBox\Tool\OpenMeteo: ~
110+
PhpLlm\LlmChain\Chain\ToolBox\Tool\SerpApi:
105111
$apiKey: '%env(SERP_API_KEY)%'
106-
PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch: ~
107-
PhpLlm\LlmChain\ToolBox\Tool\Wikipedia: ~
108-
PhpLlm\LlmChain\ToolBox\Tool\YouTubeTranscriber: ~
112+
PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch: ~
113+
PhpLlm\LlmChain\Chain\ToolBox\Tool\Wikipedia: ~
114+
PhpLlm\LlmChain\Chain\ToolBox\Tool\YouTubeTranscriber: ~
109115
```
110116

111117
Custom tools can be registered by using the `#[AsTool]` attribute:
112118
```php
113-
use PhpLlm\LlmChain\ToolBox\AsTool;
119+
use PhpLlm\LlmChain\Chain\ToolBox\Attribute\AsTool;
114120
115121
#[AsTool('company_name', 'Provides the name of your company')]
116122
final class CompanyName

composer.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
],
1212
"require": {
1313
"php": ">=8.2",
14-
"php-llm/llm-chain": "0.7.*",
14+
"php-llm/llm-chain": "^0.9.3",
1515
"symfony/config": "^6.4 || ^7.0",
1616
"symfony/dependency-injection": "^6.4 || ^7.0",
1717
"symfony/framework-bundle": "^6.4 || ^7.0"
1818
},
1919
"require-dev": {
20-
"php-cs-fixer/shim": "^3.64",
20+
"php-cs-fixer/shim": "^3.65",
2121
"phpstan/phpstan": "^2.0",
22-
"phpunit/phpunit": "^11.3"
22+
"phpunit/phpunit": "^11.5"
2323
},
2424
"config": {
2525
"sort-packages": true
@@ -33,7 +33,5 @@
3333
"psr-4": {
3434
"PhpLlm\\LlmChainBundle\\Tests\\": "tests/"
3535
}
36-
},
37-
"minimum-stability": "dev",
38-
"prefer-stable": true
36+
}
3937
}

profiler.png

-23.8 KB
Loading

src/DependencyInjection/Configuration.php

+111-36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpLlm\LlmChainBundle\DependencyInjection;
66

7+
use PhpLlm\LlmChain\PlatformInterface;
8+
use PhpLlm\LlmChain\Store\StoreInterface;
79
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
810
use Symfony\Component\Config\Definition\ConfigurationInterface;
911

@@ -16,61 +18,134 @@ public function getConfigTreeBuilder(): TreeBuilder
1618

1719
$rootNode
1820
->children()
19-
->arrayNode('platforms')
20-
->normalizeKeys(false)
21-
->useAttributeAsKey('name')
22-
->arrayPrototype()
23-
->children()
24-
->enumNode('type')->values(['openai', 'azure'])->isRequired()->end()
25-
->scalarNode('api_key')->isRequired()->end()
26-
->scalarNode('base_url')->end()
27-
->scalarNode('deployment')->end()
28-
->scalarNode('version')->info('The used API version')->end()
21+
->arrayNode('platform')
22+
->children()
23+
->arrayNode('anthropic')
24+
->children()
25+
->scalarNode('api_key')->isRequired()->end()
26+
->scalarNode('version')->defaultNull()->end()
27+
->end()
28+
->end()
29+
->arrayNode('azure')
30+
->normalizeKeys(false)
31+
->useAttributeAsKey('name')
32+
->arrayPrototype()
33+
->children()
34+
->scalarNode('api_key')->isRequired()->end()
35+
->scalarNode('base_url')->isRequired()->end()
36+
->scalarNode('deployment')->isRequired()->end()
37+
->scalarNode('api_version')->info('The used API version')->end()
38+
->end()
39+
->end()
40+
->end()
41+
->arrayNode('openai')
42+
->children()
43+
->scalarNode('api_key')->isRequired()->end()
44+
->end()
2945
->end()
3046
->end()
3147
->end()
32-
->arrayNode('llms')
48+
->arrayNode('chain')
3349
->normalizeKeys(false)
3450
->useAttributeAsKey('name')
3551
->arrayPrototype()
3652
->children()
37-
->scalarNode('platform')->end()
53+
->scalarNode('platform')
54+
->info('Service name of platform')
55+
->defaultValue(PlatformInterface::class)
56+
->end()
57+
->arrayNode('model')
58+
->children()
59+
->scalarNode('name')->isRequired()->end()
60+
->scalarNode('version')->defaultNull()->end()
61+
->arrayNode('options')
62+
->scalarPrototype()->end()
63+
->end()
64+
->end()
65+
->end()
66+
->arrayNode('tools')
67+
->scalarPrototype()->end()
68+
->end()
3869
->end()
3970
->end()
4071
->end()
41-
->arrayNode('embeddings')
42-
->normalizeKeys(false)
43-
->useAttributeAsKey('name')
44-
->arrayPrototype()
45-
->children()
46-
->scalarNode('platform')->end()
72+
->arrayNode('store')
73+
->children()
74+
->arrayNode('azure_search')
75+
->normalizeKeys(false)
76+
->useAttributeAsKey('name')
77+
->arrayPrototype()
78+
->children()
79+
->scalarNode('endpoint')->isRequired()->end()
80+
->scalarNode('api_key')->isRequired()->end()
81+
->scalarNode('index_name')->isRequired()->end()
82+
->scalarNode('api_version')->isRequired()->end()
83+
->scalarNode('vector_field')->isRequired()->end()
84+
->end()
85+
->end()
86+
->end()
87+
->arrayNode('chroma_db')
88+
->normalizeKeys(false)
89+
->useAttributeAsKey('name')
90+
->arrayPrototype()
91+
->children()
92+
->scalarNode('collection')->isRequired()->end()
93+
->end()
94+
->end()
95+
->end()
96+
->arrayNode('mongodb')
97+
->normalizeKeys(false)
98+
->useAttributeAsKey('name')
99+
->arrayPrototype()
100+
->children()
101+
->scalarNode('database')->isRequired()->end()
102+
->scalarNode('collection')->isRequired()->end()
103+
->scalarNode('index_name')->isRequired()->end()
104+
->scalarNode('vector_field')->isRequired()->end()
105+
->booleanNode('bulk_write')->isRequired()->end()
106+
->end()
107+
->end()
108+
->end()
109+
->arrayNode('pinecone')
110+
->normalizeKeys(false)
111+
->useAttributeAsKey('name')
112+
->arrayPrototype()
113+
->children()
114+
->scalarNode('namespace')->end()
115+
->arrayNode('filter')
116+
->scalarPrototype()->end()
117+
->end()
118+
->integerNode('top_k')->defaultValue(3)->end()
119+
->end()
120+
->end()
47121
->end()
48122
->end()
49123
->end()
50-
->arrayNode('stores')
124+
->arrayNode('embedder')
51125
->normalizeKeys(false)
52126
->useAttributeAsKey('name')
53127
->arrayPrototype()
54128
->children()
55-
->enumNode('engine')->values(['azure-search', 'chroma-db', 'mongodb', 'pinecone'])->isRequired()->end()
56-
// Azure AI Search & MongoDB
57-
->scalarNode('index_name')->end()
58-
// Azure AI Search
59-
->scalarNode('api_key')->end()
60-
->scalarNode('api_version')->end()
61-
->scalarNode('endpoint')->end()
62-
// ChromaDB & MongoDB
63-
->scalarNode('collection_name')->end()
64-
// MongoDB
65-
->scalarNode('database_name')->end()
66-
->scalarNode('vector_field_name')->defaultValue('vector')->end()
67-
->booleanNode('bulk_write')->defaultValue(false)->end()
68-
// Pinecone
69-
->arrayNode('filter')->end()
70-
->scalarNode('namespace')->end()
71-
->scalarNode('top_k')->end()
129+
->scalarNode('store')
130+
->info('Service name of store')
131+
->defaultValue(StoreInterface::class)
132+
->end()
133+
->scalarNode('platform')
134+
->info('Service name of platform')
135+
->defaultValue(PlatformInterface::class)
136+
->end()
137+
->arrayNode('model')
138+
->children()
139+
->scalarNode('name')->isRequired()->end()
140+
->scalarNode('version')->defaultNull()->end()
141+
->arrayNode('options')
142+
->scalarPrototype()->end()
143+
->end()
144+
->end()
145+
->end()
72146
->end()
73147
->end()
148+
->end()
74149
->end()
75150
;
76151

0 commit comments

Comments
 (0)