Problem/Motivation
The ai_dropsolid module extends Dropsolid’s AI provider but lacks accurate token counting for Dropsolid’s custom embedding model. That model uses a custom tokenizer (SentencePiece), and there is no PHP-native support for this tokenization.
Without a compatible counter, usage tracking and token-based limits can be inconsistent with how the model actually processes text. We need a way to count tokens using the same tokenizer/model setup as the Dropsolid provider.
Proposed resolution
Add token-counting support in ai_dropsolid that leverages the same tokenizer and configuration as the Dropsolid embedding provider. The approach should remain implementation-agnostic but ensure counts align with the provider’s custom tokenizer (SentencePiece) to deliver accurate reporting and parity.
Remaining tasks
- Define an approach to integrate token counting with the custom tokenizer.
- Implement the token-counting mechanism within the module.
- Add tests to validate accuracy against the provider’s behavior.
- Document configuration and usage for maintainers and site builders.
Issue fork ai_dropsolid-3553576
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
nikro commentedAfter several hours of research and not finding any PHP library, I concluded there are two possible approaches:
/utils/token_counterendpoint specifically for this purpose.This approach will be slightly slower since it operates over HTTPS, but it will be accurate.
We will need to implement both options to provide customers with flexibility.
Even more than that - we need to create a custom Tokenizer that is aware of the custom embeddings we use, particularly in the context of the Search API.
This tokenizer should be injected into the system - potentially via a decorator - so that we can override or “hijack” the default tokenizer and replace it with our own implementation when needed.
This approach will ensure that tokenization is consistent with the embedding model’s expectations and that downstream processes relying on accurate token boundaries continue to function correctly.
Comment #3
nikro commentedI'll take this over - in a separate branch.
Comment #5
nikro commentedTesting Instructions for TokenAware Text Chunking Implementation
Code Review - Overview
This MR introduces a token-aware text chunking system with custom tokenizer support for Dropsolid AI integrations. The implementation includes:
src/Service/TokenAwareTextChunker.php- Core chunking service with density-based probingsrc/Service/TokenAwareTextChunkerInterface.php- Service interfacesrc/Plugin/EmbeddingStrategy/TokenAwareEmbeddingStrategy.php- Embedding strategy pluginsrc/Tokenizer/DropsolidXlmRobertaTokenizer.php- Custom XLM-RoBERTa tokenizersrc/Tokenizer/DropsolidTokenizerDecorator.php- Tokenizer decorator for AI module integrationsrc/Form/TokenizerSettingsForm.php- Admin configuration UIPrerequisites
Required Access: You must have access to Dropsolid's infrastructure services:
Testing Steps
Step 1: Configure the Tokenizer
/admin/config/ai/ai-dropsolid/tokenizerStep 2: Create Search API Server
/admin/config/search/search-api/add-servereu-e5-large-embeddings-selfhostedDropsolid AI XLM-Roberta-basefrom dropdownToken-Aware Embedding Strategy (Separator-Based Chunking)480or500tokens70,80, or100tokens (your choice)25%or30%Step 3: Create Search API Index
/admin/config/search/search-api/add-indexStep 4: Test Chunking with Content Preview
/admin/config/search/search-api/index/[your_index_name]/fieldsStep 5: Go wild and index stuff
Process:
Technical Notes
\n\n→\n→.→\t→in order of preferenceTroubleshooting
If tokenization test fails:
/admin/config/ai/provider/litellmComment #6
nikro commentedComment #7
abhisekmazumdarI followed the steps and was able start Chunking the content.
I took a quick glance at the code. Everything appears good, except for a single inline comment.
Comment #10
nikro commentedThanks for the review!
Added 1 change and merged it in!