[PR #12889] [CLOSED] fix: Limit RAG-knowledge results to <= k when using multiple sources (and knowledge bases) #46092

Closed
opened 2026-04-29 20:45:15 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/12889
Author: @almajo
Created: 4/15/2025
Status: Closed

Base: devHead: fix/respect_topk_on_multiple_knowledge_bases


📝 Commits (2)

  • 18fbf82 ~ Limit rag results to <= k
  • 0129e38 ~ Exclude empty documents

📊 Changes

1 file changed (+0 additions, -0 deletions)

View changed files

📝 backend/open_webui/retrieval/utils.py (+150 -105)

📄 Description

Pull Request Checklist

Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.

Before submitting, make sure you've checked the following:

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests to validate the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

I already described the problem in https://github.com/open-webui/open-webui/discussions/12749.

When using multiple knowledge bases (let's say n) with a setting of top-k = k, the llm was provided with n*k chunks instead of k. For a model with 5 knowledge bases and top-k=10, this means 50 chunks which has many disadvantages.

In this PR, we correct this by leveraging the list parameter of collection_names for query_collection and query_hybrid_collection by actually passing multiple instead of only one at a time. Hence, the merge_results at the end of these methods does the job correctly.
However, as the merge_and_sort_results method removed the actual collection heritage, I needed to add some context code where we store the heritage information - which we might need later in the frontend or wherever.

Changed

  • Changed utils class for get_sources_from_files to respect the k parameter

Additional Information

The result of the get_sources_from_files method remains the same: A list of collections, each with some chunks which in total are <= k. When 2 knowledge bases are used (or 1 knowledge base, 1 in-chat file), it's a list of length 2 - as before.

As the results are grouped together by knowledge base, it's good to understand that this means that the order of ranked documents is a good mix. Here is an example:

source_1_distances = [1, 0.5]
source_2_distances = [0.9]
source_3_distances = [0.4]

Hence, when giving the model the chunks for citation the enumeration of distances will be [1, 0.5, 0.9, 0.4] (as sources are given by number from source 1 to n), i.e. it's not an ordered list from highest to lowest. However, this is rather an additional feature of this implementation as it is an advanced approach to shuffle results for better RAG performance.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/12889 **Author:** [@almajo](https://github.com/almajo) **Created:** 4/15/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/respect_topk_on_multiple_knowledge_bases` --- ### 📝 Commits (2) - [`18fbf82`](https://github.com/open-webui/open-webui/commit/18fbf82898c7dde2b70cdbb36c2df82fc71eab54) ~ Limit rag results to <= k - [`0129e38`](https://github.com/open-webui/open-webui/commit/0129e38545d3af969acf94a352303d6f506f2e9e) ~ Exclude empty documents ### 📊 Changes **1 file changed** (+0 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/retrieval/utils.py` (+150 -105) </details> ### 📄 Description # Pull Request Checklist ### Note to first-time contributors: Please open a discussion post in [Discussions](https://github.com/open-webui/open-webui/discussions) and describe your changes before submitting a pull request. **Before submitting, make sure you've checked the following:** - [x] **Target branch:** Please verify that the pull request targets the `dev` branch. - [x] **Description:** Provide a concise description of the changes made in this pull request. - [x] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [ ] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources? - [ ] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [ ] **Testing:** Have you written and run sufficient tests to validate the changes? - [x] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [x] **Prefix:** To clearly categorize this pull request, prefix the pull request title using one of the following: - **BREAKING CHANGE**: Significant changes that may affect compatibility - **build**: Changes that affect the build system or external dependencies - **ci**: Changes to our continuous integration processes or workflows - **chore**: Refactor, cleanup, or other non-functional code changes - **docs**: Documentation update or addition - **feat**: Introduces a new feature or enhancement to the codebase - **fix**: Bug fix or error correction - **i18n**: Internationalization or localization changes - **perf**: Performance improvement - **refactor**: Code restructuring for better maintainability, readability, or scalability - **style**: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.) - **test**: Adding missing tests or correcting existing tests - **WIP**: Work in progress, a temporary label for incomplete or ongoing work # Changelog Entry ### Description I already described the problem in https://github.com/open-webui/open-webui/discussions/12749. When using multiple knowledge bases (let's say **n**) with a setting of top-k = **k**, the llm was provided with `n*k` chunks instead of k. For a model with 5 knowledge bases and top-k=10, this means 50 chunks which has many disadvantages. In this PR, we correct this by leveraging the list parameter of collection_names for `query_collection` and `query_hybrid_collection` by actually passing multiple instead of only one at a time. Hence, the merge_results at the end of these methods does the job correctly. However, as the `merge_and_sort_results` method removed the actual collection heritage, I needed to add some context code where we store the heritage information - which we might need later in the frontend or wherever. ### Changed - Changed utils class for `get_sources_from_files` to respect the `k` parameter ### Additional Information The result of the `get_sources_from_files` method remains the same: A list of collections, each with some chunks which in total are <= k. When 2 knowledge bases are used (or 1 knowledge base, 1 in-chat file), it's a list of length 2 - as before. As the results are grouped together by knowledge base, it's good to understand that this means that the order of ranked documents is a good mix. Here is an example: ```python source_1_distances = [1, 0.5] source_2_distances = [0.9] source_3_distances = [0.4] ``` Hence, when giving the model the chunks for citation the enumeration of distances will be [1, 0.5, 0.9, 0.4] (as sources are given by number from source 1 to n), i.e. it's not an ordered list from highest to lowest. However, this is rather an additional feature of this implementation as it is an advanced approach to shuffle results for better RAG performance. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-29 20:45:15 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#46092