안녕하세요? 이번엔 Local LLM과 랭체인으로 간단한 한국어 단어장을 만들어 보려고 합니다. 단어를 선택하면 뜻, 예문을 볼 수 있고 Local LLM이 csv file에 있는 예문과 비슷한 예문들과 그 뜻을 생성해서 보여주는 단어장입니다.
1. Claude에게 한국어 단어장 만들기에 필요한 code를 작성해 달라고 하기
1) Ollama 설치
2) 단어장에 사용될 csv file 만들기: korean_vocab.csv
3) VS Code에 아래의 파일을 복사해 저장합니다: app.py
import gradio as gr
import pandas as pd
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.schema.runnable import RunnablePassthrough, RunnableParallel
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load CSV data
try:
df = pd.read_csv('korean_vocab.csv')
word_list = df['word'].tolist()
except Exception as e:
logger.error(f"Error loading CSV file: {e}")
word_list = []
# Initialize Ollama LLM with a default model
try:
llm = Ollama(model="llama3")
except Exception as e:
logger.error(f"Error initializing Ollama: {e}")
llm = None
# Create vector store for RAG
def create_vector_store(df):
embeddings = HuggingFaceEmbeddings()
texts = df.apply(lambda row: f"Word: {row['word']}\nMeaning: {row['meaning']}\nExample: {row['example']}", axis=1).tolist()
return FAISS.from_texts(texts, embeddings)
vector_store = create_vector_store(df)
# Create a prompt template
prompt = PromptTemplate(
input_variables=["word", "meaning", "example", "context"],
template="""Word: {word}
Meaning: {meaning}
Example: {example}
Similar examples:
{context}
Please provide similar example sentences and their meanings based on the given word, its meaning, and the similar examples."""
)
def get_relevant_examples(word, meaning, k=3):
query = f"Word: {word}\nMeaning: {meaning}"
docs = vector_store.similarity_search(query, k=k)
return "\n".join(doc.page_content for doc in docs)
# Create the chain
chain = RunnableParallel(
{
"word": lambda x: x["word"],
"meaning": lambda x: x["meaning"],
"example": lambda x: x["example"],
"context": lambda x: get_relevant_examples(x["word"], x["meaning"])
}
) | prompt | llm
def get_flashcard(word):
try:
# Look up the word in the CSV
row = df[df['word'] == word]
if row.empty:
logger.warning(f"Word not found: {word}")
return "Word not found", "", ""
meaning = row['meaning'].values[0]
example = row['example'].values[0]
# Generate an explanation using the local LLM with RAG
explanation = chain.invoke({
"word": word,
"meaning": meaning,
"example": example
})
logger.info(f"Successfully generated flashcard for: {word}")
return meaning, example, explanation
except Exception as e:
logger.error(f"Error in get_flashcard: {e}")
return f"Error: {str(e)}", "", ""
# Define the Gradio interface
iface = gr.Interface(
fn=get_flashcard,
inputs=[
gr.Dropdown(choices=word_list, label="Select a word")
],
outputs=[
gr.Textbox(label="Meaning"),
gr.Textbox(label="Example"),
gr.Textbox(label="Similar Examples and Meanings")
],
title="Local LLM Flashcard System with RAG",
)
# Launch the Gradio app with a public link
iface.launch()
2. 애플리케이션 실행
1) VS Code에서 터미널을 열고 app.py 파일을 실행합니다.
python app.py
2) 웹 브라우저에서 http://127.0.0.1:7862 를 열어 한국어 단어장을 실행해 봅니다.
아래와 같이 결과가 나왔습니다. 😊
3. 앞으로 해야할 일
Cloud 기반 LLM을 사용하여 더 많은 기능을 추가한 단어장 만들어 보기
#11기랭체인