Skip to content

Instantly share code, notes, and snippets.

@jung-han
Created March 8, 2024 00:24
Show Gist options
  • Save jung-han/8da0da670fff3c600cf3f6d72a271271 to your computer and use it in GitHub Desktop.
Save jung-han/8da0da670fff3c600cf3f6d72a271271 to your computer and use it in GitHub Desktop.
langchain js > cot example
// https://github.com/rajib76/langchain_examples/blob/main/examples/chain_of_thoughts_example_01.py
import { PromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
const prefix =
"당신은 도움이 되는 챗봇이며, 제공된 맥락에 기반하여 질문에 대답합니다. 만약 답변이 맥락에 없다면, 당신은 답변이 없다는 것을 정중하게 말할 수 있습니다.";
const examples = [
`다음 형식을 사용하세요:
Question: 답해야할 질문
Thought: 무엇을 할 지 생각해야 합니다.
Action: 취할 행동, {context}를 기반으로 합니다.
Action Input: Action에 대한 입력
Observation: Action의 결과
...(이 Thought/Action/Action Input/Observation은 N번 반복할 수 있습니다.)
Thought: 이제 최종 답을 알고 있습니다.
Final Answer: 원래 질문에 대한 최종 답변
`,
];
const suffix = `
Context: {context}
Users: {query}
AI:
`;
const prompt = PromptTemplate.fromExamples(
examples,
suffix,
["context", "query"],
undefined,
prefix
);
const query = "Google 주식을 사고 싶어. 은행을 통해 사는게 좋을까?"; // 은행 계좌를 통해 직접 주식을 사는 것은 불가능합니다. Google 주식을 사려면 거래 계좌를 개설해야 합니다. 은행 고객이라면 은행 포털에 로그인하여 거래 계좌를 개설할 수 있습니다.
// const query = "네이버 주식에 대해 알려줘"; // 죄송합니다, 제공된 정보에서는 네이버 주식에 대한 정보를 찾을 수 없습니다.
const context =
"은행 고객은 은행 계좌를 통해 주식 및 뮤추얼 펀드를 거래할 수 없습니다. 시장에서 거래하려면 거래 계좌를 개설해야 합니다. 은행 고객은 은행 포털에 로그인하여 거래 계좌를 개설할 수 있습니다.";
const formattedPrompt = await prompt.format({ query, context });
const openAI = new ChatOpenAI({
temperature: 0,
modelName: "gpt-4-32k",
openAIApiKey: process.env.OPEN_AI_API_KEY,
});
const result = await openAI.invoke(formattedPrompt);
console.log(result.content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment