Fake 통합

Fake 통합

LangChain JavaScript로 Fake 채팅 모델과 통합하는 방법을 안내할게요.

출처: 문서

본문

LangChain은 테스트 목적의 fake LLM 채팅 모델을 제공해요. 이를 통해 LLM 호출을 목킹(mock)하고, LLM이 특정 방식으로 응답했을 때 어떤 일이 일어날지 시뮬레이션할 수 있어요.

사용법

import { FakeListChatModel } from "@langchain/core/utils/testing";
import { HumanMessage } from "@langchain/core/messages";
import { StringOutputParser } from "@langchain/core/output_parsers";

/**
 * The FakeListChatModel can be used to simulate ordered predefined responses.
 */

const chat = new FakeListChatModel({
  responses: ["I'll callback later.", "You 'console' them!"],
});

const firstMessage = new HumanMessage("You want to hear a JavaScript joke?");
const secondMessage = new HumanMessage(
  "How do you cheer up a JavaScript developer?"
);
const firstResponse = await chat.invoke([firstMessage]);
const secondResponse = await chat.invoke([secondMessage]);

console.log({ firstResponse });
console.log({ secondResponse });

/**
 * The FakeListChatModel can also be used to simulate streamed responses.
 */

const stream = await chat
  .pipe(new StringOutputParser())
  .stream(`You want to hear a JavaScript joke?`);
const chunks = [];
for await (const chunk of stream) {
  chunks.push(chunk);
}

console.log(chunks.join(""));

/**
 * The FakeListChatModel can also be used to simulate delays in either synchronous or streamed responses.
 */

const slowChat = new FakeListChatModel({
  responses: ["Because Oct 31 equals Dec 25", "You 'console' them!"],
  sleep: 1000,
});

const thirdMessage = new HumanMessage(
  "Why do programmers always mix up Halloween and Christmas?"
);
const slowResponse = await slowChat.invoke([thirdMessage]);
console.log({ slowResponse });

const slowStream = await slowChat
  .pipe(new StringOutputParser())
  .stream("How do you cheer up a JavaScript developer?");
const slowChunks = [];
for await (const chunk of slowStream) {
  slowChunks.push(chunk);
}

console.log(slowChunks.join(""));

관련 자료


[Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers. [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/chat/fake.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).

더 알아보기