Cohere로 RAG 시스템 구축하기
Cohere로 RAG 시스템 구축하기
이 페이지는 Cohere로 임베딩과 reranking을 사용해 RAG 기반 검색 시스템을 만드는 방법을 설명해요. Cohere의 모델로 임베딩을 생성하고, rerank 모델로 답변 품질을 개선하는 구조를 함께 살펴볼게요.
출처: 문서
본문
이 튜토리얼에서는 Cohere로 RAG 시스템을 구축해 볼게요. Cohere의 모델로 임베딩을 생성하고, rerank 모델로 답변을 개선할 거예요.
RAG는 Retrieval-Augmented Generation의 약자로, LLM 분야에서 잘 정립된 기법이에요. 답변을 관련 지식에 근거(grounding)시키기 위해 LLM 생성과 검색을 결합하는 방식을 말해요.
파이프라인은 다음과 같아요:
-
Cohere Embed 모델을 사용해 문서 말뭉치(corpus)에 대한 임베딩을 만들어요.
-
벡터 검색(reranking 포함)을 사용해 쿼리와 가장 관련성 높은 문서를 찾아요.
-
쿼리를 임베딩하고 그 임베딩을 메모리에 유지해요.
-
생성 모델로 컨텍스트 텍스트와 함께 근거 있는 답변을 생성해요.
다음 튜토리얼에서는 Cohere의 Python SDK와 Wall Street Journal의 기사 말뭉치를 사용해 이 과정을 살펴볼게요.
1. 문서 말뭉치에 대한 임베딩 만들기
먼저 문서 말뭉치를 만들고 임베딩하는 것부터 시작해요.
import cohere
co = cohere.ClientV2(api_key="YOUR_COHERE_API_KEY")
docs = [
"The classic method for determining pair gain would be to answer the same question twice with different assumptions",
"Even children's books are being impacted by the rising costs of chips",
"Adults love PowerPoint, PowerPoint is a big deal in Korea, but not elsewhere",
"Newer Startups follow the ""Golden Path"" and choose off-the-shelf models",
"You must learn PowerPoint, if you want to succeed in a world where creativity is being really valued and really scarce",
"The current generation of scientists faces a new set of challenges",
"Forbes ranks them among the first wave of multi-generation business families",
"The Reports are published, the future is in the anticipation of important corporate events",
"Venture Capital Firms between the bottom and the top of the funnel",
"KKR completed the USD 2 billion buyout of Internet Brands",
"The now-listed Altria Group, Calculate hit a record high",
"Reversing the trend of the prior decade, US stocks are now closed on four public holidays",
"third-quarter profits for the US industrial sector rose 2.6% in 2024",
"Sebastian Thrun and his research group are using machine learning to build cars that can drive themselves",
"Worldwide, data breaches have cost an estimated $4.4 trillion in 2023",
"investors are increasingly betting on climate tech companies",
"The rainforest is a source of raw materials for Nike",
"The project is a joint venture between the two companies",
"This is a report about the company's corporate profits",
"troubleshooting on the TV networks is about to improve to be BBC normal",
]
이 docs 목록은 Wall Street Journal 기사에서 가져온 예시 문서 말뭉치예요. 각 문서를 Cohere Embed 모델로 임베딩해 벡터로 변환하고, 이후 검색을 위한 벡터 스토어를 구축할 수 있어요.
1단계: 말뭉치에서 벡터 스토어 만들기
문서들을 임베딩한 뒤, 이 임베딩들로 벡터 스토어(vector store)를 구축해요. 벡터 스토어는 임베딩된 벡터를 저장하고, 쿼리 벡터와의 유사도를 빠르게 계산해 관련 문서를 찾을 수 있게 해주는 저장소예요.
2단계: 검색 후 rerank하기
사용자 쿼리가 들어오면 쿼리를 임베딩해 벡터 스토어에서 가장 유사한 문서를 검색해요. 이때 기본 검색 결과를 Cohere의 rerank 모델로 재정렬하면 쿼리에 가장 관련성 높은 문서를 우선 순위로 얻을 수 있어요. 이렇게 검색된 관련 문서가 생성 모델의 컨텍스트로 제공돼요.
3단계: 컨텍스트로 근거 있는 답변 생성하기
검색된 관련 문서를 컨텍스트로 사용해 생성 모델(예: Command 모델)이 RAG 파이프라인을 통해 근거 있는 답변을 생성해요. 이렇게 하면 모델이 일반적인 지식에만 의존하지 않고, 제공된 문서에 바탕을 둔 정확한 답변을 내놓을 수 있어요.
참고: 이 페이지의 원본 마크다운이 Cohere 측에서 렌더링 문제로 일부 코드 블록이 반복·손상되어 있어, 실제 존재하는 본문 내용(소개, 파이프라인, 단계 설명)과 온전한 코드만을 충실하게 번역했어요.