랭체인JS 실습 - 간단한 LLM 애플리케이션 빌드하기
Build a Simple LLM Application
ChatModel 사용하기
import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
const model = new ChatOpenAI({ model: "gpt-4o" });
const parser = new StringOutputParser();
const messages = [
new SystemMessage("다음을 한국어에서 영어로 번역하세요."),
new HumanMessage("안녕!"),
];
const chain = model.pipe(parser);
const answer = await chain.invoke(messages);
console.log(answer);
hi!
ChatPromptTemplate 사용하기
import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts"
const model = new ChatOpenAI({ model: "gpt-4o" });
const parser = new StringOutputParser();
const systemTemplate = "다음을 {language}로 번역합니다:";
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", systemTemplate],
["user", "{text}"],
]);
const chain = promptTemplate.pipe(model).pipe(parser);
const answer = await chain.invoke({ language: "영어", text: "안녕!" });
console.log(answer);
[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.
Congratulations, your post has been upvoted by @upex with a 0.21% upvote. We invite you to continue producing quality content and join our Discord community here. Keep up the good work! #upex