벡터 데이터베이스로 RAG 저장 및 검색

LangChain으로 LLM 애플리케이션 개발하기

Jonathan Bennion

AI Engineer & LangChain Contributor

RAG 개발 단계

일반적인 RAG 워크플로: 문서 로더, 문서 분할기, 저장 및 검색 프로세스.

  • 이 비디오의 초점: 저장과 검색
LangChain으로 LLM 애플리케이션 개발하기

벡터 데이터베이스란? 왜 필요한가요?

전형적인 RAG 워크플로.

LangChain으로 LLM 애플리케이션 개발하기

어떤 벡터 DB를 선택할까?

 

오픈 소스/상용, 전용/지원 여부로 구분된 다양한 벡터 데이터베이스 현황.

 

고려 사항:

  • 오픈 소스 vs. 상용(라이선스)
  • 클라우드 vs. 온프레미스
  • 경량 vs. 강력
1 이미지 출처: Yingjun Wu
LangChain으로 LLM 애플리케이션 개발하기

문서를 살펴봅시다

docs
[
    Document(
        page_content="In all marketing copy, TechStack should always be written with the T and S
        capitalized. Incorrect: techstack, Techstack, etc.",
        metadata={"guideline": "brand-capitalization"}
    ),
    Document(
        page_content="Our users should be referred to as techies in both internal and external
        communications.",
        metadata={"guideline": "referring-to-users"}
    )
]
LangChain으로 LLM 애플리케이션 개발하기

Chroma 벡터 DB 설정

from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma

embedding_function = OpenAIEmbeddings(api_key=openai_api_key, model='text-embedding-3-small')


vectorstore = Chroma.from_documents( docs, embedding=embedding_function, persist_directory="path/to/directory" )
retriever = vectorstore.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
LangChain으로 LLM 애플리케이션 개발하기

프롬프트 템플릿 만들기

from langchain_core.prompts import ChatPromptTemplate

message = """
Review and fix the following TechStack marketing copy with the following guidelines in consideration:

Guidelines:
{guidelines}

Copy:
{copy}

Fixed Copy:
"""

prompt_template = ChatPromptTemplate.from_messages([("human", message)])
LangChain으로 LLM 애플리케이션 개발하기

모두 연결하기

from langchain_core.runnables import RunnablePassthrough

rag_chain = ({"guidelines": retriever, "copy": RunnablePassthrough()}
             | prompt_template
             | llm)

response = rag_chain.invoke("Here at techstack, our users are the best in the world!")
print(response.content)
Here at TechStack, our techies are the best in the world!
LangChain으로 LLM 애플리케이션 개발하기

Ayo berlatih!

LangChain으로 LLM 애플리케이션 개발하기

Preparing Video For Download...