Leverage Generative AI in your PHP E-Commerce website with Qdrant and LLPhant
Maxime Thoonsen5 min read
There are many use cases of Generative AI: e-commerce search, customer support and sales optimization, law case search…
To help e-commerce and other use cases written in PHP, I’m happy..I’m happy to announce the addition of Qdrant as a vectorStore in LLPhant to open up the possibility of making production grade applications with LLPhant easier. Let me introduce the two tools.
Introducing Qdrant
Qdrant is an open-source cutting-edge vector database and vector similarity search engine. Think of it as a specialized database designed to handle high-dimensional vectors (including embeddings), which are the output of modern machine learning models. Deployed as an API service, Qdrant offers the ability to perform advanced search for the nearest high-dimensional vectors.
Meet LLPhant
LLPhant is the PHP framework to build Generative AI applications. LLPhant facilitates data manipulation to create embeddings and advanced information retrieval flows. With LLPhant, creating AI chatbots, setting up question-answering flows, or even scripting AI-powered PHP functions becomes a breeze. What makes LLPhant even more appealing is its compatibility. Whether you’re a fan of Laravel, Symfony, or any other PHP framework, LLPhant has got you covered. If you know LangChain you can see it as LangChain in PHP.
Find similar products on your E-Commerce website using Qdrant and LLphant
You can now use Qdrant as a VectorStore in LLPhant. This integration means that you can store vectors in Qdrant and perform similarity searches right from your PHP applications. Here’s a detailed example.
We first need to create the embeddings before storing them. This diagram shows the whole process.
- First we need get the products description from some files using a DataReader
- Then create chunk of texts of the right size that will fit in the embedding models
- and finally we can send the text to OpenAI and create the embeddings from the chunks
Let’s say you want to store the description of your E-Commerce products. It will look like this:
use LLPhant\Embeddings\DataReader\FileDataReader;
use LLPhant\Embeddings\Document;
use LLPhant\Embeddings\DocumentSplitter\DocumentSplitter;
use LLPhant\Embeddings\EmbeddingFormatter\EmbeddingFormatter;
use LLPhant\Embeddings\EmbeddingGenerator\OpenAIEmbeddingGenerator;
use LLPhant\Embeddings\VectorStores\Qdrant\QdrantVectorStore;
use Qdrant\Config;
use Qdrant\Models\Filter\Condition\MatchString;
$directoryPath = __DIR__.'/products';
$reader = new FileDataReader($directoryPath, Document::class);
$documents = $reader->getDocuments();
$splittedDocuments = DocumentSplitter::splitDocuments($documents, 200);
$formattedDocuments = EmbeddingFormatter::formatEmbeddings($splittedDocuments);
$embeddingGenerator = new OpenAIEmbeddingGenerator();
$embeddedDocuments = $embeddingGenerator->embedDocuments($formattedDocuments);
Now we can store them in Qdrant using the dedicated class in LLPhant:
$host = getenv('QDRANT_HOST');
$apiKey = getenv('QDRANT_API_KEY');
$config = new Config($host);
$config->setApiKey($apiKey);
$collectionName = 'mycollection';
$vectorStore = new QdrantVectorStore($config, $collectionName);
//If the collection doesn't exist yet, you need to create it
$vectorStore->createCollection($collectionName);
// This save all your embeddings on Qdrant with the right parameters
$vectorStore->addDocuments($embeddedDocuments);
Once your vectorStore is filled with your data you can start using it. If you have an E-Commerce website, you can look for similar products.
$embedding = $embeddingGenerator->embedText('Black dress casual');
// You can add any filter available in Qdrant like an exact match on one property
$condition = new MatchString('color', 'black');
$filter['must'] = [$condition];
/** @var Document[] $result */
$similarProducts = $vectorStore->similaritySearch($embedding, 4, $filter);
// You can then suggest the $similarProducts to your users
Create an interactive Q&A with your customers
Another use case is to provide to your users an augmented Q&A bot. Once you have created your vectorStore with Qdrant, you can do it in few lines.
I recommend you to use VercelAI frontend components to create the AI bot if you want to start quickly. They have a complete set of components to create great UI/UX for Generative AI applications.
To understand how it works, there are basically 3 step that happens under the hood:
This is the code that you can use in your controller to create an API endpoint for your bot:
$vectorStore = new QdrantVectorStore($config, $collectionName);
$qa = new QuestionAnswering(respo
$vectorStore,
new OpenAIEmbeddingGenerator(),
new OpenAIChat()
);
/** @var Message[] $messages */
$bodyContent = $request->getContent(); // Messages from VercelAI
$data = json_decode($bodyContent, true, 512, JSON_THROW_ON_ERROR);
// We need to format the messages from the front in a way LLPhant can handle
$messages = [];
foreach ($data->messages as $value) {
$message = new Message();
$message->content = $value->content;
$message->role = ChatRole::from($value->role);
$messages[] = $message;
}
return $qa->answerQuestionFromChat($messages);
If you want, you can customize the way the bot will answer by changing the System Message that OpenAI uses to know how it should behave.
$qa->systemMessageTemplate =
"You are a Q&A system from a E-Commerce website.
Always answer with a helpful tone.
Use the following pieces of context to answer the question of the user.
If you don't know the answer, just say that you don't know,
don't try to make up an answer.\n\n{context}.";
You only need to have “{context}” in your string so that LLPhant can inject the context.
Conclusion: Qdrant will help you build production grade Generative AI application with LLPhant
As you can see with few lines of code we can create powerful features. You can do more with LLphant and Qdrant like leveraging the power of OpenAI functions to call your own PHP functions. This can be used to create a smarter customer support that can help your customer right away.
So, if you’re looking to elevate your PHP applications with the power of Generative AI and vector similarity, it’s time to explore the combined capabilities of Qdrant and LLPhant. Happy coding!