Skip to content
المشاريع

حول

Private, local RAGs. Supercharge LLMs with your own knowledge base.

الذكاء الاصطناعي وتعلّم الآلة

كيف تُقاس هذه الأرقام

ما يمكنك فعله

  • Modular: Use only the components you need. Choose from LLM, Embeddings, VectorStore, and TextSplitter.
  • Extensible: Create your own components by implementing the LLM, Embeddings, VectorStore, and TextSplitter interfaces.
  • Multiple Integration Options: Whether you prefer a simple hook (useRAG), a powerful class (RAG), or direct component interaction, the library adapts to your needs.
  • On-device Inference: Powered by @react-native-rag/executorch, allowing for private and efficient model execution directly on the user's device.

النمو المقاس للنجوم

خلال 24 ساعة
٠
خلال 7 أيام
٠
خلال 15 يومًا
+٢
خلال شهر
جارٍ جمع السجل

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

React Native RAG

header

Ad Ad Ad

Private, local RAGs. Supercharge LLMs with your own knowledge base.

Navigation

:rocket: Features

  • Modular: Use only the components you need. Choose from LLM, Embeddings, VectorStore, and TextSplitter.
  • Extensible: Create your own components by implementing the LLM, Embeddings, VectorStore, and TextSplitter interfaces.
  • Multiple Integration Options: Whether you prefer a simple hook (useRAG), a powerful class (RAG), or direct component interaction, the library adapts to your needs.
  • On-device Inference: Powered by @react-native-rag/executorch, allowing for private and efficient model execution directly on the user's device.
  • Vector Store Persistence: Includes support for SQLite with @react-native-rag/op-sqlite to save and manage vector stores locally.
  • Semantic Search Ready: Easily implement powerful semantic search in your app by using the VectorStore and Embeddings components directly.

:earth_africa: Real-World Example

React Native RAG is powering Private Mind, a privacy-first mobile AI app available on App Store and Google Play.

Private Mind promo

:package: Installation

npm install react-native-rag

You will also need an embeddings model and a large language model. We recommend using @react-native-rag/executorch for on-device inference. To use it, install the following packages:

npm install @react-native-rag/executorch react-native-executorch react-native-worklets react-native-blob-util

react-native-executorch 0.11 requires React Native 0.83 to 0.86 (0.87 with react-native-worklets 0.12+), Expo SDK 55+ with development builds, the New Architecture, iOS 17+ and Android 13+. See the @react-native-rag/executorch README for details.

For persisting vector stores, you can use @react-native-rag/op-sqlite:

:iphone: Quickstart - Example App

For a complete example app that demonstrates how to use the library, check out the example app.

:books: Usage

We offer three ways to integrate RAG, depending on your needs.

1. Using the useRAG Hook

The easiest way to get started. Good for simple use cases where you want to quickly set up RAG.

import React from 'react';
import { Text } from 'react-native';

import { useRAG, MemoryVectorStore } from 'react-native-rag';
import { models } from 'react-native-executorch';
import {
  ExecuTorchEmbeddings,
  ExecuTorchLLM,
} from '@react-native-rag/executorch';

const vectorStore = new MemoryVectorStore({
  embeddings: new ExecuTorchEmbeddings(
    models.textEmbeddings.ALL_MINILM_L6_V2.DEFAULT
  ),
});

const llm = new ExecuTorchLLM(models.llm.LFM2_5_1_2B.DEFAULT);

const App = () => {
  const rag = useRAG({ vectorStore, llm });
  return <Text>{rag.response}</Text>;
};

2. Using the RAG Class

For more control over components and configuration.

import React, { useEffect, useState } from 'react';
import { Text } from 'react-native';

import { RAG, MemoryVectorStore } from 'react-native-rag';
import {
  ExecuTorchEmbeddings,
  ExecuTorchLLM,
} from '@react-native-rag/executorch';
import { models } from 'react-native-executorch';

const App = () => {
  const [rag, setRag] = useState<RAG | null>(null);
  const [response, setResponse] = useState<string | null>(null);

  useEffect(() => {
    const initializeRAG = async () => {
      const embeddings = new ExecuTorchEmbeddings(
        models.textEmbeddings.ALL_MINILM_L6_V2.DEFAULT
      );

      const llm = new ExecuTorchLLM(models.llm.LFM2_5_1_2B.DEFAULT);

      const vectorStore = new MemoryVectorStore({ embeddings });
      const ragInstance = new RAG({ llm, vectorStore });

      await ragInstance.load();
      setRag(ragInstance);

      const answer = await ragInstance.generate({
        input: 'What is RAG?',
        callback: (token) => setResponse((prev) => (prev ?? '') + token),
      });
      console.log(answer);
    };
    initializeRAG();
  }, []);

  return <Text>{response}</Text>;
};

3. Using RAG Components Separately

For advanced use cases requiring fine-grained control.

This is the recommended way if you want to implement semantic search in your app - use the VectorStore and Embeddings classes directly.

import React, { useEffect, useState } from 'react';
import { Text } from 'react-native';

import { MemoryVectorStore } from 'react-native-rag';
import {
  ExecuTorchEmbeddings,
  ExecuTorchLLM,
} from '@react-native-rag/executorch';
import { models } from 'react-native-executorch';

const App = () => {
  const [embeddings, setEmbeddings] = useState<ExecuTorchEmbeddings | null>(null);
  const [llm, setLLM] = useState<ExecuTorchLLM | null>(null);
  const [vectorStore, setVectorStore] = useState<MemoryVectorStore | null>(null);
  const [response, setResponse] = useState<string | null>(null);

  useEffect(() => {
    const initialize = async () => {
      // Instantiate and load the Embeddings Model
      // NOTE: Calling load on VectorStore will automatically load the embeddings model
      // so loading the embeddings model separately is not necessary in this case.
      const embeddings = await new ExecuTorchEmbeddings(
        models.textEmbeddings.ALL_MINILM_L6_V2.DEFAULT
      ).load();

      // Instantiate and load the Large Language Model
      const llm = await new ExecuTorchLLM(
        models.llm.LFM2_5_1_2B.DEFAULT
      ).load();

      // Instantiate and initialize the Vector Store
      const vectorStore = await new MemoryVectorStore({ embeddings }).load();

      setEmbeddings(embeddings);
      setLLM(llm);
      setVectorStore(vectorStore);

      // Generate directly with the LLM, streaming tokens as they arrive
      await llm.generate(
        [{ role: 'user', content: 'What is RAG?' }],
        (token) => setResponse((prev) => (prev ?? '') + token)
      );
    };
    initialize();
  }, []);

  return <Text>{response}</Text>;
};

:jigsaw: Using Custom Components

Bring your own components by creating classes that implement the LLM, Embeddings, VectorStore and TextSplitter interfaces. This allows you to use any model or service that fits your needs.

:electric_plug: Plugins

:handshake: Contributing

Contributions are welcome! See the contributing guide to learn about the development workflow.

:page_facing_up: License

MIT

React Native RAG is created by Software Mansion AI Labs

Software Mansion AI Labs

We're an end-to-end AI development lab focused on local and hybrid AI. We build and fine-tune models, deploy them anywhere and turn them into agents that solve real business problems. Hire us.

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…