Spaces:
Sleeping
Sleeping
Upload database.py with huggingface_hub
Browse files- database.py +83 -0
database.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Supabase 데이터베이스 연동 모듈
|
| 3 |
+
- 문서 저장 및 검색
|
| 4 |
+
- 벡터 유사도 검색 (pgvector)
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from supabase import create_client
|
| 8 |
+
from config import SUPABASE_URL, SUPABASE_KEY
|
| 9 |
+
from typing import List, Dict, Optional
|
| 10 |
+
|
| 11 |
+
# Supabase 클라이언트 초기화
|
| 12 |
+
supabase = create_client(SUPABASE_URL, SUPABASE_KEY) if SUPABASE_URL and SUPABASE_KEY else None
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def save_document(title: str, content: str, embedding: List[float], category: str = "일반") -> bool:
|
| 16 |
+
"""문서를 데이터베이스에 저장"""
|
| 17 |
+
if not supabase:
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
supabase.table("documents").insert({
|
| 22 |
+
"title": title,
|
| 23 |
+
"content": content,
|
| 24 |
+
"embedding": embedding,
|
| 25 |
+
"category": category
|
| 26 |
+
}).execute()
|
| 27 |
+
return True
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"문서 저장 오류: {e}")
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def search_documents(query_embedding: List[float], top_k: int = 3) -> List[Dict]:
|
| 34 |
+
"""벡터 유사도 기반 문서 검색"""
|
| 35 |
+
if not supabase:
|
| 36 |
+
return []
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
# Supabase RPC 함수 호출 (pgvector 유사도 검색)
|
| 40 |
+
result = supabase.rpc(
|
| 41 |
+
"match_documents",
|
| 42 |
+
{
|
| 43 |
+
"query_embedding": query_embedding,
|
| 44 |
+
"match_count": top_k
|
| 45 |
+
}
|
| 46 |
+
).execute()
|
| 47 |
+
return result.data if result.data else []
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"문서 검색 오류: {e}")
|
| 50 |
+
return []
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def get_all_documents(category: Optional[str] = None) -> List[Dict]:
|
| 54 |
+
"""모든 문서 조회"""
|
| 55 |
+
if not supabase:
|
| 56 |
+
return []
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
query = supabase.table("documents").select("id, title, content, category")
|
| 60 |
+
if category:
|
| 61 |
+
query = query.eq("category", category)
|
| 62 |
+
result = query.execute()
|
| 63 |
+
return result.data if result.data else []
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"문서 조회 오류: {e}")
|
| 66 |
+
return []
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def save_chat_history(user_message: str, bot_response: str, session_id: str) -> bool:
|
| 70 |
+
"""채팅 기록 저장"""
|
| 71 |
+
if not supabase:
|
| 72 |
+
return False
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
supabase.table("chat_history").insert({
|
| 76 |
+
"session_id": session_id,
|
| 77 |
+
"user_message": user_message,
|
| 78 |
+
"bot_response": bot_response
|
| 79 |
+
}).execute()
|
| 80 |
+
return True
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"채팅 기록 저장 오류: {e}")
|
| 83 |
+
return False
|