Skip to content
Snippets Groups Projects
Commit afc05168 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

add RAG to mistral example

parent 02a0f5e9
Branches
Tags
No related merge requests found
import { MistralAI, MistralAIEmbedding } from "llamaindex"; import * as fs from "fs/promises";
import {
BaseEmbedding,
Document,
LLM,
MistralAI,
MistralAIEmbedding,
VectorStoreIndex,
serviceContextFromDefaults,
} from "llamaindex";
async function rag(llm: LLM, embedModel: BaseEmbedding, query: string) {
// Load essay from abramov.txt in Node
const path = "node_modules/llamaindex/examples/abramov.txt";
const essay = await fs.readFile(path, "utf-8");
// Create Document object with essay
const document = new Document({ text: essay, id_: path });
// Split text and create embeddings. Store them in a VectorStoreIndex
const serviceContext = serviceContextFromDefaults({ llm, embedModel });
const index = await VectorStoreIndex.fromDocuments([document], {
serviceContext,
});
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(query);
return response.response;
}
(async () => { (async () => {
// embeddings // embeddings
...@@ -26,4 +57,12 @@ import { MistralAI, MistralAIEmbedding } from "llamaindex"; ...@@ -26,4 +57,12 @@ import { MistralAI, MistralAIEmbedding } from "llamaindex";
for await (const chunk of stream) { for await (const chunk of stream) {
process.stdout.write(chunk); process.stdout.write(chunk);
} }
// rag
const ragResponse = await rag(
llm,
embedding,
"What did the author do in college?",
);
console.log(ragResponse);
})(); })();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment