Skip to content

feat(rag): rank RAG results by reference material priority - #787

Merged
haiphucnguyen merged 2 commits into
askimo-ai:mainfrom
rcpeken:feat/source-priority
Oct 30, 2026
Merged

haiphucnguyen merged 2 commits into
askimo-ai:mainfrom
rcpeken:feat/source-priority

Conversation

@rcpeken

@rcpeken rcpeken commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Closes #345

Summary

Adds a priority to each reference material (Low / Normal / High / Always prioritize) in Projects and Resource Collections. Hybrid RAG ranking multiplies each result's RRF score by the priority of the source it came from, as discussed in #345.

Related Issue(s)

Closes #345

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation
  • ♻️ Refactor (no functional change)
  • 🧪 Tests
  • 🔧 Chore / tooling / CI

What Changed & Why

  • Model. KnowledgeSourceConfig.priority (a plain Double, default 1.0) plus KnowledgeSourcePriority with the weights we agreed on: LOW=0.5, NORMAL=1.0, HIGH=2.0, ALWAYS_PRIORITIZE=100.0. Sources are stored as JSON with defaults and ignoreUnknownKeys, so there's no migration and existing sources decode as Normal.
  • Ranking. HybridContentRetriever sorts by rrfScore * priority, as you suggested. The same priority / (k + rank) weighting is also applied when only one retriever returns results; otherwise priority would be silently ignored whenever, for example, keyword search comes back empty. With every source at Normal, the ranking is identical to today.
  • Matching results to sources. The new KnowledgeSourcePriorityResolver maps each chunk back to its reference material: longest folder prefix on a path boundary, exact file path, exact URL. Live web results and unmatched chunks stay at 1.0. No re-index is needed.
  • Priority changes apply to open chats. The retriever is cached per session (up to 30 minutes idle), so the resolver reads the container's current sources on each query (one SQLite read) instead of capturing them when the retriever is built.
  • UI. A small priority selector on each source row in both the Project and Resource Collection views (shared knowledgeSourcePrioritySelector). It persists the same way the watch toggle does. Strings added to all 10 locales.
  • Related fix in RagIndexer. handleRemoveIndexEvent looked up the live coordinator by full config equality. After a priority (or watch) change, the stored config no longer equals the coordinator's copy, so deleting that source fell back to persisted cleanup and left its folder watcher running. It now matches by resourceIdentifier, like the other lookups in that file.

How to Test

./gradlew :shared-jvm:test --tests "io.askimo.core.rag.HybridContentRetrieverTest" \
  --tests "io.askimo.core.rag.KnowledgeSourcePriorityResolverTest" \
  --tests "io.askimo.core.chat.domain.KnowledgeSourcePriorityTest"

Manually: in a collection with two folders, set one to High and ask something both folders can answer. Results from the High folder rank first. Set it to Always prioritize to see it dominate.


Checklist

- [x] I have read the CONTRIBUTING (../CONTRIBUTING.md) guide
- [x] My branch is up to date with main
- [x] I ran ./gradlew spotlessApply and ./gradlew detekt locally
- [ ] I ran ./gradlew test and all tests pass
- [x] I added/updated tests for my changes (if applicable)
- [ ] I added/updated documentation (if applicable)
- [x] My commits are signed off (DCO) — git commit -s
- [x] This PR is focused on a single feature/fix

Additional Notes

- Tests run. All of :shared-jvm:test passes, including 21 new tests, as does ChatSessionServiceIT. I didn't tick the full ./gradlew test box because some cli tests need Docker, which I don't have locally.
- Weight strength. With k=60, RRF scores are close together, so High (2.0) is fairly strong: a High result can outrank Normal results that sit much higher in a single list. A result found by both retrievers still beats a High result found by only one, and HybridContentRetrieverTest documents both cases. Happy to tune the weights.
- Out of scope. Web search per collection (follow-up ticket), server-managed collections (retrieval is remote), and the grep path used for SEARCH intent.
- Translations. Non-English translations would benefit from a native speaker's review.

Copilot AI balanced review requested due to automatic review settings October 29, 2026 10:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@rcpeken

rcpeken commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

@haiphucnguyen this is ready for review, and CI is green. A few things worth a look:

  • The weights are the ones from Enhance RRF ranking in HybridContentRetriever with source-aware scoring #345 (Low 0.5 / Normal 1.0 / High 2.0 / Always prioritize 100). They're defined in one enum, KnowledgeSourcePriority, so tuning them is a one-line change.
  • With every source at Normal, the ranking is unchanged, and existing sources load as Normal, so there's no migration or re-index.
  • There's one small related fix in RagIndexer: the removal lookup now matches by resourceIdentifier. The reason is in the description.

Happy to adjust anything.

@haiphucnguyen

Copy link
Copy Markdown
Collaborator

Thank you @rcpeken for this great PR. I pulled the PR and test it first. The output looks good with the high quality response from the AI RAG. I am using model qwen2.5-coder:14b

Screenshot 2026-10-29 at 10 36 50 AM

I will look into the code and provide the feedback when I have time today.

In the mean time, since you are interested to RAG, we also use the metadata to instruct the AI response in the format we want, see https://github.com/askimo-ai/askimo/blob/main/shared-jvm/src/main/kotlin/io/askimo/core/rag/MetadataAwareContentInjector.kt. If you are interested in RAG, I can create a follow up ticket relates to Web Search, and another one how to control the metadata content instruction that we allow users can decide the format they want. If you're interested and have time, please let me know and I leave them for you :)

@haiphucnguyen haiphucnguyen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rcpeken ! It is a great PR, I left some minor comment about the enum.

import java.nio.file.Path
import kotlin.test.assertEquals

class HybridContentRetrieverTest {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love these tests :).

@rcpeken

rcpeken commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for testing it with qwen2.5-coder @haiphucnguyen, great to see it working end to end! I've pushed the enum change from your review.

And yes, I'm interested in both follow-ups: web search configuration per collection, and letting users control the metadata instruction format in MetadataAwareContentInjector. Feel free to assign them to me. I'll start with web search once this PR is settled, then the metadata one.

@haiphucnguyen haiphucnguyen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @rcpeken ! LGTM

@haiphucnguyen
haiphucnguyen merged commit fded9b5 into askimo-ai:main Oct 30, 2026
12 checks passed
@rcpeken

rcpeken commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Hİ @haiphucnguyen. I've already looked into the web search side. Today the web search chip only shows in project sessions, so chats using a resource collection can't turn it on at all, and the toggle resets when the app restarts. I have a short proposal ready (a per-collection "Use web search" default that the session chip can override, as you suggested). Would you like me to open the issue with it, or would you prefer to create it yourself? Either way, I'd be glad to work on it.

@haiphucnguyen

Copy link
Copy Markdown
Collaborator

Hİ @haiphucnguyen. I've already looked into the web search side. Today the web search chip only shows in project sessions, so chats using a resource collection can't turn it on at all, and the toggle resets when the app restarts. I have a short proposal ready (a per-collection "Use web search" default that the session chip can override, as you suggested). Would you like me to open the issue with it, or would you prefer to create it yourself? Either way, I'd be glad to work on it.

Thank you @rcpeken!. Feel free to create a ticket by yourself. Your proposal is good. The minor logic is the search is the toogle enable option (let user option they do want the privacy with the AI response). We should warn user must enable search option before using RAG web search

@rcpeken

rcpeken commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I've opened the ticket with your privacy point included: the global web search setting stays the master switch, with a warning when it's off. #810

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance RRF ranking in HybridContentRetriever with source-aware scoring

3 participants