Spaces:
Running

File size: 3,991 Bytes
b146445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- =====================================================================
-- k12-math-rag-tutor : 006_ebsi_embedding.sql
-- ๋œป(์˜๋ฏธ) ๊ฒ€์ƒ‰์„ ์œ„ํ•œ ๋ฒกํ„ฐ ์—ด๊ณผ, ๊ธ€์ž ๊ฒ€์ƒ‰ + ๋œป ๊ฒ€์ƒ‰์„ ์„ž์€ ๊ฒ€์ƒ‰ ํ•จ์ˆ˜.
-- ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ: BAAI/bge-m3 (1024์ฐจ์›, ์‚ฌ์šฉ์ž ์„ ํƒ 2026-12-01)
-- Supabase SQL Editor ์—์„œ ํ†ต์งธ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด๋„ ์•ˆ์ „)
-- ์ˆœ์„œ: 005_ebsi_search.sql ์„ ๋จผ์ € ์‹คํ–‰ํ•œ ๋’ค ์ด ํŒŒ์ผ
--
-- ์ด ํŒŒ์ผ์ด ํ•˜๋Š” ์ผ
--   1) pgvector ํ™•์žฅ (๋ฒกํ„ฐ ์ €์žฅยท๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ)
--   2) questions ์— embedding(1024์ฐจ์›) ์—ด๊ณผ embedding_model(์–ด๋–ค ๋ชจ๋ธ๋กœ ๋งŒ๋“ค์—ˆ๋Š”์ง€) ์—ด ์ถ”๊ฐ€
--   3) ๋ฒกํ„ฐ ๊ทผ์ ‘ ๊ฒ€์ƒ‰ ์ธ๋ฑ์Šค(HNSW, ์ฝ”์‚ฌ์ธ ๊ฑฐ๋ฆฌ)
--   4) ํ˜ผํ•ฉ ๊ฒ€์ƒ‰ ํ•จ์ˆ˜ search_items() โ€” ๋œป 0.7 + ๊ธ€์ž 0.3. ์„œ๋ฒ„(service_role)๋งŒ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
--
-- ์ฃผ์˜
--   * ์ฐจ์› 1024 ๋Š” bge-m3 ์ „์šฉ. ๋ชจ๋ธ์„ ๋ฐ”๊พธ๋ฉด ์—ด์„ ์ง€์šฐ๊ณ  ๋‹ค์‹œ ๋งŒ๋“ค์–ด์•ผ ํ•จ(๋‹ค๋ฅธ ๋ชจ๋ธ ๋ฒกํ„ฐ๋ผ๋ฆฌ๋Š” ๋น„๊ต ๋ถˆ๊ฐ€).
--   * ๊ฒ€์ƒ‰์–ด๋„ ๋ฐ˜๋“œ์‹œ ๊ฐ™์€ ๋ชจ๋ธ(bge-m3)๋กœ ๋ฒกํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด q_embedding ์œผ๋กœ ๋„˜๊ฒจ์•ผ ํ•จ.
--   * ์ž„๋ฒ ๋”ฉ ๊ฐ’์€ ์ ์žฌ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ฑ„์›€(์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์‹คํ–‰). ์ด ํŒŒ์ผ์€ ๋นˆ ์—ด๋งŒ ๋งŒ๋“ฆ.
-- =====================================================================

-- 1. ํ™•์žฅ
create extension if not exists vector with schema extensions;

-- 2. ์—ด
alter table questions
  add column if not exists embedding       extensions.vector(1024),
  add column if not exists embedding_model text;       -- ์˜ˆ: 'BAAI/bge-m3'

-- 3. ์ธ๋ฑ์Šค (์ฝ”์‚ฌ์ธ ๊ฑฐ๋ฆฌ)
create index if not exists questions_embedding_idx
  on questions using hnsw (embedding extensions.vector_cosine_ops);

-- 4. ํ˜ผํ•ฉ ๊ฒ€์ƒ‰ ํ•จ์ˆ˜
--   ์ ์ˆ˜ = 0.7 x ๋œป ์œ ์‚ฌ๋„(1 - ์ฝ”์‚ฌ์ธ ๊ฑฐ๋ฆฌ) + 0.3 x ๊ธ€์ž ์œ ์‚ฌ๋„(005 ์™€ ๊ฐ™์€ ๊ณ„์‚ฐ)
--   ์ž„๋ฒ ๋”ฉ์ด ์•„์ง ์—†๋Š” ๋ฌธํ•ญ์€ ๊ธ€์ž ์œ ์‚ฌ๋„๋งŒ์œผ๋กœ ์ ์ˆ˜๋ฅผ ๋งค๊น€.
--   p_student=true(๊ธฐ๋ณธ)์ด๋ฉด ๊ฒŒ์‹œ(published)ยท์ด์šฉํ—ˆ๋ฝ(cleared) ๋ฌธํ•ญ๋งŒ -> EBSi ์ดˆ์•ˆ์€ ํ•™์ƒ์—๊ฒŒ ์•ˆ ๋‚˜์˜ด.
create or replace function search_items(
  q_embedding extensions.vector(1024),
  q_text      text,
  p_grade     text       default null,
  p_year      smallint   default null,
  p_unit      text       default null,
  p_diff      difficulty default null,
  p_limit     int        default 5,
  p_student   boolean    default true
) returns table (id uuid, score real)
language sql stable
set search_path = public, extensions
as $$
  select q.id,
         (case when q.embedding is null or q_embedding is null
               then greatest(similarity(coalesce(q.search_text, ''), coalesce(q_text, '')),
                             word_similarity(coalesce(q_text, ''), coalesce(q.search_text, '')))
               else 0.7 * (1 - (q.embedding <=> q_embedding))
                  + 0.3 * greatest(similarity(coalesce(q.search_text, ''), coalesce(q_text, '')),
                                   word_similarity(coalesce(q_text, ''), coalesce(q.search_text, '')))
          end)::real as score
  from questions q
  where (q.embedding is not null or q.search_text is not null)
    and (not p_student or (q.status = 'published' and q.rights_status = 'cleared'))
    and (p_grade is null or q.grade_code = p_grade)
    and (p_year  is null or q.exam_year  = p_year)
    and (p_unit  is null or q.unit_id    = p_unit)
    and (p_diff  is null or q.difficulty = p_diff)
  order by score desc
  limit greatest(1, least(p_limit, 50));
$$;

revoke all on function search_items(extensions.vector, text, text, smallint, text, difficulty, int, boolean) from public;
revoke all on function search_items(extensions.vector, text, text, smallint, text, difficulty, int, boolean) from anon, authenticated;
grant execute on function search_items(extensions.vector, text, text, smallint, text, difficulty, int, boolean) to service_role;

-- ํ™•์ธ์šฉ
-- select count(*) filter (where embedding is not null) as ๋ฒกํ„ฐ์žˆ์Œ, count(*) as ์ „์ฒด from questions where source = 'ebsi';