Retrieval-Augmented Generation
AboutCommentsNotes
Retrieval-Augmented Generation
Expand for more info
index.js
run
preview
console
/* Retrieval-Augmented Generation */

import { ChatOpenAI } from "langchain/chat_models/openai";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PromptTemplate } from "langchain/prompts";
import {
RunnableSequence,
RunnablePassthrough,
} from "langchain/schema/runnable";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { StringOutputParser } from "langchain/schema/output_parser";

const model = new ChatOpenAI({});

const vectorStore = await MemoryVectorStore.fromTexts(
[
"mitochondria is the powerhouse of the cell",
"lysosomes are the garbage disposal of the cell",
"the nucleus is the control center of the cell",
],
[{ id: 1 }, { id: 2 }, { id: 3 }],
new OpenAIEmbeddings(),
);

const prompt =
PromptTemplate.fromTemplate(`Answer the question based only on the following context:
{context}

Question: {question}`);

const chain = RunnableSequence.from([
prompt,
model,
new StringOutputParser(),
]);

const result = await chain.invoke("What is the powerhouse of the cell?");

console.log(result);
Console
/index.html
-13:50