랭체인JS 실습 - 간단한 LLM 애플리케이션 빌드하기

in #kr-dev2 months ago

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);

Posted using Obsidian Steemit plugin

Sort:  

[광고] 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

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 57328.77
ETH 3111.24
USDT 1.00
SBD 2.42