ChunkFetcher object that can apply limits including max_per_interview #3231
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We fetch chunks with embedding vector similarity, in pgvector, using the neighbor gem. It's just one line of code -- but we start by extracting it to a
ChunkFetcherservice object so we can add more complicated code, primarily llimits and exclusions.The ability to say "not including these Chunks" (use case: that I already fetched), or "not including these Intevrviews" (same use case) is pretty easy ActiveRecord.
Harder, we want ot say "give me the top-ranked chunks, but no more than N-per interview." Googling and ChatGPT showed me there's a way to do that with a sub-query Common Table Expression (CTE) using ROW_NUMBER "window_function" with aggregates... phew! Fairly straightforward after I figured it out, then I figured out how to use ActiveRecord to generically wrap a query as a sub-query in larger query, so it could be "that query but limited to top-2 per interviewee".
Basic format of the SQL (as given to me by claude, ha) is:
You can ask chatgpt or claude for more info on it. :)
Then use it, to expand chunks to Claude
After some experimentation, I think this is a fine point to test at:
(It's possible point of diminishing returns is even a bit fewer chunks; adding TONS more chunks did not seem to help, see wiki).