๐Ÿฆœ๐Ÿ”— LangChain ๊ณผ ChatGPT๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ RAG ๊ตฌํ˜„ํ•˜๊ธฐ

Judyยท2024๋…„ 5์›” 15์ผ
2

OpenLLM์Šคํ„ฐ๋””

๋ชฉ๋ก ๋ณด๊ธฐ
5/10

๐Ÿฆœ๐Ÿ”—LangChain?

  • = ๊ฒ€์ƒ‰ + ํ”„๋กฌํ”„ํŠธ + ์ž„๋ฒ ๋”ฉ/์ €์žฅ + ์œ ์‚ฌ๋„ ๊ฒ€์ƒ‰ + ๋žญํ‚น
  • LLM๊ณผ ์™ธ๋ถ€ ๋„๊ตฌ๋ฅผ ๋งˆ์น˜ ์‚ฌ์Šฌ(์ฒด์ธ)์ฒ˜๋Ÿผ ์—ฎ์–ด ๊ฒฐํ•ฉ์‹œ์ผœ ์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

LangChain ๋ชจ๋“ˆ

  • ๋ชจ๋ธ I/O
    • LLM์— ์ „๋‹ฌ๋  ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ (PromptTemplate)
    • ๋‹ต๋ณ€์„ ๋ฐ›๊ธฐ ์œ„ํ•ด ๋ชจ๋ธ API ํ˜ธ์ถœ (HuggingFaceHub ํ™œ์šฉ ๋“ฑ)
    • ๋‹ต๋ณ€์— ๋Œ€ํ•œ ์ถœ๋ ฅ (์ถœ๋ ฅ ํŒŒ์„œ ํ™œ์šฉ)
  • ๋ฐ์ดํ„ฐ ์—ฐ๊ฒฐ
    • ๋ฌธ์„œ ๊ฐ€์ ธ์˜ค๊ธฐ (๋กœ๋”)
    • ๋ฌธ์„œ ๋ณ€ํ™˜
    • ๋ฌธ์„œ ์ž„๋ฒ ๋”ฉ (OpenAIEmbeddings, sentence-transformers ๋“ฑ ํ™œ์šฉ)
    • ๋ฒกํ„ฐ ์ €์žฅ (๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ FAISS ํ™œ์šฉ ๊ฐ€๋Šฅ)
    • ๊ฒ€์ƒ‰๊ธฐ (RetrievalQA)
  • ์ฒด์ธ
    • ์—ฌ๋Ÿฌ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์กฐํ•ฉํ•ด์„œ ํ•˜๋‚˜์˜ ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์„ฑ
      • ๋‹จ์ผ ๋žญ์ฒด์ธ์€ LLMChain ํ™œ์šฉ
      • ์ฒด์ธ ๋‘๊ฐœ ์—ฐ๊ฒฐํ•  ๊ฒฝ์šฐ SequentialChain ํ™œ์šฉ
  • ๋ฉ”๋ชจ๋ฆฌ
    • ๋ชจ๋“  ๋Œ€ํ™” ์œ ์ง€
    • ์ตœ๊ทผ k๊ฐœ์˜ ๋Œ€ํ™” ์œ ์ง€
    • ๋Œ€ํ™”๋ฅผ ์š”์•ฝํ•ด์„œ ์œ ์ง€
  • Agent / Tool
    • ์™ธ๋ถ€ ์ •๋ณด๋ฅผ ํ™œ์šฉํ•  ๋•Œ ์‚ฌ์šฉ
    • Agent ๋Š” LLM์„ ์ด์šฉํ•ด์„œ ์–ด๋–ค ์ž‘์—…์„ ์ˆœ์„œ๋Œ€๋กœ ์ˆ˜ํ–‰ํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ณ , ์ด ์ž‘์—…์— Tool ์‚ฌ์šฉ
    • Tool ์€ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๋กœ, ์œ„ํ‚คํ”ผ๋””์•„ ๋“ฑ LLM ์ด์™ธ์˜ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค

RAG(Retrieval-Argumented Generation)?

  • = ์ •๋ณด ๊ฒ€์ƒ‰ + ์ƒ์„ฑ

Code

์‚ฌ์ „ ์ž‘์—…

  • OpenAI API ๋ฐœ๊ธ‰ ๋ฐ Billing ์ถ”๊ฐ€

๋ฆฌ์†Œ์Šค

Google Colab Pro+
T4 GPU + ๊ณ ์šฉ๋Ÿ‰ RAM

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

# python ์€ 3.8 ๊ถŒ๊ณ  (ํ˜„์žฌ colab ์€ 3.10)
!pip install langchain
# ์ฑ…์—์„œ๋Š” !pip install langchain==0.0.350 ๊ถŒ๊ณ .
!pip install unstructured
!pip install sentence-transformers==2.2.2
!pip install chromadb
!pip install openai

Drive Mount (๋กœ์ปฌ ๊ฒฝ๋กœ ์ด์šฉํ•  ๊ฒฝ์šฐ ์ƒ๋žต ๊ฐ€๋Šฅ)

from google.colab import drive
drive.mount('/content/drive')

1. ํ…์ŠคํŠธ ํŒŒ์ผ ๋กœ๋“œ

https://github.com/gilbutITbook/080413/blob/main/Data/AI.txt

from langchain.document_loaders import TextLoader
documents = TextLoader("/content/sample_data/AI.txt").load()

2. ๋ฌธ์„œ๋ฅผ ์ฒญํฌ๋กœ ๋ถ„ํ• 

ํฐ ๋ฉ์–ด๋ฆฌ์˜ ๋ฌธ์„œ๋ฅผ ์ž‘์€ ๋ฉ์–ด๋ฆฌ๋กœ ๋ถ„ํ• ํ•˜๋Š” ๊ณผ์ •

from langchain.text_splitter import RecursiveCharacterTextSplitter

def split_docs(documents,chunk_size=1000,chunk_overlap=20):
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
  docs = text_splitter.split_documents(documents)
  return docs

# docs ๋ณ€์ˆ˜์— ๋ถ„ํ•  ๋ฌธ์„œ๋ฅผ ์ €์žฅ
docs = split_docs(documents)

3. Chromdb์— ๋ฒกํ„ฐ ์ €์žฅ

๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ธ ํฌ๋กœ๋งˆ์— ์ž„๋ฒ ๋”ฉ ์ฒ˜๋ฆฌ๋œ ๋ฒกํ„ฐ ์ €์žฅ
์ž„๋ฒ ๋”ฉ ์ฒ˜๋ฆฌ ๋ชจ๋ธ : all-MiniLM-L6-v2

from langchain_community.embeddings import SentenceTransformerEmbeddings
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

from langchain.vectorstores import Chroma
db = Chroma.from_documents(docs, embeddings)

์ฝ”๋“œ์—์„œ langchain_community ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ์‹คํ–‰์‹œ์ผœ ๋ณธ๋‹ค (์ด๊ฒŒ ํ•ด๋‹ต์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์Œ..)

!pip install langchain_community

4. OpenAI API Key ์„ค์ •

import os #์šด์˜์ฒด์ œ(os) ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
os.environ["OPENAI_API_KEY"] = "sk-key" #openai ํ‚ค ์ž…๋ ฅ

5. Q&A ์ฒด์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๋‹ต๋ณ€ ์–ป๊ธฐ

from langchain.chains.question_answering import load_qa_chain
chain = load_qa_chain(llm, chain_type="stuff",verbose=True)

6. ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์œ ์‚ฌ์„ฑ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋‹ต๋ณ€์„ ์ƒ์„ฑ

query = "AI๋ž€?"
matching_docs = db.similarity_search(query)
answer =  chain.run(input_documents=matching_docs, question=query)
answer

๊ฒฐ๊ณผ

'AI๋ž€ ์ธ๊ฐ„์ด๋‚˜ ๋™๋ฌผ์˜ ์ง€๋Šฅ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ๊ธฐ๊ณ„๋‚˜ ์†Œํ”„ํŠธ์›จ์–ด์˜ ์ง€๋Šฅ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์šฉ์–ด์ž…๋‹ˆ๋‹ค. ์ปดํ“จํ„ฐ ๊ณผํ•™ ๋ถ„์•ผ์—์„œ ๋ฐœ์ „ํ•˜๊ณ  ์—ฐ๊ตฌ๋˜๋Š” ์ง€๋Šฅ์ ์ธ ๊ธฐ๊ณ„๋“ค์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, AI ๊ธฐ์ˆ ์€ ์‚ฐ์—…, ์ •๋ถ€, ๊ณผํ•™ ๋ถ„์•ผ์—์„œ ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. AI ๋ถ„์•ผ์˜ ์ฃผ์š” ์‘์šฉ ๋ถ„์•ผ๋กœ๋Š” ๊ณ ๊ธ‰ ์›น ๊ฒ€์ƒ‰ ์—”์ง„, ์ถ”์ฒœ ์‹œ์Šคํ…œ, ์Œ์„ฑ ์ธ์‹, ์ž์œจ ์ฃผํ–‰ ์ž๋™์ฐจ, ์ฐฝ์กฐ์ ์ธ ๋„๊ตฌ, ์ „๋žต ๊ฒŒ์ž„ ๋ถ„์„ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. Alan Turing์ด ๊ธฐ๊ณ„ ์ง€๋Šฅ์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๋ถ„์•ผ์—์„œ ์ฒซ ์—ฐ๊ตฌ๋ฅผ ์‹ค์‹œํ–ˆ๊ณ , 1956๋…„์— ํ•™๋ฌธ์  ๋ถ„์•ผ๋กœ ์„ค๋ฆฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ ๋”ฅ ๋Ÿฌ๋‹๊ณผ transformer ์•„ํ‚คํ…์ฒ˜์˜ ๋“ฑ์žฅ์œผ๋กœ AI์— ๋Œ€ํ•œ ๊ด€์‹ฌ๊ณผ ์ž๊ธˆ์ด ํฌ๊ฒŒ ์ฆ๊ฐ€ํ–ˆ์œผ๋ฉฐ, 2020๋…„๋Œ€์—๋Š” ๋ฏธ๊ตญ์„ ์ค‘์‹ฌ์œผ๋กœ ์ค‘์š”ํ•œ ๋ฐœ์ „์ด ์ด๋ฃจ์–ด์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.'

Reference

profile
NLP Researcher

0๊ฐœ์˜ ๋Œ“๊ธ€