| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- retrieval augmented generation
- yum-me
- Factorization Machine
- fooddist
- DeepFM
- hin
- heterogeneous graph neural network
- mature-food
- graphrag
- gat
- Machine Learning
- food recommendation system
- ์ถ์ฒ์์คํ
- retrieval-augmented generation
- Neo4j
- heterogeneous graph
- nlp
- food recommender system
- GenAI
- heterogeneous graph attention network
- digital health
- Rag
- graph
- ์ถ์ฒ ์์คํ
- Today
- Total
๐ ์ํ์์ AI ๊ณต๋ถํ๊ธฐ
RAG(Retrieval-Augmented Generation): ์ ํํ ์ ๋ณด ์ ๊ณต์ ์ํ AI ๊ธฐ์ ๋ณธ๋ฌธ
RAG(Retrieval-Augmented Generation): ์ ํํ ์ ๋ณด ์ ๊ณต์ ์ํ AI ๊ธฐ์
FoodAI 2025. 4. 7. 23:00๐ก๋ค์ด๊ฐ๋ฉฐ
์ต๊ทผ AI ๊ธฐ์ ์ ๋ฐ์ ์ผ๋ก ChatGPT์ ๊ฐ์ ๋ํ ์ธ์ด ๋ชจ๋ธ(LLM)์ด ๋๋ผ์ด ์ ๋ณด ์์ฑ ๋ฅ๋ ฅ์ ๋ณด์ฌ์ฃผ๊ณ ์์ต๋๋ค. ํ์ง๋ง ์ด๋ฌํ ๋ชจ๋ธ๋ค์ ์ข ์ข ์๋ชป๋ ์ ๋ณด๋ฅผ ์์ฑํ๋ 'ํ๊ฐ(Hallucination)' ํ์์ ๋ณด์ด๊ธฐ๋ ํฉ๋๋ค. ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ฑ์ฅํ ๊ธฐ์ ์ด ๋ฐ๋ก RAG(Retrieval-Augmented Generation)์ ๋๋ค. RAG๋ ์ธ๋ถ ์ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ด๋ จ ์ ๋ณด๋ฅผ ๊ฒ์ํ์ฌ AI์ ์๋ต์ ๋ณด๊ฐํจ์ผ๋ก์จ ๋ ์ ํํ๊ณ ์ ๋ขฐํ ์ ์๋ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณตํฉ๋๋ค. ํนํ ๊ฑด๊ฐ๊ณผ ์์ ๋ถ์ผ์์๋ ์ ํํ ์ ๋ณด ์ ๊ณต์ด ๋งค์ฐ ์ค์ํ๊ธฐ ๋๋ฌธ์, RAG ๊ธฐ์ ์ ํ์ฉ ๊ฐ์น๊ฐ ๋งค์ฐ ๋์ต๋๋ค.
I. RAG์ ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ
์ผ๋ฐ ๋ชฉ์ ์ธ์ด ๋ชจ๋ธ์ ํ๊ณ
ํ๊ฐ(Hallucination) ๋ฌธ์
- BERT, BART, GPT, T5 ๊ฐ์ ์ผ๋ฐ ๋ชฉ์ ์ธ์ด ๋ชจ๋ธ๋ค์ ๋๊ท๋ชจ ๋ง๋ญ์น(corpus)์์ ํ์ตํ ์ง์์ ํ๋ผ๋ฏธํฐ์ ์ ์ฅํ์ง๋ง, ์ข ์ข ์ฌ์ค๊ณผ ๋ค๋ฅธ ์ ๋ณด๋ฅผ ์์ฑํฉ๋๋ค.
- ํน์ ์ ๋ฌด์ ํนํ๋ ๋ชจ๋ธ๋ณด๋ค ์ฑ๋ฅ์ด ๋ฎ์ ์ ์์ต๋๋ค.
- ๋ชจ๋ธ์ ์ง์ ๊ธฐ๋ฐ์ด ์ ํ์ ์ด๋ฉฐ, ํ์ต ๋ฐ์ดํฐ ์ดํ์ ์ ๋ณด๋ ์ ์ ์์ต๋๋ค.
์๋ ํ๊ท(Auto-Regression) LLM ๋ชจ๋ธ์ ํน์ฑ
- ์ด์ ๋จ์ด๋ฅผ ๋ณด๊ณ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๋ค์ ๋จ์ด๋ฅผ ์์ธกํ๋ ๋ฐฉ์์ผ๋ก ์๋ํฉ๋๋ค.
- ๋ฌธ๋งฅ์ ๋ฐ๋ผ ๋ค์ํ ๋ต๋ณ์ ์์ฑํ ์ ์์ต๋๋ค.
- ํ์ตํ ์ ๋ณด์ ํด๋น์ด ์๋๋ผ๋, ํ์ต ๋ฐฉ์์ ๊ธฐ์ค์ผ๋ก ๊ทธ๋ด๋ฏํ ๋ต๋ณ์ ์์ฑํ๊ธฐ๋ ํฉ๋๋ค.
๊ฒ์ ๋ชจ๋ธ(Non-parametric method)
- ๋ชจ๋ธ ์ธ๋ถ์์ ์ ๊ทผํ ์ ์๋ ์ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
- ์ง๋ฌธ์ ๋ํด ์ ์ ํ ๋ต๋ณํ๊ธฐ ์ํด Wikipedia์ ๊ฐ์ ์ธ๋ถ ์ง์์ ๊ฐ์ ธ์ ํ์ฉํฉ๋๋ค.
- ์ ๋ณด ๊ฒ์ ์์ ์์ ์ข์ ์ฑ๋ฅ์ ๋ณด์ ๋๋ค.

๋ ์ ๊ทผ ๋ฐฉ์์ ๋น๊ต:
| Parametric (ํ๋ผ๋ฏธํฐ ๊ธฐ๋ฐ) | Non-parametric (๋นํ๋ผ๋ฏธํฐ ๊ธฐ๋ฐ) |
| ๋๊ท๋ชจ ๋ง๋ญ์น๋ก ๋ง์ ์์ ์ง์ ํ์ต | ์ธ๋ถ ์ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๊ทผ ๊ฐ๋ฅ |
| ํ๋ผ๋ฏธํฐ ํ์ฅ, ์์ , ํด์์ด ์ด๋ ค์ | ํ๋ผ๋ฏธํฐ ํ์ฅ, ์์ , ํด์ ๊ฐ๋ฅ |
| BART, GPT-3์ฒ๋ผ ์์ฑ์ ํนํ๋จ | BM25, ๊ฒ์ ์์ง์ฒ๋ผ ๊ฒ์์ ํนํ๋จ |
II. RAG(Retrieval Augmented Generation)์ ๊ฐ์
RAG๋ ์ฌ์ฉ์ ์ง๋ฌธ์ ๊ด๋ จ๋ ์ ๋ณด๋ฅผ ์ธ๋ถ ๋ฌธ์์์ ๊ฒ์ํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ธ์ด ๋ชจ๋ธ์ ์ ๋ฌํ์ฌ ์์ฑํ ์๋ต์ ์ฌ์ฉ์์๊ฒ ์ ๊ณตํ๋ ๋ฐฉ์์ ๋๋ค.
RAG์ ์ฃผ์ ๊ตฌ์ฑ ์์
- Retriever: ํ๋ผ๋ฉํธ๋ฆญ ์ ๊ทผ์ ๋จ์ ์ธ ์ธ๋ถ ์ง์์ ์ฐธ์กฐํ์ฌ ์ฌ์ค์ ๊ฐ๊น์ด ์ ๋ณด๋ฅผ ๊ฒ์ํฉ๋๋ค.
- Generator: ๋นํ๋ผ๋ฉํธ๋ฆญ ์ ๊ทผ์ ์ฅ์ ์ธ ๋๊ท๋ชจ ๋ง๋ญ์น ํ์ต์ ํตํด ๋ค์ํ ํํ์ ๋ต๋ณ์ ์์ฑํฉ๋๋ค.
RAG์ ์๋ ๋ฐฉ์์ ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ต๋๋ค:

์ด ๊ณผ์ ์์ Retriever๋ ์ฌ์ฉ์ ์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ๋ฌธ์๋ฅผ ์ธ๋ถ ์ง์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐพ์๋ด๊ณ , Generator๋ ๊ฒ์๋ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ฐ์ค๋ฌ์ด ์๋ต์ ์์ฑํฉ๋๋ค.
III. RAG ๋ชจ๋ธ์ ๊ตฌ์กฐ
Retriever ๋ชจ๋ธ
Retriever ๋ชจ๋ธ์ ์ผ๋ฐ์ ์ผ๋ก Bi-Encoder ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค:

- ์ ๋ ฅ ์ํ์ค x์ ์ฌ๋ฌ ํ๋ณด ๋ฌธ์ y ์ฌ์ด์ ์ ์ฌ๋๋ฅผ ๊ณ์ฐํ์ฌ ๊ฐ์ฅ ๊ด๋ จ์ฑ ๋์ ๋ฌธ์๋ฅผ ์ฐพ์๋ ๋๋ค.
- ๋ ๊ฐ์ ๋ ๋ฆฝ์ ์ธ BERT ์ธ์ฝ๋๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
- ์ง๋ฌธ๊ณผ ๋ฌธ์๋ฅผ ๊ฐ๊ฐ ๋ณ๋์ BERT์ ์ ๋ ฅํ์ฌ ์๋ฒ ๋ฉ์ ์์ฑํฉ๋๋ค.
- ๋ ์๋ฒ ๋ฉ ๊ฐ์ ๋ด์ (dot product)์ ํตํด ์ ์ฌ๋ ์ ์๋ฅผ ๊ณ์ฐํฉ๋๋ค.
- ๊ฐ์ฅ ๋์ ์ ์๋ฅผ ๋ฐ์ ๋ฌธ์๊ฐ ์ ํ๋ฉ๋๋ค.

Generator ๋ชจ๋ธ
Generator๋ ๊ฒ์๋ ๋ฌธ์์ ์ง๋ฌธ์ ๊ฒฐํฉํ์ฌ ์ต์ข ์๋ต์ ์์ฑํฉ๋๋ค:
- BART๋ GPT-3์ ๊ฐ์ ์์ฑ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
- ์ฌ๋ฌ ๊ฒ์ ๊ฒฐ๊ณผ(Passage)๋ฅผ ์ฐธ์กฐํ์ฌ ํ๋ฅ ์ ์ผ๋ก ๊ฐ์ฅ ์ ์ ํ ์๋ต์ ์ ํํฉ๋๋ค.
- ๊ฒ์๋ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ฐ์ค๋ฌ์ด ๋ฌธ์ฅ์ ๊ตฌ์ฑํฉ๋๋ค.
IV. RAG ์ค์ ๊ตฌํ ์์
RAG๋ฅผ ์ค์ ๋ก ๊ตฌํํ๋ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
1. ๋ฌธ์ ์ค๋น ๋ฐ ์ฒ๋ฆฌ
์ ๋ PubMed์์ ํน์ ๋จ์ด๋ก ๊ฒ์๋ ๋ ผ๋ฌธ์ abstract๋ค์ 'abstracts'๋ผ๋ ํด๋์ txt ํ์ผ ํํ๋ก ์ ์ฅํด๋์ด, ์ด ํด๋์์ ๋ฌธ์๋ฅผ load ํ์์ต๋๋ค.
loader = DirectoryLoader('abstracts', glob="*.txt", loader_cls=TextLoader)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_documents(documents)
์ด ์ฝ๋๋ ๋ฌธ์๋ฅผ 1000์ ๋จ์๋ก ๋๋๊ณ , ์ค๋ณต์ ์ํด 200์์ฉ ๊ฒน์น๊ฒ ์ค์ ํฉ๋๋ค.
2. ์๋ฒ ๋ฉ ์์ฑ ๋ฐ ๋ฒกํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฅ
persist_directory = 'db'
embedding = OpenAIEmbeddings()
vectordb = Chroma.from_documents(
documents=texts,
embedding=embedding,
persist_directory=persist_directory)
vectordb.persist()
vectordb = None
vectordb = Chroma(
persist_directory=persist_directory,
embedding_function=embedding)
OpenAI์ ์๋ฒ ๋ฉ ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ๋ฌธ์๋ฅผ ๋ฒกํฐํํ๊ณ , ChromaDB์ ์ ์ฅํฉ๋๋ค.
3. Retrieval-Generator ๋ชจ๋ธ ๊ตฌ์ถ
qa_chain = RetrievalQA.from_chain_type(
llm=OpenAI(),
chain_type="stuff",
retriever=vectordb.as_retriever(),
return_source_documents=True)
def process_llm_response(llm_response):
result = llm_response['result']
source_info = []
for source in llm_response["source_documents"]:
pubmeid = source.metadata['source'].replace(".txt","").split("_")[0].replace('abstract','')
article_type = " ".join(source.metadata['source'].replace(".txt","").split("_")[1:])
source_info.append(f'Sources: {pubmeid}\nArticle Type: {article_type}')
return result, '\n\n'.join(source_info)
์ด ์ฝ๋๋ RetrievalQA๋ฅผ ์ค์ ํ๊ณ , ์๋ต๊ณผ ํจ๊ป ์ถ์ฒ ์ ๋ณด๋ฅผ ํจ๊ป ์ ๊ณตํ๋๋ก ํฉ๋๋ค.
์์ ๋ฐ ๊ฑด๊ฐ ๊ด๋ จ ์์ ์ฟผ๋ฆฌ
python query = "Recommend diet foods for management of hypertension in adults."
llm_response = qa_chain(query) process_llm_response(llm_response)
'Foods that may help manage hypertension in adults include fruits and vegetables, whole grains, low-fat dairy products, lean proteins, and nuts and seeds. Foods to avoid or limit include foods high in salt, saturated fat, and sugar.'
'Source PMID: 16003449 Source PMID: 19583632'
python query = "Is 25-hydroxyvitamin D good for obese men?"
llm_response = qa_chain(query) process_llm_response(llm_response)
'This study suggests that weight loss is associated with a marginally improved vitamin D status. Additional studies in unsupplemented individuals are needed to confirm these findings, but it may be beneficial for obese men to consider weight loss as a way to improve their vitamin D status.'
'Source PMID: 27604772'
IV. ๊ฒฐ๋ก ๐ฏ
RAG(Retrieval-Augmented Generation) ๊ธฐ์ ์ AI ์ธ์ด ๋ชจ๋ธ์ ํ๊ณ๋ฅผ ๋ณด์ํ๋ ํจ๊ณผ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค. ํนํ ๊ฑด๊ฐ๊ณผ ์์ ๋ถ์ผ์ ๊ฐ์ด ์ ํํ ์ ๋ณด๊ฐ ์ค์ํ ์์ญ์์๋ ๋์ฑ ๊ฐ์น๊ฐ ์์ต๋๋ค. RAG๋:
- ํ๊ฐ(Hallucination) ๋ฌธ์ ๋ฅผ ์ค์ด๊ณ ์ฌ์ค์ ๊ธฐ๋ฐํ ์๋ต์ ์ ๊ณตํฉ๋๋ค.
- ์ต์ ์ ๋ณด์ ์ ๊ทผํ ์ ์์ด ๋ชจ๋ธ์ ์ง์ ํ๊ณ๋ฅผ ๊ทน๋ณตํฉ๋๋ค.
- ๊ทผ๊ฑฐ ์ถ์ฒ๋ฅผ ์ ์ํ์ฌ ์๋ต์ ์ ๋ขฐ์ฑ์ ๋์ ๋๋ค.
- ์์ ์๋ด, ์๋จ ์ถ์ฒ, ๊ฑด๊ฐ ์ ๋ณด ์ ๊ณต ๋ฑ ๋ค์ํ ํฌ์ค์ผ์ด ์ ํ๋ฆฌ์ผ์ด์ ์ ํ์ฉํ ์ ์์ต๋๋ค.
์์ผ๋ก ๋ ๋ฐ์ ๋ RAG ๊ธฐ์ ์ ๊ฐ์ธ ๋ง์ถคํ ์์ ์๋ด, ์ํ ์ ๋ณด ๊ฒ์, ๊ฑด๊ฐ ๊ด๋ฆฌ ์๋ด ๋ฑ ํฌ์ค์ผ์ด ๋ถ์ผ์์ ์ค์ํ ์ญํ ์ ํ ๊ฒ์ผ๋ก ๊ธฐ๋๋ฉ๋๋ค.
์ฐธ๊ณ ๋ฌธํ:
- Lewis, P., Perez, E., Piktus, A., Petroni, F., Karpukhin, V., Goyal, N., ... & Kiela, D. (2020). Retrieval-augmented generation for knowledge-intensive NLP tasks. Advances in Neural Information Processing Systems, 33.
- Izacard, G., & Grave, E. (2021). Leveraging passage retrieval with generative models for open domain question answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics.
- Karpukhin, V., Oguz, B., Min, S., Lewis, P., Wu, L., Edunov, S., ... & Yih, W. T. (2020). Dense passage retrieval for open-domain question answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP).
'Food_Health_AI > RAG' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| GraphRAG: Neo4j GenAI ํ์ฉํ์ฌ ๊ตฌํํด๋ณด๊ธฐ (3) | 2025.04.10 |
|---|---|
| GraphRAG: ์ ๋ณด์ ์ ํ์ฑ์ ๋์ด๋ ๊ทธ๋ํ ๊ธฐ๋ฐ ๊ฒ์ ์ฆ๊ฐ ์์ฑ ๊ธฐ์ (3) | 2025.04.10 |