|
| 1 | +Symfony AI - Store Component |
| 2 | +============================ |
| 3 | + |
| 4 | +The Platform component provides an abstraction for interacting with different models, their providers and contracts. |
| 5 | + |
| 6 | +Installation |
| 7 | +------------ |
| 8 | + |
| 9 | +Install the component using Composer: |
| 10 | + |
| 11 | +.. code-block:: terminal |
| 12 | +
|
| 13 | + $ composer require symfony/ai-platform |
| 14 | +
|
| 15 | +Purpose |
| 16 | +------- |
| 17 | + |
| 18 | +The Platform component provides a unified interface for working with various AI models, hosted and run by different |
| 19 | +providers. It allows developers to easily switch between different AI models and providers without changing their |
| 20 | +application code. This is particularly useful for applications that require flexibility in choosing AI models based on |
| 21 | +specific use cases or performance requirements. |
| 22 | + |
| 23 | +Usage |
| 24 | +----- |
| 25 | + |
| 26 | +The instantiation of the ``Symfony\AI\Platform\Platform`` class is usually delegated to a provider-specific factory, |
| 27 | +with a provider being OpenAI, Azure, Google, Replicate, and others. |
| 28 | + |
| 29 | +For example, to use the OpenAI provider, you would typically do something like this: |
| 30 | + |
| 31 | +.. code-block: php |
| 32 | +
|
| 33 | + use Symfony\AI\Platform\Bridge\OpenAI\Embeddings; |
| 34 | + use Symfony\AI\Platform\Bridge\OpenAI\GPT; |
| 35 | + use Symfony\AI\Platform\Bridge\OpenAI\PlatformFactory; |
| 36 | +
|
| 37 | + // Platform |
| 38 | + $platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); |
| 39 | +
|
| 40 | + // Embeddings Model |
| 41 | + $embeddings = new Embeddings(); |
| 42 | +
|
| 43 | + // Language Model in version gpt-4o-mini |
| 44 | + $gpt = new GPT(GPT::GPT_4O_MINI); |
| 45 | +
|
| 46 | +And with a ``Symfony\AI\Platform\PlatformInterface`` instance, and a ``Symfony\AI\Platform\Model`` instance, you can now |
| 47 | +use the platform to interact with the AI model: |
| 48 | + |
| 49 | +.. code-block: php |
| 50 | +
|
| 51 | + // Generate a vector embedding for a text, returns a Symfony\AI\Platform\Response\VectorResponse |
| 52 | + $response = $platform->request($embeddings, 'What is the capital of France?'); |
| 53 | +
|
| 54 | + // Generate a text completion with GPT, returns a Symfony\AI\Platform\Response\TextResponse |
| 55 | + $embeddingsResult = $platform->request($gpt, new MessageBag(Message::ofUser('What is the capital of France?')); |
| 56 | +
|
| 57 | +Depending on the model and its capabilities, different types of inputs and outputs are supported, which results in a |
| 58 | +very flexible and powerful interface for working with AI models. |
| 59 | + |
| 60 | +Models |
| 61 | +------ |
| 62 | + |
| 63 | +The component provides a model base class ``Symfony\AI\Platform\Model`` which is a combination of a model name, a set of |
| 64 | +capabilities, and additional options. Usually, bridges to specific providers extend this base class to provide a quick |
| 65 | +start for vendor-specific models and their capabilities, see ``Symfony\AI\Platform\Bridge\Anthropic\Claude`` or |
| 66 | +``Symfony\AI\Platform\Bridge\OpenAI\GPT``. |
| 67 | + |
| 68 | +**Capabilities** are a list of strings defined by ``Symfony\AI\Platform\Capability``, which can be used to check if a model |
| 69 | +supports a specific feature, like ``Capability::INPUT_AUDIO`` or ``Capability::OUTPUT_IMAGE``. |
| 70 | + |
| 71 | +**Options** are additional parameters that can be passed to the model, like ``temperature`` or ``max_tokens``, and are |
| 72 | +usually defined by the specific models and their documentation. |
| 73 | + |
| 74 | +Language Models and Messages |
| 75 | +---------------------------- |
| 76 | + |
| 77 | +One central feature of the Platform component is the support for language models and easing the interaction with them. |
| 78 | +This is supported by providing an extensive set of data classes around the concept of messages and their content. |
| 79 | + |
| 80 | +Messages can be of different types, most importantly ``UserMessage``, ``SystemMessage``, or ``AssistantMessage``, can |
| 81 | +have different content types, like ``Text``, ``Image`` or ``Audio``, and can be grouped into a ``MessageBag``: |
| 82 | + |
| 83 | +.. code-block: php |
| 84 | +
|
| 85 | + use Symfony\AI\Platform\Message\Content\Image; |
| 86 | + use Symfony\AI\Platform\Message\Message; |
| 87 | + use Symfony\AI\Platform\Message\MessageBag; |
| 88 | +
|
| 89 | + // Create a message bag with a user message |
| 90 | + $messageBag = new MessageBag( |
| 91 | + Message::ofSystem('You are a helpful assistant.') |
| 92 | + Message::ofUser('Please describe this picture?', Image::fromFile('/path/to/image.jpg')), |
| 93 | + ); |
| 94 | +
|
| 95 | +Supported Models & Platforms |
| 96 | +---------------------------- |
| 97 | + |
| 98 | +* **Language Models** |
| 99 | + * `OpenAI's GPT`_ with `OpenAI`_ and `Azure`_ as Platform |
| 100 | + * `Anthropic's Claude`_ with `Anthropic`_ and `AWS Bedrock`_ as Platform |
| 101 | + * `Meta's Llama`_ with `Azure`_, `Ollama`_, `Replicate`_ and `AWS Bedrock`_ as Platform |
| 102 | + * `Google's Gemini`_ with `Google`_ and `OpenRouter`_ as Platform |
| 103 | + * `DeepSeek's R1`_ with `OpenRouter`_ as Platform |
| 104 | + * `Amazon's Nova`_ with `AWS Bedrock`_ as Platform |
| 105 | + * `Mistral's Mistral`_ with `Mistral`_ as Platform |
| 106 | +* **Embeddings Models** |
| 107 | + * `OpenAI's Text Embeddings`_ with `OpenAI`_ and `Azure`_ as Platform |
| 108 | + * `Voyage's Embeddings`_ with `Voyage`_ as Platform |
| 109 | + * `Mistral Embed`_ with `Mistral`_ as Platform |
| 110 | +* **Other Models** |
| 111 | + * `OpenAI's Dall·E`_ with `OpenAI`_ as Platform |
| 112 | + * `OpenAI's Whisper`_ with `OpenAI`_ and `Azure`_ as Platform |
| 113 | + * All models provided by `HuggingFace`_ can be listed with a command in the examples folder, |
| 114 | + and also filtered, e.g. ``php examples/huggingface/_model-listing.php --provider=hf-inference --task=object-detection`` |
| 115 | + |
| 116 | +See `GitHub`_ for planned support of other models and platforms. |
| 117 | + |
| 118 | +.. _`OpenAI's GPT`: https://platform.openai.com/docs/models/overview |
| 119 | +.. _`OpenAI`: https://platform.openai.com/docs/overview |
| 120 | +.. _`Azure`: https://learn.microsoft.com/azure/ai-services/openai/concepts/models |
| 121 | +.. _`Anthropic's Claude`: https://www.anthropic.com/claude |
| 122 | +.. _`Anthropic`: https://www.anthropic.com/ |
| 123 | +.. _`AWS Bedrock`: https://aws.amazon.com/bedrock/ |
| 124 | +.. _`Meta's Llama`: https://www.llama.com/ |
| 125 | +.. _`Ollama`: https://ollama.com/ |
| 126 | +.. _`Replicate`: https://replicate.com/ |
| 127 | +.. _`Google's Gemini`: https://gemini.google.com/ |
| 128 | +.. _`Google`: https://ai.google.dev/ |
| 129 | +.. _`OpenRouter`: https://www.openrouter.com/ |
| 130 | +.. _`DeepSeek's R1`: https://www.deepseek.com/ |
| 131 | +.. _`Amazon's Nova`: https://nova.amazon.com |
| 132 | +.. _`Mistral's Mistral`: https://www.mistral.ai/ |
| 133 | +.. _`Mistral`: https://www.mistral.ai/ |
| 134 | +.. _`OpenAI's Text Embeddings`: https://platform.openai.com/docs/guides/embeddings/embedding-models |
| 135 | +.. _`Voyage's Embeddings`: https://docs.voyageai.com/docs/embeddings |
| 136 | +.. _`Voyage`: https://www.voyageai.com/ |
| 137 | +.. _`Mistral Embed`: https://www.mistral.ai/ |
| 138 | +.. _`OpenAI's Dall·E`: https://platform.openai.com/docs/guides/image-generation |
| 139 | +.. _`OpenAI's Whisper`: https://platform.openai.com/docs/guides/speech-to-text |
| 140 | +.. _`HuggingFace`: https://huggingface.co/ |
| 141 | +.. _`GitHub`: https://github.com/symfony/ai/issues/16 |
0 commit comments