Input Refinement for Enhanced Search/Retrieval Quality
Summary
Enhance the output quality of the semantic search/retrieval system by introducing a robust input normalization step. The goal is to ensure that both search queries and stored data are processed into a similar format before embedding and retrieval, thereby improving the accuracy and relevance of semantic search results.
Problem Statement
Currently, the semantic search/retrieval system may suffer from suboptimal results due to inconsistencies between the format of user input queries and the format of stored data. Differences in casing, punctuation, whitespace, or other textual features can reduce the effectiveness of vector-based or semantic search methods, as embeddings may not accurately reflect true similarity. This leads to lower retrieval quality and a less effective search experience.
Technical Requirements
- Implement a shared normalization utility function (e.g.,
normalizeTextForRetrieval) to standardize text input. - The normalization process should, at a minimum, include:
- Lowercasing all text
- Removing or standardizing punctuation
- Normalizing whitespace (e.g., trimming, collapsing multiple spaces)
- (Optional/Configurable) Removing stopwords, stemming/lemmatization, or language-specific normalization
- Apply this normalization utility to:
- All new data before embedding and storage
- All incoming search queries before embedding and retrieval
- Ensure the normalization logic is easily configurable for future extensibility.
- Refactor relevant storage and retrieval code paths to use the normalization utility.
- Maintain backward compatibility with existing stored data (consider migration or dual-path normalization if needed).
Implementation Guidelines
-
Utility Creation
- Create a new utility function in
src/core/brain/embedding/utils.ts(or a new file if more appropriate) namednormalizeTextForRetrieval. - The function should accept a string and return a normalized string according to the requirements above.
- Add configuration options (e.g., via a config file or environment variable) to enable/disable specific normalization steps.
- Create a new utility function in
-
Integration
- Storage Pipeline:
- Locate where new data is processed before embedding and storage (likely in
src/core/brain/embedding/manager.tsor related ingestion code). - Apply the normalization utility to all text before generating embeddings and storing vectors.
- Locate where new data is processed before embedding and storage (likely in
- Retrieval Pipeline:
- Locate where search queries are processed before embedding and retrieval (likely in
src/core/brain/embedding/manager.tsorsrc/core/brain/vector_storage/manager.ts). - Apply the normalization utility to all incoming queries before generating embeddings and performing search.
- Locate where search queries are processed before embedding and retrieval (likely in
- Storage Pipeline:
-
Configuration
- Allow normalization rules to be adjusted via configuration (e.g., JSON config, environment variables).
-
Backward Compatibility
- If existing stored data is not normalized, consider:
- A migration script to re-embed and store normalized data, OR
- Applying normalization only to new data and queries, with a note on potential retrieval inconsistencies for legacy data.
- If existing stored data is not normalized, consider:
-
Documentation
- Document the normalization process and configuration options in the codebase (e.g., in README or code comments).
Testing Requirements
- Unit Tests
- For the normalization utility, covering all normalization steps and edge cases.
- Integration Tests
- For the storage pipeline: Ensure that data is normalized before embedding and storage.
- For the retrieval pipeline: Ensure that queries are normalized before embedding and search.
- For the end-to-end search: Demonstrate improved retrieval quality with normalized input.
- Regression Tests
- Ensure that existing functionality is not broken and that backward compatibility is maintained.
Definition of Done
- A shared normalization utility is implemented and tested.
- Both storage and retrieval pipelines use the normalization utility.
- Configuration options for normalization are available and documented.
- All relevant code is refactored to use the new normalization step.
- Unit and integration tests are written and pass successfully.
- Documentation is updated to reflect the new normalization process.
- Retrieval quality is demonstrably improved in test cases.
Code Reference
Key files to create/modify:
src/core/brain/embedding/utils.ts(addnormalizeTextForRetrieval)src/core/brain/embedding/manager.ts(apply normalization in storage and retrieval)src/core/brain/vector_storage/manager.ts(if retrieval logic is here, apply normalization)- (Optional) Configuration file or section for normalization rules
Example references for similar logic:
- See how text is currently processed before embedding in
src/core/brain/embedding/manager.ts - See how queries are handled in
src/core/brain/vector_storage/manager.ts - Use existing utility patterns in
src/core/brain/embedding/utils.ts
Additional Notes
- If the codebase supports multiple languages or data types, consider extensibility for language-specific normalization.
- If a migration is needed for legacy data, scope and plan this as a follow-up task.
- Monitor retrieval performance and quality after deployment to ensure the normalization has the intended effect.
If you need further clarification or a more detailed breakdown of code locations, please request a deeper code reference analysis.
Close this by #186