from sentence_transformers import SentenceTransformer
# Load the free, open-source embedding model.# The model downloads from Hugging Face on first run and saves locally.# trust_remote_code=True is required by this model:
model = SentenceTransformer("nomic-ai/nomic-embed-text-v1", trust_remote_code=True)defget_embedding(text, precision="float32"):# Use this function when embedding text you plan to store in MongoDB:return model.encode(text, precision=precision).tolist()defget_query_embedding(text, precision="float32"):# Use this function when embedding a user's search query:return model.encode(text, precision=precision).tolist()์ ๊ณต
from embedding_utils import get_embedding
from pymongo import MongoClient
# Replace the placeholder with your Atlas connection string:
mongodb_client = MongoClient("mongodb+srv://<USERNAME>:<PASSWORD>@<HOST>/",
appname="devrel-tutorial-python-semantic-search")
collection = mongodb_client["sample_db"]["documents"]# Sample data:
sample_documents =[{"title":"MongoDB Atlas","text":"MongoDB Atlas is a fully managed cloud database."},{"title":"Vector Search","text":"Vector search finds results based on semantic meaning."},{"title":"Nomic AI","text":"nomic-embed-text-v1 is a free, open-source embedding model."},]
docs_to_insert =[]for doc in sample_documents:
embedding = get_embedding(doc["text"])
docs_to_insert.append({"title": doc["title"],"text": doc["text"],# The vector lives alongside your original data in the same document:"embedding": embedding
})# Drop the collection before each run to avoid inserting duplicate documents:
collection.drop()
result = collection.insert_many(docs_to_insert)print(f"Inserted {len(result.inserted_ids)} documents with embeddings.")
mongodb_client.close()์ ๊ณต
from pymongo.mongo_client import MongoClient
from pymongo.operations import SearchIndexModel
import time
# Replace the placeholder with your Atlas connection string:
mongodb_client = MongoClient("mongodb+srv://<USERNAME>:<PASSWORD>@<HOST>/",
appname="devrel-tutorial-python-semantic-search")# Point to the same database and collection you used in the previous step:
database = mongodb_client["sample_db"]
collection = database["documents"]# Define the vector search index.# The three required fields tell MongoDB what to index and how to compare vectors:
search_index_model = SearchIndexModel(
definition={"fields":[{"type":"vector","path":"embedding",# The field name from generate_embeddings.py"numDimensions":768,# nomic-embed-text-v1 always outputs 768 dimensions"similarity":"cosine"# Recommended similarity function for this model}]},
name="vector_index",type="vectorSearch")
result = collection.create_search_index(model=search_index_model)print("New search index named "+ result +" is building.")# Poll every five seconds until the index is ready to accept queries:print("Polling to check if the index is ready. This may take up to a minute.")
predicate =lambda index: index.get("queryable")isTruewhileTrue:
indices =list(collection.list_search_indexes(result))iflen(indices)and predicate(indices[0]):break
time.sleep(5)print(result +" is ready for querying.")
mongodb_client.close()์ ๊ณต
New search index named vector_index is building.
Polling to check if the index is ready. This may take up to a minute.
vector_index is ready for querying.์ ๊ณต
from embedding_utils import get_query_embedding
user_query ="I need an automated, scalable system for serious information storage"
query_vector = get_query_embedding(user_query)print(f"Query: '{user_query}'")print(f"Vector dimensions: {len(query_vector)}")print(f"First 5 values: {query_vector[:5]}")์ ๊ณต
<All keys matched successfully>
Query: 'I need an automated, scalable system for serious information storage'
Vector dimensions: 768
First 5 values: [0.0025914530269801617, 0.09862980246543884, -0.023092379793524742, -0.0171672236174345, -0.05548065900802612]์ ๊ณต
from embedding_utils import get_query_embedding
from pymongo import MongoClient
# Replace the placeholder with your Atlas connection string:
mongodb_client = MongoClient("mongodb+srv://<USERNAME>:<PASSWORD>@<HOST>/",
appname="devrel-tutorial-python-semantic-search")
collection = mongodb_client["sample_db"]["documents"]# Generate a query vector from the user's search input:
user_query ="I need an automated, scalable system for serious information storage"
query_vector = get_query_embedding(user_query)# Define the $vectorSearch aggregation pipeline:
pipeline =[{"$vectorSearch":{"index":"vector_index",# The index created in create_vector_index.py"path":"embedding",# The field that holds your stored vectors"queryVector": query_vector,# The vector generated from the user's query"numCandidates":150,# How many neighbors MongoDB considers"limit":3# How many results to return}},{"$project":{"_id":0,"title":1,"text":1,"score":{"$meta":"vectorSearchScore"# Relevance score for each result}}}]
results = collection.aggregate(pipeline)print(f"\nTop results for query: '{user_query}'\n")for doc in results:print(f"Title: {doc['title']}")print(f"Text: {doc['text']}")print(f"Score: {doc['score']:.4f}")print()
mongodb_client.close()์ ๊ณต
<All keys matched successfully>
Top results for query: 'I need an automated, scalable system for serious information storage.'
Title: MongoDB Atlas
Text: MongoDB Atlas is a fully managed cloud database.
Score: 0.7211
Title: Nomic AI
Text: nomic-embed-text-v1 is a free, open-source embedding model.
Score: 0.6818
Title: Vector Search
Text: Vector search finds results based on semantic meaning.
Score: 0.6642์ ๊ณต
์ฟผ๋ฆฌ "I need an automated, scalable system for serious information storage"์ "MongoDB Atlas is a fully managed cloud database."๋ ๋จ์ด๋ฅผ ๊ณต์ ํ์ง ์์ง๋ง, "MongoDB Atlas"๊ฐ ๊ฐ์ฅ ๋์ ์ ์๋ฅผ ๋ฐ์ต๋๋ค. ๋ ๋ฌธ์ฅ์ ๋ฒกํฐ๊ฐ ์๋ฏธ์ ๊ฐ๊น๊ธฐ ๋๋ฌธ์ ๋๋ค. "...์ ๋ณด ์ ์ฅ์ ์ํ ์๋ํ๋๊ณ ํ์ฅ ๊ฐ๋ฅํ ์์คํ "์ ์๋ฏธ์ ์ผ๋ก "...์์ ๊ด๋ฆฌํ ํด๋ผ์ฐ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค"์ ๋งคํ๋ฉ๋๋ค. ๋์ผํ ์ฟผ๋ฆฌ๋ก ํ ์คํธ ๊ฒ์์ ํ๋ฉด ์ด๋ค ๋ฌธ์์๋ ์ ํํ ์ฟผ๋ฆฌ ๋จ์ด๊ฐ ์์ผ๋ฏ๋ก ๊ฒฐ๊ณผ๊ฐ 0๊ฑด์ด ๋ฉ๋๋ค. ์ด๊ฒ์ด ์๋ํ ๋๋ก ๋์ํ๋ ์๋งจํฑ ๊ฒ์์ ๋๋ค.
์ด๋ฐ ์๊ฐ์๋ ์ผ๋ฐ์ ์ธ ํ ์คํธ ๊ฒ์์ด ํ๊ณ๋ฅผ ๋๋ฌ๋ ๋๋ค. ์ค์ ๋ก๋ ๋ค์๊ณผ ๊ฐ์ ์ํฉ์ด ๋ฐ์ํฉ๋๋ค.
์๋งจํฑ ๊ฒ์์ ์ด๋ฅผ ํด๊ฒฐํฉ๋๋ค. ์ ํํ ๋จ์ด ์ผ์น๊ฐ ์๋๋ผ, ์ฟผ๋ฆฌ์ ์๋ฏธ์ ์๋๋ฅผ ์ดํดํฉ๋๋ค.
์ด ํํ ๋ฆฌ์ผ์์๋ Python๊ณผ ๋ฌด๋ฃ nomic-embed-text-v1 ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ์ฌ์ฉํด MongoDB์์ ์๋งจํฑ ๊ฒ์์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ๋๋ฆฝ๋๋ค.
์ฐธ๊ณ :
nomic-embed-text-v1์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ๋ฉด ์ฝ 0.27GB์ด๋ฏ๋ก, ์ด ํํ ๋ฆฌ์ผ์ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ธฐ ์ ์ ์ต์ 1GB ์ด์์ ์ฌ์ฉ ๊ฐ๋ฅํ RAM์ด ์๋์ง ํ์ธํ์ธ์. ์ฒ์ ์คํ ์ ๋ชจ๋ธ์ด Hugging Face์์ ๋ค์ด๋ก๋๋๋ฏ๋ก ์ธํฐ๋ท ์ฐ๊ฒฐ ์๋์ ๋ฐ๋ผ ์๊ฐ์ด ๋ ํ์ํ ์ ์์ต๋๋ค. ์ดํ ์คํ์์๋ ๋ก์ปฌ ์บ์์์ ๋ชจ๋ธ์ด ๋ก๋๋ฉ๋๋ค. CPU ์ถ๋ก ์ GPU ์ถ๋ก ๋ณด๋ค ํจ์ฌ ๋๋ฆฝ๋๋ค. ์ ์ฉ GPU๊ฐ ์๋ ๋จธ์ ์์๋ ๋ชจ๋ธ์ด CPU์์ ์คํ๋์ด VRAM ๋์ RAM์ ์ฌ์ฉํ๋ฏ๋ก ์๋ฒ ๋ฉ ์์ฑ์ ์๊ฐ์ด ๋ ๊ฑธ๋ฆด ์ ์์ต๋๋ค.์ด ํํ ๋ฆฌ์ผ์์ ๋ค์์ ํ์ตํฉ๋๋ค.
$vectorSearch์ง๊ณ ํ์ดํ๋ผ์ธ์ ์คํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํด์ํ๋ ๋ฐฉ๋ฒ์ด ํํ ๋ฆฌ์ผ์ ๋ชจ๋ ์ฝ๋ ์ํ์ GitHub ์ ์ฅ์์์ ํ์ธํ ์ ์์ต๋๋ค.
ํ ์คํธ ๊ฒ์ vs ์๋งจํฑ ๊ฒ์
ํ ์คํธ ๊ฒ์์ ์ฟผ๋ฆฌ์ ํฌํจ๋ ์ ํํ ๋จ์ด๋ฅผ ํฌํจํ๋ ๋ฌธ์๋ฅผ ๋งค์นญํฉ๋๋ค. ์๋งจํฑ ๊ฒ์์ ์ ํ ๋ค๋ฅธ ๋จ์ด๋ฅผ ์ฌ์ฉํ๋๋ผ๋ ์ฟผ๋ฆฌ์ ๊ฐ์ ์๋ฏธ๋ฅผ ๋ด์ ๋ฌธ์๋ฅผ ๋งค์นญํฉ๋๋ค.
๋ค์ ํ๋ "heart problems"๋ผ๋ ๊ฒ์ ์ฟผ๋ฆฌ์ ๋ํด ๊ฐ ์ ๊ทผ ๋ฐฉ์์ด ๋ฐํํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
์๋งจํฑ ๊ฒ์ ํต์ฌ ๊ฐ๋
MongoDB์ ์๋งจํฑ ๊ฒ์์๋ ์ธ ๊ฐ์ง ๊ฐ๋ ์ด ํต์ฌ์ ๋๋ค. ์ด๋ฅผ ์ดํดํ๋ฉด ์ด ํํ ๋ฆฌ์ผ์ ๊ฐ ๋จ๊ณ๊ฐ ์ ๊ทธ๋ ๊ฒ ๋์ํ๋์ง ํ์ ํ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
๋ฒกํฐ ์๋ฒ ๋ฉ
์๋ฒ ๋ฉ ๋ชจ๋ธ์ ํ ์คํธ๋ฅผ ๋ฒกํฐ๋ผ ๋ถ๋ฆฌ๋ ๊ณ ์ ๊ธธ์ด์ ์ซ์ ๋ชฉ๋ก์ผ๋ก ๋ณํํฉ๋๋ค. ๋ฒกํฐ์ ๊ฐ ์ซ์๋ ์๋ฏธ์ ํ ์ฐจ์์ ๋ํ๋ ๋๋ค.
์ ์ฌํ ์๋ฏธ์ ๋ ํ ์คํธ๋ ์์น์ ์ผ๋ก ์๋ก ๊ฐ๊น์ด ๋ฒกํฐ๋ฅผ ์์ฑํฉ๋๋ค. "cardiac conditions"์ "heart problems"๋ ๋จ์ด๋ฅผ ๊ณต์ ํ์ง ์๋๋ผ๋ ๋ฒกํฐ ๊ณต๊ฐ์์๋ ์๋ก ๊ฐ๊น๊ฒ ์์นํฉ๋๋ค. ์ด๋ฌํ ๊ทผ์ ์ฑ์ด ์๋งจํฑ ๊ฒ์์ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค.
์ด ํํ ๋ฆฌ์ผ์ ๋ก์ปฌ ๋จธ์ ์์ ์์ ํ ์คํ๋๋ ๋ฌด๋ฃ ์คํ ์์ค ์๋ฒ ๋ฉ ๋ชจ๋ธ
nomic-embed-text-v1์ ์ฌ์ฉํฉ๋๋ค. ์คํฌ๋ฆฝํธ๋ฅผ ์ฒ์ ์คํํ๋ฉด ๋ชจ๋ธ์ด Hugging Face์์ ์๋์ผ๋ก ๋ค์ด๋ก๋๋์ด ์ดํ ์คํ์ ์ํด ๋ก์ปฌ์ ์ ์ฅ๋ฉ๋๋ค. ์ด ๋ชจ๋ธ์ ์ ๋ ฅ๋น 768๊ฐ์ ์ซ์๋ฅผ ์์ฑํฉ๋๋ค.๋ฒกํฐ ๊ฒ์ ์ธ๋ฑ์ค
๋ฒกํฐ ๊ฒ์ ์ธ๋ฑ์ค๋ ์ด๋ค ํ๋์ ์๋ฒ ๋ฉ์ด ์ ์ฅ๋์ด ์๋์ง, ๋ช ์ฐจ์์ธ์ง, ๋น๊ต์ ์ด๋ค ์ ์ฌ๋ ํจ์๋ฅผ ์ฌ์ฉํ ์ง MongoDB์ ์๋ ค์ค๋๋ค. ์ด๋ค
$vectorSearch์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ธฐ ์ ์ ๋ฐ๋์ ์ด ์ธ๋ฑ์ค๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค.$vectorSearch์ง๊ณ ๋จ๊ณ$vectorSearch๋ ๊ฒ์์ ์ํํ๋ ์ง๊ณ ๋จ๊ณ์ ๋๋ค. ์ฟผ๋ฆฌ ๋ฒกํฐ๋ฅผ ๋ฐ์ ์ธ๋ฑ์ฑ๋ ํ๋๋ฅผ ๊ฒ์ํ๊ณ , ์๋งจํฑ ์ ์ฌ๋ ์์ผ๋ก ๋ฌธ์๋ฅผ ๋ฐํํฉ๋๋ค. ๋ค๋ฅธ ์ง๊ณ ํ์ดํ๋ผ์ธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก$project๋ฑ์ ๋จ๊ณ์ ํจ๊ป ์ฒด์ด๋ํฉ๋๋ค.๊ทธ๋ผ ์์ํด ๋ณด๊ฒ ์ต๋๋ค.
์ฌ์ ์ค๋น ์ฌํญ
์์ํ๊ธฐ ์ ์ ๋ค์์ ์ค๋นํ์ธ์.
pymongo4.7 ์ด์,sentence-transformers,einopsPython ํจํค์ง ์ค์นํ๋ก์ ํธ ์ค์
ํ๋ก์ ํธ์ฉ ํด๋๋ฅผ ๋ง๋ค๊ณ ํฐ๋ฏธ๋์์ ํด๋น ํด๋๋ก ์ด๋ํ์ธ์.
ํ๋ก์ ํธ ์ข ์์ฑ์ ๊ฒฉ๋ฆฌํ๊ธฐ ์ํด ๊ฐ์ ํ๊ฒฝ์ ์์ฑํ๊ณ ํ์ฑํํ์ธ์.
Windows์์๋ ๋ค์์ผ๋ก ๊ฐ์ ํ๊ฒฝ์ ํ์ฑํํฉ๋๋ค.
์ด์ ํ์ํ ํจํค์ง๋ฅผ ์ค์นํ์ธ์.
์ด ํํ ๋ฆฌ์ผ์ ๊ฐ ๋จ๊ณ๋ง๋ค ํ๋์ Python ํ์ผ์ ๋ง๋ญ๋๋ค. ๋ชจ๋ ํ์ผ์
mongodb-semantic-searchํด๋์ ์ ์ฅํฉ๋๋ค.์๋ฒ ๋ฉ ์ ํธ๋ฆฌํฐ ํ์ผ ๋ง๋ค๊ธฐ
embedding_utils.py๋ผ๋ ํ์ผ์ ๋ง๋์ธ์. ์ด ํ์ผ์๋ ํํ ๋ฆฌ์ผ์์ ์ฌ์ฉํ๋ ๋ ๊ฐ์ง ์๋ฒ ๋ฉ ํจ์๊ฐ ํฌํจ๋ฉ๋๋ค.๊ฒฝ๊ณ :
trust_remote_code=True๋ Hugging Face์์ ๋ค์ด๋ก๋๋ ๋ชจ๋ธ๋ณ Python ์ฝ๋๋ฅผ ๋ก์ปฌ ๋จธ์ ์์ ์คํํ ์ ์๋๋ก ํ์ฉํฉ๋๋ค. Hugging Face์ ๋ชจ๋ธ ์์ค๊ฐ ์์๋๊ฑฐ๋ ์ ์์ ์ธ ๋ณ๊ฒฝ์ผ๋ก ์ ๋ฐ์ดํธ๋๋ฉด ํด๋น ์ฝ๋๊ฐ ํ๊ฒฝ์์ ์๋์ผ๋ก ์คํ๋ฉ๋๋ค. ํ๋ก๋์ ํ๊ฒฝ์์ ์ด ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ๋๋ ์ฃผ์๊ฐ ํ์ํฉ๋๋ค.๋ฒกํฐ ์๋ฒ ๋ฉ ์์ฑ ๋ฐ ์ ์ฅ
generate_embeddings.py๋ผ๋ ํ์ผ์ ๋ง๋์ธ์. ์ด ํ์ผ์embedding_utils.py์์ ์๋ฒ ๋ฉ ํจ์๋ฅผ ๊ฐ์ ธ์ ๊ฐ ๋ฌธ์์ text ํ๋์ ๋ํ ๋ฒกํฐ๋ฅผ ์์ฑํ๊ณ , ์๋ฒ ๋ฉ์ ํฌํจํ ์ ์ฒด ๋ฌธ์๋ฅผ MongoDB์ ์ ์ฅํฉ๋๋ค.์ ์ฝ๋์์
collection.drop()์ ๋งค ์คํ ์ ์ ์ปฌ๋ ์ ์ ๋ชจ๋ ๋ฌธ์์ ์ธ๋ฑ์ค๋ฅผ ์ญ์ ํฉ๋๋ค.generate_embeddings.py์์ ์ด ์ค ์์ด ๋ค์ ์คํํ๋ฉด ๋ฌธ์๊ฐ ์ค๋ณต ์ฝ์ ๋์ด$vectorSearch๊ฐ ๋์ผํ ๋ฌธ์๋ฅผ ์ฌ๋ฌ ๋ฒ ๋ฐํํ๊ฒ ๋ฉ๋๋ค. ์ปฌ๋ ์ ์ ๋๋กญํ๋ฉด ๋ฒกํฐ ๊ฒ์ ์ธ๋ฑ์ค๋ ์ญ์ ๋๋ฏ๋ก,generate_embeddings.py๋ฅผ ๋ค์ ์คํํ ๋๋ง๋คcreate_vector_index.py๋ฅผ ์ฌ์คํํด์ผ ํฉ๋๋ค.์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ธ์.
ํฐ๋ฏธ๋์ ๋ค์๊ณผ ๊ฐ์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
์ด์ MongoDB์ ๊ฐ ๋ฌธ์์๋ ์๋ณธ ํ ์คํธ์ 768์ฐจ์ ๋ฒกํฐ๊ฐ
embeddingํ๋์ ํจ๊ป ์ ์ฅ๋์ด ์์ต๋๋ค. ๋ฒกํฐ๋ ๋์ผํ ๋ฌธ์ ๋ด ๋ฐ์ดํฐ์ ๋๋ํ ์กด์ฌํ๋ฏ๋ก, ์ฟผ๋ฆฌ ์ ๋ณ๋์ ์ ์ฅ์๋ ์กฐํ๊ฐ ํ์ํ์ง ์์ต๋๋ค.MongoDB Vector Search ์ธ๋ฑ์ค ๋ง๋ค๊ธฐ
create_vector_index.py๋ผ๋ ํ์ผ์ ๋ง๋์ธ์. ์ด ํ์ผ์embeddingํ๋์ ๋ฒกํฐ ๊ฒ์ ์ธ๋ฑ์ค๋ฅผ ์ ์ํ๊ณ ์์ฑํ์ฌ MongoDB๊ฐ ์ปฌ๋ ์ ์ ๋ํด$vectorSearch์ฟผ๋ฆฌ๋ฅผ ์คํํ ์ ์๋๋ก ํฉ๋๋ค.์ธ๋ฑ์ค ์ ์์๋ ์ธ ๊ฐ์ง ํ๋๊ฐ ํ์ํฉ๋๋ค.
path: ์๋ฒ ๋ฉ์ด ์ ์ฅ๋ ํ๋(์ด ํํ ๋ฆฌ์ผ์์๋embedding)numDimensions: ๋ชจ๋ธ์ ์ถ๋ ฅ ํฌ๊ธฐ์ ์ผ์นํด์ผ ํจ(nomic-embed-text-v1์768)similarity: ๋น๊ต ํจ์(์ด ๋ชจ๋ธ์๋cosine์ด ์ ํฉ)numDimensions๊ฐ์ ์๋ฒ ๋ฉ ๋ชจ๋ธ๊ณผ ์ ํํ ์ผ์นํด์ผ ํฉ๋๋ค. ๋ถ์ผ์น ์ ์ธ๋ฑ์ค ์์ฑ์ด ์คํจํฉ๋๋ค.์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ธ์.
ํฐ๋ฏธ๋์ ๋ค์๊ณผ ๊ฐ์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
์ธ๋ฑ์ค ๋น๋๋ ์ต๋ 1๋ถ ์ ๋ ๊ฑธ๋ฆฝ๋๋ค. ํด๋ง ๋ฃจํ๋ 5์ด๋ง๋ค ํ์ธํ๋ฉฐ, MongoDB๊ฐ ์ธ๋ฑ์ค๊ฐ ์ฟผ๋ฆฌ ๊ฐ๋ฅํ ์ํ์์ ํ์ธํ ๋์๋ง ์ข ๋ฃํฉ๋๋ค. "vector_index is ready for querying." ๋ฉ์์ง๋ฅผ ๋ณด๊ธฐ ์ ์๋ ๋ค์ ๋จ๊ณ๋ก ๋์ด๊ฐ์ง ๋ง์ธ์.
๊ฒ์ ์ฟผ๋ฆฌ๋ฅผ ๋ฒกํฐ๋ก ๋ณํ
generate_query_vector.py๋ผ๋ ํ์ผ์ ๋ง๋์ธ์. ์ด ํ์ผ์embedding_utils.py์get_query_embedding()ํจ์๋ฅผ ๊ฐ์ ธ์ต๋๋ค.generate_query_vector.py๋ฅผ ์คํํ์ฌ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋ก๋๋๊ณ 768์ฐจ์ ๋ฒกํฐ๋ฅผ ์์ฑํ๋์ง ํ์ธํ์ธ์.get_query_embedding()ํจ์๋ ๋ค์ ๋จ๊ณ์์ ์ํฌํธํฉ๋๋ค.์ฌ๊ธฐ์์๋ ์ฒซ ๋จ๊ณ์ ๋์ผํ ๋ชจ๋ธ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ๋ค๋ฅธ ๋ชจ๋ธ์ ๋ค๋ฅธ ์์น ๊ณต๊ฐ์ ๋ฒกํฐ๋ฅผ ์์ฑํ์ฌ ๋น๊ต๊ฐ ๋ฌด์๋ฏธํด์ง๋๋ค.
์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ธ์.
๋ค์๊ณผ ์ ์ฌํ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
์ ์ถ๋ ฅ์์
Vector dimensions: 768์ ๋ชจ๋ธ ์ถ๋ ฅ์ด ์ ์ฅ๋ ์๋ฒ ๋ฉ๊ณผ ์ผ์นํจ์ ํ์ธํด ์ค๋๋ค.$vectorSearch์ฟผ๋ฆฌ ์คํrun_vector_search.py๋ผ๋ ํ์ผ์ ๋ง๋์ธ์. ์ด ํ์ผ์embedding_utils.py์get_query_embedding()ํจ์๋ฅผ ์ํฌํธํ์ฌ ์ฌ์ฉ์์ ๊ฒ์์ด๋ฅผ ๋ฒกํฐ๋ก ๋ณํํฉ๋๋ค.run_vector_search.py๋$vectorSearch์ง๊ณ ํ์ดํ๋ผ์ธ์ ์คํํ๊ณ , ์๋งจํฑ ์ ์ฌ๋ ์์ผ๋ก ์ ๋ ฌ๋ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.์ ์ฝ๋์์ ๋ ๋งค๊ฐ๋ณ์๊ฐ ๊ฒ์ ๋์์ ์ ์ดํฉ๋๋ค.
numCandidates๋ ์ต์ข ๊ฒฐ๊ณผ๋ก ์ขํ๊ธฐ ์ ์ MongoDB๊ฐ ์ด๊ธฐ ๊ฒ์ ํ์์ ๊ฒ์ฌํ๋ ๊ฐ์์ ๋๋ค. ๊ฐ์ ํฌ๊ฒ ํ๋ฉด ์ฌํ์จ์ด ๊ฐ์ ๋์ง๋ง ์ฝ๊ฐ ๋ ์ค๋ ๊ฑธ๋ฆฝ๋๋ค.limit์ ๋ฐํํ ๊ฒฐ๊ณผ ์๋ฅผ ์ค์ ํฉ๋๋ค. ์ด ํํ ๋ฆฌ์ผ์์๋limit์ 3์ผ๋ก, numCandidates๋ฅผ 150์ผ๋ก ์ค์ ํฉ๋๋ค. ์ผ๋ฐ์ ์ธ ์์์ ์numCandidates๋ฅผ limit์ 10~15๋ฐฐ๋ก ๋๋ ๊ฒ์ ๋๋ค.์ด์ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ธ์.
๋ค์๊ณผ ์ ์ฌํ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
์ฟผ๋ฆฌ "I need an automated, scalable system for serious information storage"์ "MongoDB Atlas is a fully managed cloud database."๋ ๋จ์ด๋ฅผ ๊ณต์ ํ์ง ์์ง๋ง, "MongoDB Atlas"๊ฐ ๊ฐ์ฅ ๋์ ์ ์๋ฅผ ๋ฐ์ต๋๋ค. ๋ ๋ฌธ์ฅ์ ๋ฒกํฐ๊ฐ ์๋ฏธ์ ๊ฐ๊น๊ธฐ ๋๋ฌธ์ ๋๋ค. "...์ ๋ณด ์ ์ฅ์ ์ํ ์๋ํ๋๊ณ ํ์ฅ ๊ฐ๋ฅํ ์์คํ "์ ์๋ฏธ์ ์ผ๋ก "...์์ ๊ด๋ฆฌํ ํด๋ผ์ฐ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค"์ ๋งคํ๋ฉ๋๋ค. ๋์ผํ ์ฟผ๋ฆฌ๋ก ํ ์คํธ ๊ฒ์์ ํ๋ฉด ์ด๋ค ๋ฌธ์์๋ ์ ํํ ์ฟผ๋ฆฌ ๋จ์ด๊ฐ ์์ผ๋ฏ๋ก ๊ฒฐ๊ณผ๊ฐ 0๊ฑด์ด ๋ฉ๋๋ค. ์ด๊ฒ์ด ์๋ํ ๋๋ก ๋์ํ๋ ์๋งจํฑ ๊ฒ์์ ๋๋ค.
ํต์ฌ ์ ๋ฆฌ
numDimensions๋ ์ธ๋ฑ์ค ์ ์์์ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ์ถ๋ ฅ ํฌ๊ธฐ์ ์ ํํ ์ผ์นํด์ผ ํฉ๋๋ค.nomic-embed-text-v1์ ํญ์ 768์ฐจ์์ ์์ฑํฉ๋๋ค.input_type="document"๋ ์ ์ฅ์ ์ต์ ํ๋ ์๋ฒ ๋ฉ์,input_type="query"๋ ๊ฒ์์ ์ต์ ํ๋ ์๋ฒ ๋ฉ์ ์์ฑํฉ๋๋ค. ๊ฐ ๋จ๊ณ์์ ์ฌ๋ฐ๋ฅธ ํ์ ์ ์ฌ์ฉํ์ธ์.numCandidates๋ MongoDB๊ฐ ์ต์ขlimit๊ฒฐ๊ณผ๋ก ์ขํ๊ธฐ ์ ์ ํ์ํ๋ ๋ฒ์๋ฅผ ์ ์ดํฉ๋๋ค. ๊ฐ์ ํฌ๊ฒ ํ๋ฉด ์ฌํ์จ์ด ํฅ์๋์ง๋ง ์ฟผ๋ฆฌ ์๊ฐ์ด ๋์ด๋ฉ๋๋ค.vectorSearchScore๋ ์๋งจํฑ ์ ์ฌ๋๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ญํนํฉ๋๋ค. ๊ฒฐ๊ณผ์๋ ์ฟผ๋ฆฌ์ ์ ํํ ๋จ์ด๊ฐ ํฌํจ๋ ํ์๊ฐ ์์ต๋๋ค. ์ ์ ๋ฒ์๋ ๋ชจ๋ธ๊ณผ ๋ฐ์ดํฐ์ ์ ๋ฐ๋ผ ๋ค๋ฅด์ง๋ง,nomic-embed-text-v1์ ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ธฐ์ค์ผ๋ก ๋ค์ ๊ฒฝ๊ณ๊ฐ์ด ์ ์ฉํ ์์์ ์ ๋๋ค.์ด ํํ ๋ฆฌ์ผ์ ๋ชจ๋ ์ฝ๋ ์ํ์ GitHub ์ ์ฅ์์์ ํ์ธํ ์ ์์ต๋๋ค.
์ถ๊ฐ๋ก ์ฝ๊ธฐ