Implement RAG (Retrieval Augmented Generation)
mainRAG allows the model to reference a local corpus of documents.
- Setup: Initialize
CactusLMwith acorpusDirpointing to a directory containing.txtfiles. - Generation: Use
complete({ messages })to ask questions based on the corpus. - Direct Query: Use
ragQuery({ query, topK })to search the RAG index directly. It returns an array ofchunks, where each chunk contains ascore,sourcefilename, and thecontent.
ragQuery parameters:
query: The search terms.topK: The number of chunks to retrieve.
import { CactusLM, type CactusLMMessage } from 'cactus-react-native';
const cactusLM = new CactusLM({
corpusDir: 'path/to/your/corpus', // Directory containing .txt files
});
const messages: CactusLMMessage[] = [
{ role: 'user', content: 'What information is in the documents?' },
];
const result = await cactusLM.complete({ messages });
console.log(result.response);
// Or query the RAG index directly
const ragResult = await cactusLM.ragQuery({ query: 'search terms', topK: 5 });
console.log('Chunks:', ragResult.chunks);
// [{ score: 0.85, source: 'doc.txt', content: '...' }, ...]