File size: 8,329 Bytes
4a98f26 |
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
"""
๊ฐ์ ๋ ๋ฒกํฐ ์คํ ์ด ๋ชจ๋ - Milvus ์ค์ ์ต์ ํ
"""
from typing import List, Dict, Any, Optional
import uuid
from langchain.schema import Document
# ๋ฒกํฐ ์คํ ์ด ์ํฌํธ
try:
# ์ต์ ๋ฒ์ ์ํฌํธ
from langchain_milvus import Milvus
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
MODERN_IMPORTS = True
print("์ต์ langchain ํจํค์ง ์ํฌํธ ์ฑ๊ณต")
except ImportError:
# ์ด์ ๋ฒ์ ์ํฌํธ
from langchain_community.vectorstores import Milvus, FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
MODERN_IMPORTS = False
print("๋ ๊ฑฐ์ langchain_community ํจํค์ง ์ฌ์ฉ")
from config import MILVUS_HOST, MILVUS_PORT, MILVUS_COLLECTION, EMBEDDING_MODEL
class VectorStore:
def __init__(self, use_milvus: bool = True):
"""
๋ฒกํฐ ์คํ ์ด ์ด๊ธฐํ
Args:
use_milvus: Milvus ์ฌ์ฉ ์ฌ๋ถ (False์ด๋ฉด FAISS ์ฌ์ฉ)
"""
self.use_milvus = use_milvus
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ์ค์
print(f"์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ ์ค: {EMBEDDING_MODEL}")
model_kwargs = {
"device": "cpu",
"trust_remote_code": True # ์๊ฒฉ ์ฝ๋ ์คํ ํ์ฉ (ํ์)
}
encode_kwargs = {"normalize_embeddings": True}
self.embeddings = HuggingFaceEmbeddings(
model_name=EMBEDDING_MODEL,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
self.vector_store = None
print(f"์๋ฒ ๋ฉ ๋ชจ๋ธ ์ด๊ธฐํ ์๋ฃ: {EMBEDDING_MODEL}")
def init_milvus(self) -> Milvus:
"""
Milvus ๋ฒกํฐ ์คํ ์ด ์ด๊ธฐํ
Returns:
Milvus ๋ฒกํฐ ์คํ ์ด ์ธ์คํด์ค
"""
connection_args = {
"host": MILVUS_HOST,
"port": MILVUS_PORT,
}
# ๋ฒกํฐ ๊ฒ์ ์ธ๋ฑ์ค ํ๋ผ๋ฏธํฐ (FLAT ์ธ๋ฑ์ค ๋ฐ ์ฝ์ฌ์ธ ์ ์ฌ๋ ๋ฉํธ๋ฆญ)
index_params = {
"index_type": "FLAT", # ์ ํ๋ ์ฐ์ FLAT ์ธ๋ฑ์ค
"metric_type": "COSINE", # ์ฝ์ฌ์ธ ์ ์ฌ๋ (์ ๊ทํ๋ ๋ฒกํฐ์ ์ ํฉ)
"params": {} # FLAT ์ธ๋ฑ์ค์๋ ์ถ๊ฐ ํ๋ผ๋ฏธํฐ ์์
}
return Milvus(
embedding_function=self.embeddings,
collection_name=MILVUS_COLLECTION,
connection_args=connection_args,
index_params=index_params
)
def init_faiss(self) -> FAISS:
"""
FAISS ๋ฒกํฐ ์คํ ์ด ์ด๊ธฐํ (๋ก์ปฌ ๋์ฒด์ฉ)
Returns:
FAISS ๋ฒกํฐ ์คํ ์ด ์ธ์คํด์ค
"""
return FAISS.from_documents([], self.embeddings)
def create_or_load(self, documents: Optional[List[Document]] = None) -> Any:
"""
๋ฒกํฐ ์คํ ์ด ์์ฑ ๋๋ ๋ก๋
Args:
documents: ์ ์ฅํ ๋ฌธ์ ๋ฆฌ์คํธ (None์ด๋ฉด ๋น ์คํ ์ด ์์ฑ)
Returns:
๋ฒกํฐ ์คํ ์ด ์ธ์คํด์ค
"""
if self.use_milvus:
if documents:
# ๋ฌธ์๊ฐ ์ ๊ณต๋ ๊ฒฝ์ฐ ์ ์ปฌ๋ ์
์์ฑ
try:
# ์ฐ๊ฒฐ ์ค์
connection_args = {
"host": MILVUS_HOST,
"port": MILVUS_PORT,
}
# ๊ฒ์ ์ธ๋ฑ์ค ์ค์
index_params = {
"index_type": "FLAT", # ์ ํ๋ ์ฐ์
"metric_type": "COSINE", # ์ฝ์ฌ์ธ ์ ์ฌ๋
"params": {}
}
print(f"Milvus ์ปฌ๋ ์
์์ฑ: {MILVUS_COLLECTION} (๊ธฐ์กด ์ปฌ๋ ์
์ญ์ )")
# ๋ฌธ์๋ก๋ถํฐ Milvus ์ปฌ๋ ์
์์ฑ
self.vector_store = Milvus.from_documents(
documents=documents,
embedding=self.embeddings,
collection_name=MILVUS_COLLECTION,
connection_args=connection_args,
index_params=index_params,
drop_old=True # ๊ธฐ์กด ์ปฌ๋ ์
์ญ์ (์ฌ๊ตฌ์ถ)
)
print(f"Milvus ์ปฌ๋ ์
์์ฑ ์๋ฃ: {len(documents)}๊ฐ ๋ฌธ์ ์ธ๋ฑ์ฑ๋จ")
except Exception as e:
print(f"Milvus ์ปฌ๋ ์
์์ฑ ์คํจ: {e}")
# ๋์ฒด ๋ฐฉ์์ผ๋ก FAISS ์ฌ์ฉ
print("๋์ฒด ๋ฐฉ์์ผ๋ก FAISS ์ฌ์ฉ")
self.use_milvus = False
self.vector_store = FAISS.from_documents(documents, self.embeddings)
else:
# ๊ธฐ์กด ์ปฌ๋ ์
๋ก๋
try:
self.vector_store = self.init_milvus()
except Exception as e:
print(f"Milvus ์ปฌ๋ ์
๋ก๋ ์คํจ: {e}")
# ๋์ฒด ๋ฐฉ์์ผ๋ก FAISS ์ฌ์ฉ
print("๋์ฒด ๋ฐฉ์์ผ๋ก FAISS ์ฌ์ฉ")
self.use_milvus = False
self.vector_store = self.init_faiss()
else:
# FAISS ์ฌ์ฉ
if documents:
print(f"FAISS ์ธ๋ฑ์ค ์์ฑ: {len(documents)}๊ฐ ๋ฌธ์")
self.vector_store = FAISS.from_documents(documents, self.embeddings)
print("FAISS ์ธ๋ฑ์ค ์์ฑ ์๋ฃ")
else:
self.vector_store = self.init_faiss()
print("๋น FAISS ์ธ๋ฑ์ค ์ด๊ธฐํ ์๋ฃ")
return self.vector_store
def add_documents(self, documents: List[Document]) -> None:
"""
๋ฒกํฐ ์คํ ์ด์ ๋ฌธ์ ์ถ๊ฐ
Args:
documents: ์ถ๊ฐํ ๋ฌธ์ ๋ฆฌ์คํธ
"""
if self.vector_store is None:
self.create_or_load(documents)
else:
if self.use_milvus:
self.vector_store.add_documents(documents)
else:
self.vector_store.add_documents(documents)
def similarity_search(self, query: str, k: int = 5) -> List[Document]:
"""
๋ฒกํฐ ์ ์ฌ๋ ๊ฒ์ ์ํ
Args:
query: ๊ฒ์ ์ฟผ๋ฆฌ
k: ๋ฐํํ ๊ฒฐ๊ณผ ์
Returns:
์ ์ฌ๋๊ฐ ๋์ ๋ฌธ์ ๋ฆฌ์คํธ
"""
if self.vector_store is None:
raise ValueError("๋ฒกํฐ ์คํ ์ด๊ฐ ์ด๊ธฐํ๋์ง ์์์ต๋๋ค.")
print(f"๊ฒ์ ์ฟผ๋ฆฌ: '{query}', ์์ {k}๊ฐ ๊ฒฐ๊ณผ ์์ฒญ")
results = self.vector_store.similarity_search(query, k=k)
print(f"๊ฒ์ ์๋ฃ: {len(results)}๊ฐ ๊ฒฐ๊ณผ ์ฐพ์")
return results
def save_local(self, path: str = "faiss_index") -> None:
"""
FAISS ์ธ๋ฑ์ค ๋ก์ปฌ ์ ์ฅ (Milvus ์ฌ์ฉ ์ ํ ๊ฒฝ์ฐ)
Args:
path: ์ ์ฅ ๊ฒฝ๋ก
"""
if not self.use_milvus and self.vector_store is not None:
self.vector_store.save_local(path)
print(f"FAISS ์ธ๋ฑ์ค ๋ก์ปฌ ์ ์ฅ ์๋ฃ: {path}")
"""
FAISS ์ญ์ง๋ ฌํ ํ์ฉ ์ค์ ์ด, ํฌํจ๋ ๋ฒกํฐ ์คํ ์ด ์ฝ๋
"""
# vector_store.py ํ์ผ์์ load_local ๋ฉ์๋ ์์
def load_local(self, path: str = "faiss_index") -> None:
"""
FAISS ์ธ๋ฑ์ค ๋ก์ปฌ ๋ก๋ (Milvus ์ฌ์ฉ ์ ํ ๊ฒฝ์ฐ)
Args:
path: ๋ก๋ํ ์ธ๋ฑ์ค ๊ฒฝ๋ก
"""
if not self.use_milvus:
try:
print(f"FAISS ์ธ๋ฑ์ค ๋ก๋ ์ค: {path}")
# ์ญ์ง๋ ฌํ ํ์ฉ ์ต์
์ถ๊ฐ (๋ณด์ ๊ฒฝ๊ณ ํ์ธ ํ์)
self.vector_store = FAISS.load_local(
path,
self.embeddings,
allow_dangerous_deserialization=True # ์ญ์ง๋ ฌํ ํ์ฉ
)
print(f"FAISS ์ธ๋ฑ์ค ๋ก๋ ์๋ฃ: {path}")
except Exception as e:
print(f"FAISS ์ธ๋ฑ์ค ๋ก๋ ์คํจ: {e}")
# ์ค๋ฅ ์ธ๋ถ ์ ๋ณด ์ถ๋ ฅ
import traceback
traceback.print_exc()
# ์ ์ธ๋ฑ์ค ์ด๊ธฐํ
self.vector_store = self.init_faiss()
print("์ FAISS ์ธ๋ฑ์ค ์ด๊ธฐํ๋จ") |