Optimizing Llama 3 RAG Search Performance: Maximizing Korean Query and Context Understanding
This guide introduces methods to maximize the search performance of Korean Retrieval-Augmented Generation (RAG) systems using Llama 3. It presents specific technical approaches and optimization strategies for enhancing Korean query understanding and extracting relevant context, thereby significantly improving the accuracy of RAG system responses and user satisfaction.
1. The Challenge / Context
Recently, RAG systems utilizing large language models (LLMs) like Llama 3 have garnered significant attention. However, challenges persist in accurately understanding the subtle nuances of Korean queries and effectively retrieving relevant documents. In particular, due to the agglutinative nature of Korean, there are extensive inflections and high context dependency, making it difficult for LLMs to grasp precise meanings. Furthermore, the mixture of specialized terminology, slang, and jargon from various fields is a major factor in reducing the search accuracy of RAG systems. The core objective of this guide is to address these issues and maximize the performance of Korean RAG systems.
2. Deep Dive: Korean Embeddings and Cosine Similarity
The core of a RAG system is to measure the relevance between a query and documents to retrieve the most relevant ones. In this process, embedding models represent text in a high-dimensional vector space, positioning semantically similar texts closer together in that space. Currently, various embedding models specialized for Korean exist, and each model shows performance differences depending on its training data and architecture. The similarity between query and document embeddings is primarily measured using cosine similarity. Cosine similarity calculates the cosine of the angle between two vectors, returning a value from -1 (completely opposite) to 1 (completely identical). RAG systems return documents with the highest cosine similarity values as search results.
3. Step-by-Step Guide / Implementation
This section introduces specific implementation steps to optimize the Korean search performance of the Llama 3 RAG system.
Step 1: Selecting and Applying a Korean Embedding Model
First, you need to select an embedding model specialized for Korean. You can consider Korean models of Sentence-BERT (e.g., a model fine-tuned with Korean data from paraphrase-multilingual-mpnet-base-v2) or models like KoELECTRA. These models can be easily loaded and used with the Hugging Face Transformers library.
from transformers import AutoTokenizer, AutoModel
import torch
model_name = "snunlp/KR-SBERT-V40K-klueNLI-augSTS" # 예시 모델, 실제로는 fine-tuning된 모델을 사용하는 것이 좋습니다.
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
def get_embedding(text):
encoded_input = tokenizer(text, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
return model_output.last_hidden_state[:, 0, :].squeeze() # CLS 토큰 임베딩 반환
query = "최신 인공지능 기술 동향"
document = "최근 딥러닝 기반 자연어 처리 모델의 성능이 크게 향상되었다."
query_embedding = get_embedding(query)
document_embedding = get_embedding(document)
# 코사인 유사도 계산 (PyTorch 사용)
def cosine_similarity(a, b):
a_norm = torch.nn.functional.normalize(a, p=2, dim=0)
b_norm = torch.nn.functional.normalize(b, p=2, dim=0)
return torch.dot(a_norm, b_norm)
similarity = cosine_similarity(query_embedding, document_embedding)
print(f"Cosine Similarity: {similarity.item()}")
Step 2: Data Preprocessing and Cleaning
Korean text data often contains noise, including various forms of typos, non-standard language, and special characters. Therefore, data preprocessing and cleaning must be performed before inputting it into the embedding model. This involves correcting spacing errors, removing unnecessary special characters, and cleaning data using regular expressions.
import re
from hanspell import spell_checker # 한국어 맞춤법 검사기
def preprocess_text(text):
# 1. HTML 태그 제거
text = re.sub('<[^>]*>', '', text)
# 2. 특수 문자 제거 (필요에 따라 더 많은 문자 추가)
text = re.sub(r'[^\w\s]', '', text)
# 3. 띄어쓰기 정규화 (연속된 공백을 하나의 공백으로 변경)
text = re.sub(r'\s+', ' ', text).strip()
# 4. 맞춤법 검사 (hanspell 사용) - 성능 고려하여 선택적으로 적용
try:
spelled_sent = spell_checker.check(text)
checked_text = spelled_sent.checked
except Exception as e:
print(f"맞춤법 검사 오류: {e}, 원본 텍스트 사용")
checked_text = text
return checked_text
example_text = "오늘 날씨가 너무 좋네요!! 하지만...미세먼지가 심해요ㅠㅠ"
preprocessed_text = preprocess_text(example_text)
print(f"Original Text: {example_text}")
print(f"Preprocessed Text: {preprocessed_text}")
Step 3: Building and Indexing a Vector Database
To store embedded documents and search them efficiently, a vector database is used. You can choose vector databases such as FAISS, Annoy, or Pinecone. Vector databases utilize indexing techniques to speed up similarity searches. Select an appropriate indexing method to optimize query response time.
import faiss
import numpy as np
# 임베딩 차원 설정
embedding_dimension = 768 # KR-SBERT-V40K-klueNLI-augSTS 모델의 임베딩 차원
# FAISS 인덱스 생성
index = faiss.IndexFlatL2(embedding_dimension) # L2 거리 기반 인덱스 (유클리드 거리)
# 예시 데이터 (실제로는 문서 임베딩을 사용)
num_vectors = 1000
embeddings = np.float32(np.random.rand(num_vectors, embedding_dimension))
# 인덱스에 벡터 추가
index.add(embeddings)
# 검색 쿼리 (실제로는 쿼리 임베딩을 사용)
query_vector = np.float32(np.random.rand(1, embedding_dimension))
# 검색 수행
k = 5 # 상위 5개 유사한 벡터 검색
distances, indices = index.search(query_vector, k)
print(f"Distances: {distances}")
print(f"Indices: {indices}")
Step 4: Query Expansion
Query expansion is a technique to clarify the user's search intent and improve search accuracy. For Korean, queries can be expanded using synonyms, related words, and associated terms. Query expansion can be implemented using a thesaurus (WordNet) or an LLM.
from nltk.corpus import wordnet as wn
import nltk
nltk.download('wordnet') # 최초 실행 시 다운로드 필요
nltk.download('omw-1.4')
def get_synonyms(word):
synonyms = []
for syn in wn.synsets(word, lang='kor'): # lang='kor' 추가
for lemma in syn.lemmas(lang='kor'): # lang='kor' 추가
synonyms.append(lemma.name())
return list(set(synonyms))
query = "사과"
synonyms = get_synonyms(query)
print(f"Query: {query}")
print(f"Synonyms: {synonyms}")
# LLM을 이용한 쿼리 확장 예시 (OpenAI API 사용, 비용 발생)
import openai
import os
openai.api_key = os.getenv("OPENAI_API_KEY") # 환경 변수에 API 키 설정 필요
def expand_query_with_llm(query):
prompt = f"다음 단어와 관련된 유사하거나 관련된 단어를 5개만 추천해주세요: {query}\n\n추천 단어:"
response = openai.Completion.create(
engine="text-davinci-003", # 또는 다른 모델
prompt=prompt,
max_tokens=50,
n=1,
stop=None,
temperature=0.7,
)
expanded_words = response.choices[0].text.strip().split(", ")
return expanded_words
# try:
# expanded_query = expand_query_with_llm(query)
# print(f"Expanded Query with LLM: {expanded_query}")
# except Exception as e:
# print(f"LLM 쿼리 확장 오류: {e}")
Step 5: Re-ranking
This method involves selecting the top N documents from the initial search results and then reordering them using a more sophisticated model. Cross-Encoder models take both the query and documents as input and directly predict relevance scores. Re-ranking can further improve the accuracy of search results.
from sentence_transformers import CrossEncoder
model_name = 'cross-encoder/ms-marco-MiniLM-L-6-v2' # 예시 모델, 한국어 fine-tuning된 모델이 있다면 더 좋습니다.
model = CrossEncoder(model_name)
query = "한국 드라마 추천"
documents = [
"최근 넷플릭스에서 인기 있는 한국 드라마는 '오징어 게임'입니다.",
"일본 애니메이션 '귀멸의 칼날'이 전 세계적으로 큰 인기를 얻고 있습니다.",
"한국 영화 '기생충'은 아카데미 시상식에서 작품상을 수상했습니다.",
"한국 드라마 '도깨비'는 판타지 로맨스 장르의 대표작입니다."
]
# Cross-Encoder 입력 형식: [(query, document1), (query, document2), ...]
pairs = [(query, doc) for doc in documents]
# 관련성 점수 예측
scores = model.predict(pairs)
# 점수를 기준으로 문서 정렬
ranked_documents = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)
print("Ranked Documents:")
for doc, score in ranked_documents:
print(f"{doc}: {score}")
4. Real-world Use Case / Example
A domestic startup applied the Llama 3 RAG system to its customer support chatbot, reducing customer inquiry response time by 50% and improving customer satisfaction by 20%. This startup fine-tuned the KR-SBERT model to build an embedding model optimized for its customer inquiry data and maximized search accuracy by applying query expansion and re-ranking techniques. Notably, including slang and jargon frequently appearing in customer inquiries in the training data was a key success factor for improving query understanding. Based on this experience, the startup is expanding its business by offering RAG system implementation consulting services.
5. Pros & Cons / Critical Analysis
- Pros:
- Enhanced Korean query understanding and utilization of contextual information
- Increased user satisfaction through improved search accuracy
- Applicability of various Korean embedding models and techniques
- Creation of business value through improved RAG system performance
- Cons:
- Requires a high understanding of Korean data preprocessing and cleaning
- Potential costs associated with embedding model fine-tuning and maintenance
- Requires additional development effort for applying query expansion and re-ranking techniques
- RAG system performance heavily depends on data quality
6. FAQ
- Q: Which Korean embedding model should I choose?
A: The optimal model varies depending on the use case and data. You can compare and evaluate various models such as KR-SBERT, KoELECTRA, and improve performance through fine-tuning if necessary. - Q: Is query expansion an essential step?
A: Query expansion helps improve search accuracy but is not an essential step. It is particularly useful when queries are highly ambiguous or when it's difficult to clearly understand the user's search intent. - Q: What should be considered when choosing a vector database?
A: You should consider data scale, query frequency, and latency requirements. FAISS offers fast search speeds but may not be suitable for large-scale data. Cloud-based vector databases like Pinecone offer excellent scalability but may incur costs.
7. Conclusion
Optimizing the Korean search performance of the Llama 3 RAG system is a crucial process that goes beyond mere technical improvement, leading to enhanced user experience and business value creation. We hope that through the methods presented in this guide, you can maximize Korean query and context understanding and fully leverage the potential of RAG systems. Apply the code now and experience the improved performance of your Korean RAG system!


