⸻
P(다음 토큰 | 이전 토큰)
——
-> 이를 통해 “어떤 단어를 더 참고할지” 결정함
import numpy as np
# Query, Key, Value (예시)
Q = np.array([[1, 0, 1]])
K = np.array([[1, 1, 0],
[0, 1, 1],
[1, 0, 1]])
V = np.array([[1, 0],
[0, 1],
[1, 1]])
# Attention score
scores = Q @ K.T
# Softmax
weights = np.exp(scores) / np.sum(np.exp(scores))
# 결과
output = weights @ V
print(output)
어떤 단어가 더 중요한지 가중치로 반영하는 과정
——
토큰 수 = 비용
토큰 분해 방식에 따라 결과 달라짐
-> 특히 한국어는 비효율적인 경우 많음
—-
Input:
A: 1 + 1 = 2
A: 2 + 2 = 4
A: 3 + 3 = ?
프롬프트 = 임시 학습 데이터 역할
——
import openai
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "송금 실패 이유 설명"}],
temperature=0.2
)
temperature 낮음 → 안정적, 반복적
temperature 높음 → 다양성 증가
——
from sklearn.metrics.pairwise import cosine_similarity
vec1 = [0.1, 0.2, 0.3]
vec2 = [0.1, 0.25, 0.35]
similarity = cosine_similarity([vec1], [vec2])
print(similarity)
⸻
def rag_pipeline(query):
docs = vector_db.search(query)
prompt = f"문서: {docs}\n질문: {query}"
return llm(prompt)
⸻
⸻
⸻
1. 같은 입력 → 다른 결과 나올 수 있음
2. context 길이 제한 존재
3. 계산 비용 높음 (O(n²))
4. 지속적인 기억 불가능
완벽한 시스템이 아니라 “확률 기반 생성기”임
⸻
LLM은 다음 3가지로 이해하면 됨