Spaces:
Runtime error
Runtime error
File size: 6,237 Bytes
1e570c8 704a147 1e570c8 31e7ade 1e570c8 31e7ade 1e570c8 0bbd198 1e570c8 0bbd198 aea0bb5 0bbd198 aea0bb5 31e7ade aea0bb5 1e570c8 1990560 31e7ade 1990560 1e570c8 afc8097 1e570c8 31e7ade d6d8861 79a0126 afc8097 d6d8861 79a0126 afc8097 d6d8861 0bbd198 afc8097 f537bfd 14035b1 31e7ade 14035b1 afc8097 0bbd198 31e7ade 0bbd198 | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | from supabase import create_client, Client
from typing import List, Dict, Any
import os
# ํ๊ฒฝ ๋ณ์ ๋ก๋ ๋ฐ Supabase ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "100"))
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
if not SUPABASE_URL or not SUPABASE_KEY:
raise ValueError("SUPABASE_URL๊ณผ SUPABASE_KEY๊ฐ ์ค์ ๋์ด ์์ด์ผ ํฉ๋๋ค.")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
def save_embedding(
document_id: str,
chunk: str,
embedding: List[float],
chunk_index: int,
file_hash: str | None = None,
) -> Dict:
"""
[TODO Day 2] ๋ฌธ์ ์ฒญํฌ์ ์๋ฒ ๋ฉ ๋ฒกํฐ๋ฅผ Supabase DB์ ์ ์ฅํ๋ ํจ์์
๋๋ค.
์๊ตฌ์ฌํญ:
1. README.md์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ตฌ์ฑ ์น์
์์ ์์ฑํ ํ
์ด๋ธ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ์ธ์.
- ์ด๋ค ์ปฌ๋ผ์ ์ด๋ค ๊ฐ์ ์ ์ฅํด์ผ ํ๋์ง SQL ์คํค๋ง๋ฅผ ์ง์ ํ์ธํ์ธ์.
2. Supabase Python ํด๋ผ์ด์ธํธ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ง์ ์กฐ์ฌํ์ธ์.
- ํํธ: ์์์ ์ด๋ฏธ ์ด๊ธฐํ๋ `supabase` ํด๋ผ์ด์ธํธ๋ฅผ ํ์ฉํ์ธ์.
Args:
document_id (str): ๋ฌธ์ ID
chunk (str): ํ
์คํธ ์ฒญํฌ ๋ด์ฉ
embedding (List[float]): ์๋ฒ ๋ฉ ๋ฒกํฐ
chunk_index (int): ์ฒญํฌ ์์ ์ธ๋ฑ์ค
Returns:
Dict: ์ ์ฅ๋ ๋ฐ์ดํฐ ์ ๋ณด
"""
# ---------------------------------------------------------
# [Day 2] ๊ณผ์ : ์ฌ๊ธฐ์ DB ์ ์ฅ ๋ก์ง์ ์์ฑํ์ธ์.
# ---------------------------------------------------------
if file_hash:
metadata = {"file_hash": file_hash}
else:
metadata = {}
data = {
"document_id": document_id,
"content": chunk,
"embedding": embedding,
"chunk_index": chunk_index,
"metadata": metadata,
}
result = supabase.table("document_chunks").insert(data).execute()
return result.data
def save_embeddings_batch(
document_id: str,
chunks: List[str],
embeddings: List[List[float]],
file_hash: str | None = None,
) -> None:
"""
์ฌ๋ฌ ์ฒญํฌ๋ฅผ ํ ๋ฒ์ DB์ ์ ์ฅํฉ๋๋ค.
"""
metadata = {"file_hash": file_hash} if file_hash else {}
rows = [
{
"document_id": document_id,
"content": chunk,
"embedding": embedding,
"chunk_index": i,
"metadata": metadata,
}
for i, (chunk, embedding) in enumerate(zip(chunks, embeddings))
]
# Supabase๋ ํ ๋ฒ์ ์ฌ๋ฌ ํ insert ์ง์
for i in range(0, len(rows), BATCH_SIZE):
batch = rows[i : i + BATCH_SIZE]
supabase.table("document_chunks").insert(batch).execute()
def search_similar_embeddings(
query_embedding: List[float],
document_id: str,
limit: int = 10,
similarity_threshold: float = 0.6,
) -> List[Dict[str, Any]]:
"""
[TODO Day 3] ์ฌ์ฉ์์ ์ง๋ฌธ ๋ฒกํฐ์ ์ ์ฌํ ๋ฌธ์ ์ฒญํฌ๋ฅผ ๊ฒ์ํ๋ ํจ์์
๋๋ค.
์๊ตฌ์ฌํญ:
1. README.md์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ตฌ์ฑ ์น์
์์ ์์ฑํ RPC ํจ์๋ฅผ ํ์ฉํ์ฌ ์ ์ฌ๋ ๊ฒ์์ ์ํํ์ธ์.
- ์ด๋ค ์ธ์๋ฅผ ์ ๋ฌํด์ผ ํ๋์ง SQL ์คํค๋ง์ RPC ํจ์ ์ ์๋ฅผ ์ง์ ํ์ธํ์ธ์.
2. ์ ์ฌ๋(similarity)๊ฐ similarity_threshold ์ด์์ธ ๊ฒฐ๊ณผ๋ง ํํฐ๋งํ์ธ์. (์ ํ์ฌํญ)
Args:
query_embedding (List[float]): ์ง๋ฌธ์ ์๋ฒ ๋ฉ ๋ฒกํฐ
document_id (str): ๊ฒ์ํ ๋ฌธ์ ID
limit (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
similarity_threshold (float): ์ ์ฌ๋ ์๊ณ๊ฐ
Returns:
List[Dict[str, Any]]: ์ ์ฌํ ๋ฌธ์ ์ฒญํฌ ๋ฆฌ์คํธ
"""
result = supabase.rpc(
"match_documents",
{
"query_embedding": query_embedding,
"match_count": limit,
"p_document_id": document_id,
},
).execute()
# ๋๋ฒ๊น
: ๋ฒกํฐ ๊ฒ์ ๊ฒฐ๊ณผ ์์ฝ
print(f"\n{'='*60}")
print(
f" ๋ฒกํฐ ๊ฒ์ ๊ฒฐ๊ณผ (์ด {len(result.data)}๊ฑด, ์๊ณ๊ฐ >= {similarity_threshold})"
)
print(f"{'='*60}")
for i, row in enumerate(result.data, 1):
sim = row.get("similarity", 0)
preview = row.get("content", "")[:60].replace("\n", " ")
marker = "โ" if sim >= similarity_threshold else "โ"
print(f" {marker} [{i:2d}] ์ ์ฌ๋ {sim:.4f} | {preview}...")
print(f"{'='*60}")
filtered = [
row for row in result.data if row.get("similarity", 0) >= similarity_threshold
]
print(f" โ ํต๊ณผ: {len(filtered)}๊ฑด / ์ ์ธ: {len(result.data) - len(filtered)}๊ฑด")
return filtered
def search_by_keyword(
query: str, document_id: str, limit: int = 5
) -> List[Dict[str, Any]]:
"""ํค์๋ ๊ธฐ๋ฐ ๊ฒ์ (๋ฒกํฐ ๊ฒ์ ๋ณด์์ฉ). ๊ณ ์ ๋ช
์ฌ ๋ฑ ์ ํํ ๋งค์นญ์ด ํ์ํ ๋ ์ฌ์ฉ."""
STOP_WORDS = {
"์",
"๋",
"์ด",
"๊ฐ",
"์",
"๋ฅผ",
"์",
"์",
"์์",
"๋ก",
"์ผ๋ก",
"์",
"๊ณผ",
"๋",
"๋ง",
"์๊ฒ",
"ํ",
"ํ๋",
"์๋",
"์๋",
"์๋ ค์ฃผ์ธ์",
"์๋ ค์ค",
"๋ฌด์์ธ๊ฐ์",
"๋ญ์ผ",
"์ด๋ค",
}
words = query.replace("?", "").replace(".", "").replace(",", "").split()
keywords = [w for w in words if len(w) > 1 and w not in STOP_WORDS]
if not keywords:
return []
seen = set()
results = []
for kw in keywords:
rows = (
supabase.table("document_chunks")
.select("content")
.eq("document_id", document_id)
.ilike("content", f"%{kw}%")
.limit(limit)
.execute()
).data
for row in rows:
if row["content"] not in seen:
seen.add(row["content"])
results.append(row)
return results[:limit]
def get_document_id_by_hash(file_hash: str) -> str:
result = supabase.rpc("get_document_by_hash", {"p_hash": file_hash}).execute()
if result.data:
return result.data[0]["document_id"]
return None
|