Download the PHP package php-llm/llm-chain without Composer
On this page you can find all versions of the php package php-llm/llm-chain. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download php-llm/llm-chain
More information about php-llm/llm-chain
Files in php-llm/llm-chain
Package llm-chain
Short Description A slim PHP component with tooling around LLMs.
License MIT
Informations about the package llm-chain
LLM Chain
PHP library for building LLM-based features and applications.
This library is not a stable yet, but still rather experimental. Feel free to try it out, give feedback, ask questions, contribute or share your use cases. Abstractions, concepts and interfaces are not final and potentially subject of change.
Requirements
- PHP 8.2 or higher
Installation
The recommended way to install LLM Chain is through Composer:
When using Symfony Framework, check out the integration bundle php-llm/llm-chain-bundle.
Examples
See examples folder to run example implementations using this library.
Depending on the example you need to export different environment variables
for API keys or deployment configurations or create a .env.local
based on .env
file.
To run all examples, use make run-all-examples
or php example
.
Basic Concepts & Usage
Models & Platforms
LLM Chain categorizes two main types of models: Language Models and Embeddings Models.
Language Models, like GPT, Claude and Llama, as essential centerpiece of LLM applications and Embeddings Models as supporting models to provide vector representations of text.
Those models are provided by different platforms, like OpenAI, Azure, Replicate, and others.
Example Instantiation
Supported Models & Platforms
- Language Models
- OpenAI's GPT with OpenAI and Azure as Platform
- Anthropic's Claude with Anthropic as Platform
- Embeddings Models
- OpenAI's Text Embeddings with OpenAI and Azure as Platform
- Voyage's Embeddings with Voyage as Platform
See issue #28 for planned support of other models and platforms.
Chain & Messages
The core feature of LLM Chain is to interact with language models via messages. This interaction is done by sending a MessageBag to a Chain, which takes care of LLM invokation and response handling.
Messages can be of different types, most importantly UserMessage
, SystemMessage
, or AssistantMessage
, and can also
have different content types, like Text
or Image
.
Example Chain call with messages
The MessageInterface
and Content
interface help to customize this process if needed, e.g. additional state handling.
Code Examples
- Anthropic's Claude: chat-claude-anthropic.php
- OpenAI's GPT with Azure: chat-gpt-azure.php
- OpenAI's GPT: chat-gpt-openai.php
- OpenAI's o1: chat-o1-openai.php
Tools
To integrate LLMs with your application, LLM Chain supports tool calling out of the box. Tools are services that can be called by the LLM to provide additional features or process data.
Tool calling can be enabled by registering the processors in the chain:
Custom tools can basically be any class, but must configure by the #[AsTool]
attribute.
Code Examples (with built-in tools)
- Clock Tool: toolbox-clock.php
- SerpAPI Tool: toolbox-serpapi.php
- Weather Tool: toolbox-weather.php
- Wikipedia Tool: toolbox-wikipedia.php
- YouTube Transcriber Tool: toolbox-youtube.php (with streaming)
Document Embedding, Vector Stores & Similarity Search (RAG)
LLM Chain supports document embedding and similarity search using vector stores like ChromaDB, Azure AI Search, MongoDB Atlas Search, or Pinecone.
For populating a vector store, LLM Chain provides the service DocumentEmbedder
, which requires an instance of an
EmbeddingsModel
and one of StoreInterface
, and works with a collection of Document
objects as input:
The collection of Document
instances is usually created by text input of your domain entities:
[!NOTE] Not all data needs to be stored in the vector store, but you could also hydrate the original data entry based on the ID or metadata after retrieval from the store.*
In the end the chain is used in combination with a retrieval tool on top of the vector store, e.g. the built-in
SimilaritySearch
tool provided by the library:
Code Examples
- MongoDB Store: store-mongodb-similarity-search.php
- Pinecone Store: store-pinecone-similarity-search.php
Supported Stores
- ChromaDB (requires
codewithkyrian/chromadb-php
as additional dependency) - Azure AI Search
- MongoDB Atlas Search (requires
mongodb/mongodb
as additional dependency) - Pinecone (requires
probots-io/pinecone-php
as additional dependency)
See issue #28 for planned support of other models and platforms.
Advanced Usage & Features
Structured Output
A typical use-case of LLMs is to classify and extract data from unstructured sources, which is supported by some models by features like Structured Output or providing a Response Format.
PHP Classes as Output
LLM Chain support that use-case by abstracting the hustle of defining and providing schemas to the LLM and converting the response back to PHP objects.
To achieve this, a specific chain processor needs to be registered:
Array Structures as Output
Also PHP array structures as response_format
are supported, which also requires the chain processor mentioned above:
Code Examples
- Structured Output (PHP class): structured-output-math.php
- Structured Output (array): structured-output-clock.php
Tool Parameters
LLM Chain generates a JSON Schema representation for all tools in the ToolBox
based on the #[AsTool]
attribute and
method arguments and doc block. Additionally, JSON Schema support validation rules, which are partially support by
LLMs like GPT.
To leverage this, configure the #[ToolParameter]
attribute on the method arguments of your tool:
[!NOTE] Please be aware, that this is only converted in a JSON Schema for the LLM to respect, but not validated by LLM Chain.
Response Streaming
Since LLMs usually generate a response word by word, most of them also support streaming the response using Server Side Events. LLM Chain supports that by abstracting the conversion and returning a Generator as content of the response.
In a terminal application this generator can be used directly, but with a web app an additional layer like Mercure needs to be used.
Code Examples
- Streaming Claude: stream-claude-anthropic.php
- Streaming GPT: stream-gpt-openai.php
Image Processing
Some LLMs also support images as input, which LLM Chain supports as Content
type within the UserMessage
:
Code Examples
- Image Description: image-describer-binary.php (with binary file)
- Image Description: image-describer-url.php (with URL)
Embeddings
Creating embeddings of word, sentences or paragraphs is a typical use case around the interaction with LLMs and
therefore LLM Chain implements a EmbeddingsModel
interface with various models, see above.
The standalone usage results in an Vector
instance:
Code Examples
- OpenAI's Emebddings: embeddings-openai.php
- Voyage's Embeddings: embeddings-voyage.php
Input & Output Processing
The behavior of the Chain is extendable with services that implement InputProcessor
and/or OutputProcessor
interface. They are provided while instantiating the Chain instance:
InputProcessor
InputProcessor
instances are called in the chain before handing over the MessageBag
and the $options
array to the LLM and are
able to mutate both on top of the Input
instance provided.
OutputProcessor
OutputProcessor
instances are called after the LLM provided a response and can - on top of options and messages -
mutate or replace the given response:
Chain Awareness
Both, Input
and Output
instances, provide access to the LLM used by the Chain, but the chain itself is only
provided, in case the processor implemented the ChainAwareProcessor
interface, which can be combined with using the
ChainAwareTrait
:
Contributions
Contributions are always welcome, so feel free to join the development of this library.
Current Contributors
All versions of llm-chain with dependencies
phpdocumentor/reflection-docblock Version ^5.4
psr/cache Version ^3.0
psr/log Version ^3.0
symfony/clock Version ^6.4 || ^7.1
symfony/http-client Version ^6.4 || ^7.1
symfony/property-access Version ^6.4 || ^7.1
symfony/property-info Version ^6.4 || ^7.1
symfony/serializer Version ^6.4 || ^7.1
symfony/type-info Version ^6.4 || ^7.1
symfony/uid Version ^6.4 || ^7.1
webmozart/assert Version ^1.11