채팅 엔진
채팅 엔진 (Chat Engine)
채팅 엔진은 인덱스에 담긴 데이터와 대화를 나누는 빠르고 간단한 방법이에요. 쿼리 엔진이 "한 번의 질문·답변"이라면, 채팅 엔진은 여러 번의 주고받음이 일어난다는 차이가 있습니다.
출처: 공식문서
기본 사용법
가장 직접적인 방법은 검색기로 채팅 엔진을 만드는 거예요.
const retriever = index.asRetriever();
const chatEngine = new ContextChatEngine({ retriever });
// start chatting
const response = await chatEngine.chat({ message: query });
한 줄로 줄이면, index.asChatEngine()을 호출해서 바로 시작할 수도 있어요. 그러면 ContextChatEngine을 반환해줍니다.
const chatEngine = index.asChatEngine();
옵션을 채팅 엔진에 넘길 수도 있어요.
const chatEngine = index.asChatEngine({
similarityTopK: 5,
systemPrompt: "You are a helpful assistant.",
});
chat 함수도 스트리밍을 지원해요. stream: true 옵션만 추가하면 됩니다.
const chatEngine = index.asChatEngine();
const stream = await chatEngine.chat({ message: query, stream: true });
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}