Spaces:
Running
Running
Download db/006_ebsi_embedding.sql from lilyjeongwon/math-tutor: direct link, hf CLI and curl.
- Browser
- Download file 3.99 kB
-
https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/db/006_ebsi_embedding.sql
- Command line
-
hf download hf://spaces/lilyjeongwon/math-tutor/db/006_ebsi_embedding.sql
-
curl -L -o 006_ebsi_embedding.sql https://huggingface.co/spaces/lilyjeongwon/math-tutor/resolve/main/db/006_ebsi_embedding.sql
3.99 kB
| -- ===================================================================== | |
| -- 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'; | |