@@ -41,27 +41,26 @@ Those models are provided by different **platforms**, like OpenAI, Azure, Replic
4141#### Example Instantiation
4242
4343``` php
44- use PhpLlm\LlmChain\OpenAI\Model\Embeddings;
45- use PhpLlm\LlmChain\OpenAI\Model\Gpt;
46- use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
47- use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
48- use Symfony\Component\HttpClient\HttpClient;
44+ use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
45+ use PhpLlm\LlmChain\Bridge\OpenAI\GPT;
46+ use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory;
4947
5048// Platform: OpenAI
51- $platform = new OpenAI(HttpClient ::create(), $_ENV['OPENAI_API_KEY']);
49+ $platform = PlatformFactory ::create($_ENV['OPENAI_API_KEY']);
5250
5351// Language Model: GPT (OpenAI)
54- $llm = new Gpt($platform, Version::gpt4oMini() );
52+ $llm = new GPT(GPT::GPT_4O_MINI );
5553
5654// Embeddings Model: Embeddings (OpenAI)
57- $embeddings = new Embeddings($platform );
55+ $embeddings = new Embeddings();
5856```
5957
6058#### Supported Models & Platforms
6159
6260* Language Models
6361 * [ OpenAI's GPT] ( https://platform.openai.com/docs/models/overview ) with [ OpenAI] ( https://platform.openai.com/docs/overview ) and [ Azure] ( https://learn.microsoft.com/azure/ai-services/openai/concepts/models ) as Platform
6462 * [ Anthropic's Claude] ( https://www.anthropic.com/claude ) with [ Anthropic] ( https://www.anthropic.com/ ) as Platform
63+ * [ Meta's Llama] ( https://www.llama.com/ ) with [ Ollama] ( https://ollama.com/ ) and [ Replicate] ( https://replicate.com/ ) as Platform
6564* Embeddings Models
6665 * [ OpenAI's Text Embeddings] ( https://platform.openai.com/docs/guides/embeddings/embedding-models ) with [ OpenAI] ( https://platform.openai.com/docs/overview ) and [ Azure] ( https://learn.microsoft.com/azure/ai-services/openai/concepts/models ) as Platform
6766 * [ Voyage's Embeddings] ( https://docs.voyageai.com/docs/embeddings ) with [ Voyage] ( https://www.voyageai.com/ ) as Platform
@@ -71,7 +70,7 @@ See [issue #28](https://github.com/php-llm/llm-chain/issues/28) for planned supp
7170### Chain & Messages
7271
7372The core feature of LLM Chain is to interact with language models via messages. This interaction is done by sending
74- a ** MessageBag** to a ** Chain** , which takes care of LLM invokation and response handling.
73+ a ** MessageBag** to a ** Chain** , which takes care of LLM invocation and response handling.
7574
7675Messages can be of different types, most importantly ` UserMessage ` , ` SystemMessage ` , or ` AssistantMessage ` , and can also
7776have different content types, like ` Text ` or ` Image ` .
@@ -80,13 +79,13 @@ have different content types, like `Text` or `Image`.
8079
8180``` php
8281use PhpLlm\LlmChain\Chain;
83- use PhpLlm\LlmChain\Message\MessageBag;
84- use PhpLlm\LlmChain\Message\SystemMessage;
85- use PhpLlm\LlmChain\Message\UserMessage;
82+ use PhpLlm\LlmChain\Model\ Message\MessageBag;
83+ use PhpLlm\LlmChain\Model\ Message\SystemMessage;
84+ use PhpLlm\LlmChain\Model\ Message\UserMessage;
8685
87- // LLM instantiation
86+ // Platform & LLM instantiation
8887
89- $chain = new Chain($llm);
88+ $chain = new Chain($platform, $ llm);
9089$messages = new MessageBag(
9190 new SystemMessage('You are a helpful chatbot answering questions about LLM Chain.'),
9291 new UserMessage('Hello, how are you?'),
@@ -104,6 +103,8 @@ The `MessageInterface` and `Content` interface help to customize this process if
1041031 . ** OpenAI's GPT with Azure** : [ chat-gpt-azure.php] ( examples/chat-gpt-azure.php )
1051041 . ** OpenAI's GPT** : [ chat-gpt-openai.php] ( examples/chat-gpt-openai.php )
1061051 . ** OpenAI's o1** : [ chat-o1-openai.php] ( examples/chat-o1-openai.php )
106+ 1 . ** Meta's Llama with Ollama** : [ chat-llama-ollama.php] ( examples/chat-llama-ollama.php )
107+ 1 . ** Meta's Llama with Replicate** : [ chat-llama-replicate.php] ( examples/chat-llama-replicate.php )
107108
108109### Tools
109110
@@ -112,19 +113,21 @@ Tools are services that can be called by the LLM to provide additional features
112113
113114Tool calling can be enabled by registering the processors in the chain:
114115``` php
115- use PhpLlm\LlmChain\ToolBox\ChainProcessor;
116- use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
117- use PhpLlm\LlmChain\ToolBox\ToolBox;
116+ use PhpLlm\LlmChain\Chain\ ToolBox\ChainProcessor;
117+ use PhpLlm\LlmChain\Chain\ ToolBox\ToolAnalyzer;
118+ use PhpLlm\LlmChain\Chain\ ToolBox\ToolBox;
118119use Symfony\Component\Serializer\Encoder\JsonEncoder;
119120use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
120121use Symfony\Component\Serializer\Serializer;
121122
123+ // Platform & LLM instantiation
124+
122125$yourTool = new YourTool();
123126
124127$toolBox = new ToolBox(new ToolAnalyzer(), [$yourTool]);
125128$toolProcessor = new ChainProcessor($toolBox);
126129
127- $chain = new Chain($llm, inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
130+ $chain = new Chain($platform, $ llm, inputProcessor: [$toolProcessor], outputProcessor: [$toolProcessor]);
128131```
129132
130133Custom tools can basically be any class, but must configure by the ` #[AsTool] ` attribute.
@@ -159,15 +162,16 @@ For populating a vector store, LLM Chain provides the service `DocumentEmbedder`
159162` EmbeddingsModel ` and one of ` StoreInterface ` , and works with a collection of ` Document ` objects as input:
160163
161164``` php
162- use PhpLlm\LlmChain\DocumentEmbedder ;
163- use PhpLlm\LlmChain\OpenAI\Model \Embeddings;
164- use PhpLlm\LlmChain\OpenAI\Platform\ OpenAI;
165- use PhpLlm\LlmChain\Store \Pinecone\Store;
165+ use PhpLlm\LlmChain\Embedder ;
166+ use PhpLlm\LlmChain\Bridge\OpenAI \Embeddings;
167+ use PhpLlm\LlmChain\Bridge\ OpenAI\PlatformFactory ;
168+ use PhpLlm\LlmChain\Bridge \Pinecone\Store;
166169use Probots\Pinecone\Pinecone;
167170use Symfony\Component\HttpClient\HttpClient;
168171
169- $embedder = new DocumentEmbedder(
170- new Embeddings(new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);),
172+ $embedder = new Embedder(
173+ PlatformFactory::create($_ENV['OPENAI_API_KEY']),
174+ new Embeddings(),
171175 new Store(Pinecone::client($_ENV['PINECONE_API_KEY'], $_ENV['PINECONE_HOST']),
172176);
173177$embedder->embed($documents);
@@ -196,20 +200,19 @@ In the end the chain is used in combination with a retrieval tool on top of the
196200
197201``` php
198202use PhpLlm\LlmChain\Chain;
199- use PhpLlm\LlmChain\DocumentEmbedder;
200- use PhpLlm\LlmChain\Message\Message;
201- use PhpLlm\LlmChain\Message\MessageBag;
202- use PhpLlm\LlmChain\ToolBox\ChainProcessor;
203- use PhpLlm\LlmChain\ToolBox\Tool\SimilaritySearch;
204- use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
205- use PhpLlm\LlmChain\ToolBox\ToolBox;
203+ use PhpLlm\LlmChain\Model\Message\Message;
204+ use PhpLlm\LlmChain\Model\Message\MessageBag;
205+ use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor;
206+ use PhpLlm\LlmChain\Chain\ToolBox\Tool\SimilaritySearch;
207+ use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer;
208+ use PhpLlm\LlmChain\Chain\ToolBox\ToolBox;
206209
207- // Initialize Platform and LLM
210+ // Initialize Platform & Models
208211
209212$similaritySearch = new SimilaritySearch($embeddings, $store);
210213$toolBox = new ToolBox(new ToolAnalyzer(), [$similaritySearch]);
211214$processor = new ChainProcessor($toolBox);
212- $chain = new Chain(new Gpt( $platform) , [$processor], [$processor]);
215+ $chain = new Chain($platform, $llm , [$processor], [$processor]);
213216
214217$messages = new MessageBag(
215218 Message::forSystem(<<<PROMPT
@@ -250,11 +253,11 @@ the response back to PHP objects.
250253To achieve this, a specific chain processor needs to be registered:
251254``` php
252255use PhpLlm\LlmChain\Chain;
253- use PhpLlm\LlmChain\Message\Message;
254- use PhpLlm\LlmChain\Message\MessageBag;
255- use PhpLlm\LlmChain\StructuredOutput\ChainProcessor;
256- use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory;
257- use PhpLlm\LlmChain\Tests\StructuredOutput\Data\MathReasoning;
256+ use PhpLlm\LlmChain\Model\ Message\Message;
257+ use PhpLlm\LlmChain\Model\ Message\MessageBag;
258+ use PhpLlm\LlmChain\Chain\ StructuredOutput\ChainProcessor;
259+ use PhpLlm\LlmChain\Chain\ StructuredOutput\ResponseFormatFactory;
260+ use PhpLlm\LlmChain\Tests\Chain\ StructuredOutput\Data\MathReasoning;
258261use Symfony\Component\Serializer\Encoder\JsonEncoder;
259262use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
260263use Symfony\Component\Serializer\Serializer;
@@ -263,7 +266,7 @@ use Symfony\Component\Serializer\Serializer;
263266
264267$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);
265268$processor = new ChainProcessor(new ResponseFormatFactory(), $serializer);
266- $chain = new Chain($llm, [$processor], [$processor]);
269+ $chain = new Chain($platform, $ llm, [$processor], [$processor]);
267270
268271$messages = new MessageBag(
269272 Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'),
@@ -279,8 +282,8 @@ dump($response->getContent()); // returns an instance of `MathReasoning` class
279282Also PHP array structures as ` response_format ` are supported, which also requires the chain processor mentioned above:
280283
281284``` php
282- use PhpLlm\LlmChain\Message\Message;
283- use PhpLlm\LlmChain\Message\MessageBag;
285+ use PhpLlm\LlmChain\Model\ Message\Message;
286+ use PhpLlm\LlmChain\Model\ Message\MessageBag;
284287
285288// Initialize Platform, LLM and Chain with processors and Clock tool
286289
@@ -380,9 +383,9 @@ needs to be used.
380383Some LLMs also support images as input, which LLM Chain supports as ` Content ` type within the ` UserMessage ` :
381384
382385``` php
383- use PhpLlm\LlmChain\Message\Content\Image;
384- use PhpLlm\LlmChain\Message\Message;
385- use PhpLlm\LlmChain\Message\MessageBag;
386+ use PhpLlm\LlmChain\Model\ Message\Content\Image;
387+ use PhpLlm\LlmChain\Model\ Message\Message;
388+ use PhpLlm\LlmChain\Model\ Message\MessageBag;
386389
387390// Initialize Platoform, LLM & Chain
388391
@@ -411,16 +414,15 @@ therefore LLM Chain implements a `EmbeddingsModel` interface with various models
411414The standalone usage results in an ` Vector ` instance:
412415
413416``` php
414- use PhpLlm\LlmChain\OpenAI\Model\Embeddings;
415- use PhpLlm\LlmChain\OpenAI\Model\Embeddings\Version;
417+ use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings;
416418
417419// Initialize Platform
418420
419- $embeddings = new Embeddings($platform, Version::textEmbedding3Small() );
421+ $embeddings = new Embeddings($platform, Embeddings::TEXT_3_SMALL );
420422
421- $vector = $embeddings->create($ textInput);
423+ $vectors = $platform->request($embeddings, $ textInput)->getContent( );
422424
423- dump($vector ->getData()); // Array of float values
425+ dump($vectors[0] ->getData()); // Array of float values
424426```
425427
426428#### Code Examples
@@ -436,9 +438,9 @@ interface. They are provided while instantiating the Chain instance:
436438``` php
437439use PhpLlm\LlmChain\Chain;
438440
439- // Initialize LLM and processors
441+ // Initialize Platform, LLM and processors
440442
441- $chain = new Chain($llm, $inputProcessors, $outputProcessors);
443+ $chain = new Chain($platform, $ llm, $inputProcessors, $outputProcessors);
442444```
443445
444446#### InputProcessor
@@ -449,7 +451,7 @@ able to mutate both on top of the `Input` instance provided.
449451``` php
450452use PhpLlm\LlmChain\Chain\Input;
451453use PhpLlm\LlmChain\Chain\InputProcessor;
452- use PhpLlm\LlmChain\Message\AssistantMessage
454+ use PhpLlm\LlmChain\Model\ Message\AssistantMessage
453455
454456final class MyProcessor implements InputProcessor
455457{
@@ -474,7 +476,7 @@ mutate or replace the given response:
474476``` php
475477use PhpLlm\LlmChain\Chain\Output;
476478use PhpLlm\LlmChain\Chain\OutputProcessor;
477- use PhpLlm\LlmChain\Message\AssistantMessage
479+ use PhpLlm\LlmChain\Model\ Message\AssistantMessage
478480
479481final class MyProcessor implements OutputProcessor
480482{
@@ -499,7 +501,7 @@ use PhpLlm\LlmChain\Chain\ChainAwareProcessor;
499501use PhpLlm\LlmChain\Chain\ChainAwareTrait;
500502use PhpLlm\LlmChain\Chain\Output;
501503use PhpLlm\LlmChain\Chain\OutputProcessor;
502- use PhpLlm\LlmChain\Message\AssistantMessage
504+ use PhpLlm\LlmChain\Model\ Message\AssistantMessage
503505
504506final class MyProcessor implements OutputProcessor, ChainAwareProcessor
505507{
0 commit comments