rrf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +10 -0
- data/lib/configuration.rb +9 -0
- data/lib/rrf/version.rb +5 -0
- data/lib/rrf.rb +59 -0
- data/sig/rrf.rbs +4 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0e291263a6f09f559b804dbcab1332b9b110d0a5ecb6c707d2156ad4583d9873
|
4
|
+
data.tar.gz: 28524ae014e0c0ca2fa8a20f553008b3cf8b6c5e7df6101abeef514e325ab8f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ba1b4d3d5c593c864ab81824b1236d99e3d2f9ad805c92f15b895504dbad84142de698363664f7ce83a2457ea04c1fc4655ddb7d8dda535b5564f089c9981a9
|
7
|
+
data.tar.gz: d7bf0020c66eb425853e4135cb2c452000c388f190fdf094e41d556b6f4ead73e316d4c5f43893568d3a11b6a1cef47577dec921ba13e78d87b0718a9dcc8e60
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at chris@petersen.io. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Chris Petersen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# RRF
|
2
|
+
|
3
|
+
RRF (Reciprocal Rank Fusion) is a Ruby gem that provides an implementation of the Reciprocal Rank Fusion (RRF) algorithm to merge and rank results from different search engines, specifically supporting ActiveRecord and Searchkick.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rrf'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
gem install rrf
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To use RRF, include the module in your ActiveRecord model and configure it as needed.
|
28
|
+
|
29
|
+
### Example
|
30
|
+
|
31
|
+
Let’s assume you have a model named Chunk that uses both ActiveRecord (with pgvector) and Searchkick:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Chunk < ApplicationRecord
|
35
|
+
include Searchkick
|
36
|
+
include RRF::Model
|
37
|
+
|
38
|
+
# ActiveRecord vector search using pgvector
|
39
|
+
# Assuming `embedding768` is a column of type `vector` (pgvector)
|
40
|
+
|
41
|
+
# Define any additional logic or methods as needed
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
### Performing Searches and Fusing Results
|
46
|
+
|
47
|
+
You can perform searches using ActiveRecord and Searchkick, and then fuse the results using the fuse method:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# Configure the constant value if needed
|
51
|
+
RRF.configure do |config|
|
52
|
+
config.rank_bias = 70 # User-defined value
|
53
|
+
end
|
54
|
+
|
55
|
+
# Perform Searchkick search
|
56
|
+
es_result = Chunk.search("hi", load: false, limit: 50)
|
57
|
+
|
58
|
+
# Perform ActiveRecord nearest neighbor search
|
59
|
+
query_embedding = [0.1, 0.2, 0.3, ...] # Example embedding
|
60
|
+
ar_result = Chunk.where("body ilike ?", "%hi%").limit(50)
|
61
|
+
|
62
|
+
# Fuse the results and limit to 10
|
63
|
+
result = Chunk.fuse(ar_result, es_result, limit: 10)
|
64
|
+
|
65
|
+
# `result` now contains the top 10 fused search results
|
66
|
+
```
|
67
|
+
|
68
|
+
### Configuration
|
69
|
+
|
70
|
+
You can configure the rank_bias value used in the RRF algorithm:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
RRF.configure do |config|
|
74
|
+
config.rank_bias = 70 # Default is 60
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
## Development
|
79
|
+
|
80
|
+
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
|
81
|
+
|
82
|
+
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/assaydepot/rrf.
|
87
|
+
|
88
|
+
## License
|
89
|
+
|
90
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/rrf/version.rb
ADDED
data/lib/rrf.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rrf/version"
|
4
|
+
require "configuration"
|
5
|
+
|
6
|
+
module RRF
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
# Default configuration
|
10
|
+
@configuration = Configuration.new
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :configuration
|
14
|
+
|
15
|
+
def configure
|
16
|
+
yield(configuration)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Model
|
21
|
+
def self.included(base)
|
22
|
+
base.extend(ClassMethods)
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
def fuse(*result_sets, limit: 10)
|
27
|
+
rank_bias = RRF.configuration.rank_bias
|
28
|
+
record_scores = Hash.new { |hash, key| hash[key] = { score: 0, id: key, sources: [] } }
|
29
|
+
|
30
|
+
result_sets.each do |result_set|
|
31
|
+
if result_set.is_a?(ActiveRecord::Relation)
|
32
|
+
result_set.select(:id).each_with_index do |record, index|
|
33
|
+
score = 1.0 / (rank_bias + index)
|
34
|
+
record_scores[record.id][:score] += score
|
35
|
+
record_scores[record.id][:sources] << :active_record
|
36
|
+
end
|
37
|
+
elsif result_set.is_a?(Searchkick::Relation)
|
38
|
+
result_set.hits.each_with_index do |hit, index|
|
39
|
+
id = hit["_id"]
|
40
|
+
score = 1.0 / (rank_bias + index)
|
41
|
+
record_scores[id][:score] += score
|
42
|
+
record_scores[id][:sources] << :searchkick
|
43
|
+
end
|
44
|
+
else
|
45
|
+
raise RRF::Error, "Unsupported result set type: #{result_set.class}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Sort by score and limit results
|
50
|
+
top_record_ids = record_scores.sort_by { |_id, result| -result[:score] }.to_h.keys.first(limit)
|
51
|
+
|
52
|
+
# Load actual records from ActiveRecord
|
53
|
+
self.where(id: top_record_ids).index_by(&:id).values_at(*top_record_ids).each do |record|
|
54
|
+
record.define_singleton_method(:_rrf_score) { record_scores[record.id][:score] }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/sig/rrf.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rrf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Petersen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem provides an implementation of Reciprocal Rank Fusion (RRF) to
|
14
|
+
merge results from different search engines. Initially supporting Active Record
|
15
|
+
and Elasticsearch.
|
16
|
+
email:
|
17
|
+
- chris@scientist.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".rspec"
|
23
|
+
- ".standard.yml"
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/configuration.rb
|
29
|
+
- lib/rrf.rb
|
30
|
+
- lib/rrf/version.rb
|
31
|
+
- sig/rrf.rbs
|
32
|
+
homepage: https://github.com/assaydepot/rrf
|
33
|
+
licenses:
|
34
|
+
- MIT
|
35
|
+
metadata:
|
36
|
+
homepage_uri: https://github.com/assaydepot/rrf
|
37
|
+
source_code_uri: https://github.com/assaydepot/rrf
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.6.0
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.5.3
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: A Ruby gem for Reciprocal Rank Fusion across different search engines
|
57
|
+
test_files: []
|