Cohere로 챗봇 만들기
이 페이지는 Cohere의 Command 모델로 문서에 대한 질문에 답하는 챗봇을 만드는 방법을 설명해요. RAG 방식의 챗봇을 단계별로 구축해 볼게요.
출처: 문서
본문
이 튜토리얼은 Cohere의 Command 모델을 사용해 문서의 질문에 답하는 챗봇을 만드는 단계별 가이드예요.
이 튜토리얼은 코드 불필요한 Cohere 소개인 "Build Things with Cohere"에서 각색했어요. 환경을 설정하려면 다음 중 하나를 완료하세요:
-
튜토리얼 시리즈의 Part 1: 설치 및 설정을 완료해요.
-
그리고/또는 SDK 설치와 키 발급에 대한 빠른 안내가 있는 Quickstart를 확인해요.
문서와 채팅하기 (Chat with Your Documents)
이 섹션은 문서와 채팅하는 데 사용되는 챗봇에 초점을 맞춰요. 즉, 사용자가 제공한 문서의 내용을 바탕으로 응답해요.
문서와 채팅하는 챗봇을 만들려면 사용자 질문을 기반으로 문서의 관련 부분을 검색해야 해요. 이를 위해 임베딩과 벡터 검색을 활용할 수 있어요. 시맨틱 검색에 대해 자세히 알아보세요.
이 튜토리얼에서는 직원의 무료 여행 경비에 대한 FAQ 공개 데이터셋을 사용해 볼게요.
데이터셋이 짧다는 점에 유의하세요. 나중에 이를 대량의 문서로 확장하는 방법에 대해 이야기할 거예요.
RAG 챗봇 만들기 (Building a RAG Chatbot)
다음 코드는 RAG 챗봇을 만드는 방법을 보여줘요. RAG (Retrieval-Augmented Generation)는 LLM의 생성 능력과 외부 지식 소스의 지식을 결합해요.
RAG 챗봇을 만드는 단계:
-
프롬프트를 만들어요.
-
문서를 만들고 임베딩해요.
-
chat을 사용해 모델에 연결해요.
-
벡터 스토어를 구축해요.
-
검색 쿼리를 실행하고 모델에 컨텍스트를 제공해요.
코드는 다음과 같아요:
PYTHON
import cohere
co = cohere.ClientV2(api_key="YOUR_COHERE_API_KEY")
# 1. Create your prompts
prompt = "You are an expert FAQ bot that provides accurate, comprehensive, and detailed explanations based on provided documents."
# 2. Create your documents and embed them
# List of Faq docs:
faq_docs = [
{
"text": "Travel: Reimbursing Travel Expenses: Easily manage your travel expenses by submitting them through our finance tool. Approvals are prompt and straightforward.",
},
{
"text": "Travel: Working Remotely from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.",
},
{
"text": "Health and Wellness: Financial Wellness Resources: We offer financial wellness resources like financial planning tools and counseling to support your financial health.",
},
{
"text": "Health and Wellness: Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.",
},
{
"text": "Health and Wellness: Wellness Challenges: We host quarterly wellness challenges that encourage healthy habits with fun prizes for participants.",
},
]
# Embed the FAQ docs
# Get embeddings for the documents
doc_emb = co.embed(
model="embed-v4.0",
texts=[doc["text"] for doc in faq_docs], # Get only the texts from the docs
input_type="search_document",
embedding_types=["float"],
).embeddings.float_
# 3. Create a connection to the model using chat
response = co.chat(
model="command-a-03-2025",
messages=[
{
"role": "system",
"content": prompt,
},
{
"role": "user",
"content": "Where can I find the financial wellness resources?",
},
],
)
print(response.message.content[0].text)
여기서 어디로 갈까요 (Where to go from here)
- 제공된 리소스로 RAG 시스템 구축을 연습해 보세요: RAG with Cohere.
- Reranking with Cohere에서 검색 결과를 개선하는 방법을 배워보세요.
- Building an agent 튜토리얼에서 챗봇에 도구를 부여해 답변을 개선하는 방법을 배워보세요.
- 같은 데이터셋으로 더 복잡한 예제를 보려면 Command R+ 쿡북을 확인해 보세요.