자료를 임베딩한 바로 그 인코더로 질문도 임베딩한다. 다른 자로 잰 길이를 비교할 수 없듯, 같은 모델을 써야 거리가 의미를 갖는다.The question goes through the very encoder that embedded the corpus. You cannot compare lengths measured with different rulers; only the same model makes the distance mean anything.
qv = embeddings.embed_query(query) # 청크와 같은 인코더same encoder as the chunks
같은 좌표계 위에 놓인다. 그래야 사이의 거리를 잴 수 있다.It lands in the same coordinate system — which is what makes distance measurable.
질문에는 Goal도 쪽수도 없다. 전거는 찾아낸 쪽에만 있다.A question has no Goal, no page range. Provenance belongs to what is found.
수직선 위의 두 점 사이 거리는 빼면 끝이다. 3과 7의 거리는 4. 점을 끌어서 움직여 보라.On a number line, the distance between two points is just a subtraction. From 3 to 7 is 4. Drag the dots.
가로로 3, 세로로 4 떨어져 있으면 직선 거리는 5다. 중학교에서 배운 그것이다. 가로차와 세로차를 각각 제곱해 더하고, 제곱근을 취한다. 두 점을 끌어 움직여 보라.Three across and four up gives five as the crow flies — the theorem everyone met at school. Square each difference, add them, take the square root. Drag the two points.
그림은 못 그린다. 하지만 계산은 조금도 달라지지 않는다. 항이 2개에서 384개로 늘어날 뿐이다.You cannot draw it. But the calculation does not change at all — there are simply 384 terms instead of two.
실제로 해 보자. 질문 벡터와 1등으로 뽑힌 청크 G01-S01의 거리를 한 항씩 계산한다.Let us actually do it — the distance between the query and G01-S01, the nearest chunk, computed one term at a time.
차이는 음수일 수 있다. −0.14와 +0.14는 벌어진 정도가 같다. 제곱하면 둘 다 0.0196이 되어 부호가 사라진다. 덤으로 큰 차이가 더 크게 반영된다 — 조금 다른 것보다 많이 다른 것에 민감해진다.A difference can be negative, yet −0.14 and +0.14 are equally far apart. Squaring makes both 0.0196 and the sign disappears. As a bonus, large gaps count for more — the measure grows sensitive to the dimensions where the two really differ.
제곱해서 더한 값은 넓이 같은 단위가 되어 버린다. 제곱근을 취해야 다시 길이가 된다. 넓이 5인 정사각형의 한 변이 √5인 것과 같다.Once squared and summed, the number is in something like units of area. The square root brings it back to a length — just as a square of area 5 has sides of √5.
이 방식을 L2 거리 또는 유클리드 거리라 부른다. 자로 재는 그 거리, 우리가 평소에 “거리”라고 부르는 바로 그것이다. 이름이 어려워 보일 뿐 새로운 개념이 아니다.This is called L2 distance, or Euclidean distance — the distance you get with a ruler, the one everybody already means by “distance”. The name is forbidding; the idea is not new.
방금 한 계산을 176번 반복하면 검색이 끝난다. 가까운 순으로 정렬해 위에서 몇 개를 가져오면 그것이 검색 결과다.Repeat what we just did 176 times and the search is done. Sort by distance, take the top few — that is the result.
distances = [] for v in V: # 청크 176개176 chunks diff = v - Q # 384개의 차이384 differences dist = np.sqrt((diff ** 2).sum()) # 제곱 → 합 → 제곱근square → sum → root distances.append(dist) order = np.argsort(distances) # 가까운 순으로nearest first
STEP 6에서 만든 IndexFlatL2가 하는 계산이 방금 우리가 손으로 한 것과 완전히 같다. 이름의 L2가 바로 이 거리다. STEP 9에서 두 결과가 일치하는지 직접 확인한다.The IndexFlatL2 we built in STEP 6 performs exactly the calculation we just did by hand. The L2 in its name is this distance. In STEP 9 we check that the two agree.
sentence-transformers/all-MiniLM-L6-v2로 실제 임베딩 · 청크 벡터는 Source/sdg_embeddings.jsonl의 실제 값The query was embedded with the real sentence-transformers/all-MiniLM-L6-v2 · Chunk vectors are the actual values in Source/sdg_embeddings.jsonl