|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -module AiIntelligence |
4 | | - # Calculates an N×N cosine-similarity matrix from 64-dimensional champion embeddings. |
5 | | - # |
6 | | - # Embeddings are loaded once per 24h from champion_embeddings_64d.json via Rails.cache. |
7 | | - # Primary path: ai_service/data/champion_embeddings_64d.json |
8 | | - # Fallback path: models/champion_embeddings_64d.json (prostaff-ml artefact) |
9 | | - class SynergyMatrixService |
10 | | - EMBEDDINGS_FILE = Rails.root.join('ai_service', 'data', 'champion_embeddings_64d.json').freeze |
11 | | - FALLBACK_FILE = Rails.root.join('models', 'champion_embeddings_64d.json').freeze |
12 | | - CACHE_KEY = 'ai_intelligence/champion_embeddings_64d' |
13 | | - CACHE_TTL = 24.hours |
| 3 | +# Calculates an N×N cosine-similarity matrix from 64-dimensional champion embeddings. |
| 4 | +# |
| 5 | +# Embeddings are loaded once per 24h from champion_embeddings_64d.json via Rails.cache. |
| 6 | +# Primary path: ai_service/data/champion_embeddings_64d.json |
| 7 | +# Fallback path: models/champion_embeddings_64d.json (prostaff-ml artefact) |
| 8 | +class SynergyMatrixService |
| 9 | + EMBEDDINGS_FILE = Rails.root.join('ai_service', 'data', 'champion_embeddings_64d.json').freeze |
| 10 | + FALLBACK_FILE = Rails.root.join('models', 'champion_embeddings_64d.json').freeze |
| 11 | + CACHE_KEY = 'ai_intelligence/champion_embeddings_64d' |
| 12 | + CACHE_TTL = 24.hours |
14 | 13 |
|
15 | | - # @param champions [Array<String>] 2–10 champion names |
16 | | - # @return [Hash] { champions:, matrix:, top_pairs:, weakest_pairs: } |
17 | | - def self.call(champions:) |
18 | | - embs = embeddings |
19 | | - resolved = champions.filter_map do |c| |
20 | | - vec = embs[c] || embs[c.downcase] |
21 | | - [c, vec] if vec |
22 | | - end.to_h |
| 14 | + # @param champions [Array<String>] 2–10 champion names |
| 15 | + # @return [Hash] { champions:, matrix:, top_pairs:, weakest_pairs: } |
| 16 | + def self.call(champions:) |
| 17 | + embs = embeddings |
| 18 | + resolved = champions.filter_map do |c| |
| 19 | + vec = embs[c] || embs[c.downcase] |
| 20 | + [c, vec] if vec |
| 21 | + end.to_h |
23 | 22 |
|
24 | | - present = resolved.keys |
25 | | - return { champions: present, matrix: [], top_pairs: [], weakest_pairs: [] } if present.size < 2 |
| 23 | + present = resolved.keys |
| 24 | + return { champions: present, matrix: [], top_pairs: [], weakest_pairs: [] } if present.size < 2 |
26 | 25 |
|
27 | | - matrix = present.map.with_index do |a, i| |
28 | | - present.map.with_index do |b, j| |
29 | | - i == j ? 1.0 : cosine_similarity(resolved[a], resolved[b]) |
30 | | - end |
| 26 | + matrix = present.map.with_index do |a, i| |
| 27 | + present.map.with_index do |b, j| |
| 28 | + i == j ? 1.0 : cosine_similarity(resolved[a], resolved[b]) |
31 | 29 | end |
| 30 | + end |
32 | 31 |
|
33 | | - pairs = [] |
34 | | - present.combination(2).each do |a, b| |
35 | | - ia = present.index(a) |
36 | | - ib = present.index(b) |
37 | | - pairs << { pair: [a, b], score: matrix[ia][ib].round(4) } |
38 | | - end |
39 | | - pairs.sort_by! { |p| -p[:score] } |
40 | | - |
41 | | - { |
42 | | - champions: present, |
43 | | - matrix: matrix.map { |row| row.map { |v| v.round(4) } }, |
44 | | - top_pairs: pairs.first(5), |
45 | | - weakest_pairs: pairs.last(3) |
46 | | - } |
| 32 | + pairs = [] |
| 33 | + present.combination(2).each do |a, b| |
| 34 | + ia = present.index(a) |
| 35 | + ib = present.index(b) |
| 36 | + pairs << { pair: [a, b], score: matrix[ia][ib].round(4) } |
47 | 37 | end |
| 38 | + pairs.sort_by! { |p| -p[:score] } |
48 | 39 |
|
49 | | - # ── private ────────────────────────────────────────────────────────── |
| 40 | + { |
| 41 | + champions: present, |
| 42 | + matrix: matrix.map { |row| row.map { |v| v.round(4) } }, |
| 43 | + top_pairs: pairs.first(5), |
| 44 | + weakest_pairs: pairs.last(3) |
| 45 | + } |
| 46 | + end |
50 | 47 |
|
51 | | - def self.embeddings |
52 | | - Rails.cache.fetch(CACHE_KEY, expires_in: CACHE_TTL) { load_embeddings } |
53 | | - end |
54 | | - private_class_method :embeddings |
| 48 | + # ── private ────────────────────────────────────────────────────────── |
55 | 49 |
|
56 | | - def self.load_embeddings |
57 | | - path = EMBEDDINGS_FILE.exist? ? EMBEDDINGS_FILE : FALLBACK_FILE |
58 | | - raise "Champion embeddings file not found (tried #{EMBEDDINGS_FILE} and #{FALLBACK_FILE})" unless path.exist? |
| 50 | + def self.embeddings |
| 51 | + Rails.cache.fetch(CACHE_KEY, expires_in: CACHE_TTL) { load_embeddings } |
| 52 | + end |
| 53 | + private_class_method :embeddings |
59 | 54 |
|
60 | | - JSON.parse(File.read(path)) |
61 | | - end |
62 | | - private_class_method :load_embeddings |
| 55 | + def self.load_embeddings |
| 56 | + path = EMBEDDINGS_FILE.exist? ? EMBEDDINGS_FILE : FALLBACK_FILE |
| 57 | + raise "Champion embeddings file not found (tried #{EMBEDDINGS_FILE} and #{FALLBACK_FILE})" unless path.exist? |
63 | 58 |
|
64 | | - def self.cosine_similarity(a, b) |
65 | | - dot = a.zip(b).sum { |x, y| x * y } |
66 | | - na = Math.sqrt(a.sum { |x| x**2 }) |
67 | | - nb = Math.sqrt(b.sum { |x| x**2 }) |
68 | | - return 0.0 if na < 1e-9 || nb < 1e-9 |
| 59 | + JSON.parse(File.read(path)) |
| 60 | + end |
| 61 | + private_class_method :load_embeddings |
69 | 62 |
|
70 | | - (dot / (na * nb)).clamp(-1.0, 1.0) |
71 | | - end |
72 | | - private_class_method :cosine_similarity |
| 63 | + def self.cosine_similarity(a, b) |
| 64 | + dot = a.zip(b).sum { |x, y| x * y } |
| 65 | + na = Math.sqrt(a.sum { |x| x**2 }) |
| 66 | + nb = Math.sqrt(b.sum { |x| x**2 }) |
| 67 | + return 0.0 if na < 1e-9 || nb < 1e-9 |
| 68 | + |
| 69 | + (dot / (na * nb)).clamp(-1.0, 1.0) |
73 | 70 | end |
| 71 | + private_class_method :cosine_similarity |
74 | 72 | end |
0 commit comments