|
| 1 | +require_relative '../../test_helper' |
| 2 | + |
| 3 | +class Bot::Alegre5Test < ActiveSupport::TestCase |
| 4 | + def setup |
| 5 | + @team = create_team |
| 6 | + @pm1 = create_project_media team: @team |
| 7 | + @pm2 = create_project_media team: @team |
| 8 | + @ex1 = create_explainer team: @team |
| 9 | + @ex2 = create_explainer team: @team |
| 10 | + end |
| 11 | + |
| 12 | + def teardown |
| 13 | + end |
| 14 | + |
| 15 | + test "should rank results based on vector models rank when prioritizing matches" do |
| 16 | + pm_id_scores_array = [ |
| 17 | + { score: 0.75, context: { 'team_id' => @team.id, 'project_media_id' => @pm1.id, 'has_custom_id' => true, 'field' => 'original_title', 'temporary_media' => false }, model: Bot::Alegre::FILIPINO_MODEL }, |
| 18 | + { score: 0.85, context: { 'team_id' => @team.id, 'project_media_id' => @pm2.id, 'has_custom_id' => true, 'field' => 'original_title', 'temporary_media' => false }, model: Bot::Alegre::MEAN_TOKENS_MODEL } |
| 19 | + ] |
| 20 | + pm_id_scores_hash = { |
| 21 | + @pm1.id => { |
| 22 | + score: 0.75, |
| 23 | + context: { 'has_custom_id' => true, 'field' => 'original_description', 'project_media_id' => @pm1.id, 'temporary_media' => false, 'team_id' => @team.id }, |
| 24 | + model: Bot::Alegre::FILIPINO_MODEL, |
| 25 | + source_field: 'original_description', |
| 26 | + target_field: 'original_description', |
| 27 | + relationship_type: { source: 'confirmed_sibling', target: 'confirmed_sibling' } |
| 28 | + }, |
| 29 | + @pm2.id => { |
| 30 | + score: 0.85, |
| 31 | + context: { 'has_custom_id' => true, 'field' => 'original_description', 'project_media_id' => @pm2.id, 'temporary_media' => false, 'team_id' => @team.id }, |
| 32 | + model: Bot::Alegre::MEAN_TOKENS_MODEL, |
| 33 | + source_field: 'original_description', |
| 34 | + target_field: 'original_description', |
| 35 | + relationship_type: { source: 'confirmed_sibling', target: 'confirmed_sibling' } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + assert_equal @pm1.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_hash).first.first |
| 40 | + assert_equal @pm1.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_array).first.dig(:context, 'project_media_id') |
| 41 | + assert_equal @pm1.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_array.reverse).first.dig(:context, 'project_media_id') |
| 42 | + |
| 43 | + pm_id_scores_hash[@pm2.id][:model] = Bot::Alegre::OPENAI_ADA_MODEL |
| 44 | + pm_id_scores_array[1][:model] = Bot::Alegre::OPENAI_ADA_MODEL |
| 45 | + |
| 46 | + assert_equal @pm2.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_hash).first.first |
| 47 | + assert_equal @pm2.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_array).first.dig(:context, 'project_media_id') |
| 48 | + assert_equal @pm2.id, Bot::Alegre.return_prioritized_matches(pm_id_scores_array.reverse).first.dig(:context, 'project_media_id') |
| 49 | + end |
| 50 | + |
| 51 | + test "should rank results based on vector models rank when parsing fact-check search results" do |
| 52 | + results = { |
| 53 | + @pm1.id => { |
| 54 | + score: 0.75, |
| 55 | + context: { 'team_id' => @team.id, 'project_media_id' => @pm1.id, 'has_custom_id' => true, 'field' => 'claim_description_content|report_visual_card_title', 'temporary_media' => false, 'contexts_count' => 14 }, |
| 56 | + model: Bot::Alegre::FILIPINO_MODEL |
| 57 | + }, |
| 58 | + @pm2.id => { |
| 59 | + score: 0.85, |
| 60 | + context: { 'team_id' => @team.id, 'project_media_id' => @pm2.id, 'has_custom_id' => true, 'field' => 'claim_description_content|report_visual_card_title', 'temporary_media' => false, 'contexts_count' => 4 }, |
| 61 | + model: Bot::Alegre::MEAN_TOKENS_MODEL |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + assert_equal @pm1.id, Bot::Smooch.parse_search_results_from_alegre(results, 10, false).first.id |
| 66 | + |
| 67 | + results[@pm2.id][:model] = Bot::Alegre::OPENAI_ADA_MODEL |
| 68 | + |
| 69 | + assert_equal @pm2.id, Bot::Smooch.parse_search_results_from_alegre(results, 10, false).first.id |
| 70 | + end |
| 71 | + |
| 72 | + test "should rank results based on vector models rank when parsing explainer search results" do |
| 73 | + response = { |
| 74 | + 'result' => [ |
| 75 | + { |
| 76 | + 'content_hash' => 'abc123', |
| 77 | + 'doc_id' => 'xyz321', |
| 78 | + 'context' => { 'type' => 'explainer', 'team_id' => @team.id, 'language' => 'en', 'explainer_id' => @ex1.id, 'paragraph' => 1 }, |
| 79 | + 'models' => [Bot::Alegre::FILIPINO_MODEL], |
| 80 | + 'suppress_search_response' => true, |
| 81 | + 'content' => 'Foo', |
| 82 | + 'created_at' => '2025-04-05T01:59:08.010665', |
| 83 | + 'language' => nil, |
| 84 | + 'suppress_response' => false, |
| 85 | + 'contexts' => [{ 'type' => 'explainer', 'team_id' => @team.id, 'language' => 'en', 'explainer_id' => @ex1.id, 'paragraph' => 1 }], |
| 86 | + 'model' => Bot::Alegre::FILIPINO_MODEL, |
| 87 | + '_id' => 'qwe789', |
| 88 | + 'id' => 'qwe789', |
| 89 | + 'index' => 'alegre_similarity', |
| 90 | + '_score' => 0.75, |
| 91 | + 'score' => 0.75 |
| 92 | + }, |
| 93 | + { |
| 94 | + 'content_hash' => 'abc456', |
| 95 | + 'doc_id' => 'xyz654', |
| 96 | + 'context' => { 'type' => 'explainer', 'team_id' => @team.id, 'language' => 'en', 'explainer_id' => @ex2.id, 'paragraph' => 1 }, |
| 97 | + 'models' => [Bot::Alegre::MEAN_TOKENS_MODEL], |
| 98 | + 'suppress_search_response' => true, |
| 99 | + 'content' => 'Foo', |
| 100 | + 'created_at' => '2025-04-04T01:59:08.010665', |
| 101 | + 'language' => nil, |
| 102 | + 'suppress_response' => false, |
| 103 | + 'contexts' => [{ 'type' => 'explainer', 'team_id' => @team.id, 'language' => 'en', 'explainer_id' => @ex2.id, 'paragraph' => 1 }], |
| 104 | + 'model' => Bot::Alegre::MEAN_TOKENS_MODEL, |
| 105 | + '_id' => 'qwe987', |
| 106 | + 'id' => 'qwe987', |
| 107 | + 'index' => 'alegre_similarity', |
| 108 | + '_score' => 0.85, |
| 109 | + 'score' => 0.85 |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + |
| 114 | + assert_equal @ex1.id, Explainer.sort_similarity_search_results(response).first.dig('context', 'explainer_id') |
| 115 | + |
| 116 | + response['result'][1]['model'] = Bot::Alegre::OPENAI_ADA_MODEL |
| 117 | + response['result'][1]['models'] = [Bot::Alegre::OPENAI_ADA_MODEL] |
| 118 | + |
| 119 | + assert_equal @ex2.id, Explainer.sort_similarity_search_results(response).first.dig('context', 'explainer_id') |
| 120 | + end |
| 121 | +end |
0 commit comments