Skip to content

Commit eb614d5

Browse files
authored
feat: improved api (#12)
* feat: improved api * feat: add GitHub Actions workflow for documentation deployment
1 parent 6446b56 commit eb614d5

67 files changed

Lines changed: 4569 additions & 751 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/docs.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Docs
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: pages
15+
cancel-in-progress: true
16+
17+
jobs:
18+
deploy:
19+
runs-on: ubuntu-latest
20+
environment:
21+
name: github-pages
22+
url: ${{ steps.deployment.outputs.page_url }}
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
cache: yarn
29+
- run: corepack enable
30+
- run: yarn install --immutable
31+
- run: yarn docs
32+
- uses: actions/upload-pages-artifact@v3
33+
with:
34+
path: docs
35+
- id: deployment
36+
uses: actions/deploy-pages@v4

README.md

Lines changed: 14 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
![header](https://github.com/user-attachments/assets/dc07a506-0248-4115-9371-e0d10e6c8792)
44

5-
65
Private, local RAGs. Supercharge LLMs with your own knowledge base.
76

87
## Navigation
@@ -15,13 +14,6 @@ Private, local RAGs. Supercharge LLMs with your own knowledge base.
1514
- [Using the `useRAG` Hook](#1-using-the-userag-hook)
1615
- [Using the `RAG` Class](#2-using-the-rag-class)
1716
- [Using RAG Components Separately](#3-using-rag-components-separately)
18-
- [:book: API Reference](#book-api-reference)
19-
- [Hooks](#hooks)
20-
- [Classes](#classes)
21-
- [Interfaces (for Custom Components)](#interfaces-for-custom-components)
22-
- [Text Splitters](#text-splitters)
23-
- [Utilities](#utilities)
24-
- [Types](#types)
2517
- [:jigsaw: Using Custom Components](#jigsaw-using-custom-components)
2618
- [:electric_plug: Plugins](#electric_plug-plugins)
2719
- [:handshake: Contributing](#handshake-contributing)
@@ -40,7 +32,7 @@ Private, local RAGs. Supercharge LLMs with your own knowledge base.
4032

4133
React Native RAG is powering [Private Mind](https://github.com/software-mansion-labs/private-mind), a privacy-first mobile AI app available on [App Store](https://apps.apple.com/gb/app/private-mind/id6746713439) and [Google Play](https://play.google.com/store/apps/details?id=com.swmansion.privatemind).
4234

43-
<img width="2720" height="1085" alt="Private Mind promo" src="https://github.com/user-attachments/assets/2a5ebb32-0146-4b8e-875f-b25bb5cc50e4" />
35+
<img width="2720" alt="Private Mind promo" src="https://github.com/user-attachments/assets/2a5ebb32-0146-4b8e-875f-b25bb5cc50e4" />
4436

4537
## :package: Installation
4638

@@ -69,7 +61,7 @@ We offer three ways to integrate RAG, depending on your needs.
6961
The easiest way to get started. Good for simple use cases where you want to quickly set up RAG.
7062

7163
```tsx
72-
import React, { useState } from 'react';
64+
import React from 'react';
7365
import { Text } from 'react-native';
7466

7567
import { useRAG, MemoryVectorStore } from 'react-native-rag';
@@ -98,12 +90,9 @@ const llm = new ExecuTorchLLM({
9890
tokenizerConfigSource: LLAMA3_2_TOKENIZER_CONFIG,
9991
});
10092

101-
const app = () => {
93+
const App = () => {
10294
const rag = useRAG({ vectorStore, llm });
103-
104-
return (
105-
<Text>{rag.response}</Text>
106-
);
95+
return <Text>{rag.response}</Text>;
10796
};
10897
```
10998

@@ -128,7 +117,7 @@ import {
128117
LLAMA3_2_TOKENIZER_CONFIG,
129118
} from 'react-native-executorch';
130119

131-
const app = () => {
120+
const App = () => {
132121
const [rag, setRag] = useState<RAG | null>(null);
133122
const [response, setResponse] = useState<string | null>(null);
134123

@@ -147,29 +136,23 @@ const app = () => {
147136
});
148137

149138
const vectorStore = new MemoryVectorStore({ embeddings });
150-
151-
const ragInstance = new RAG({
152-
llm: llm,
153-
vectorStore: vectorStore,
154-
});
139+
const ragInstance = new RAG({ llm, vectorStore });
155140

156141
await ragInstance.load();
157142
setRag(ragInstance);
158143
};
159144
initializeRAG();
160145
}, []);
161146

162-
return (
163-
<Text>{response}</Text>
164-
);
165-
}
147+
return <Text>{response}</Text>;
148+
};
166149
```
167150

168151
### 3. Using RAG Components Separately
169152

170153
For advanced use cases requiring fine-grained control.
171154

172-
This is the recommended way you if you want to implement semantic search in your app, use the `VectorStore` and `Embeddings` classes directly.
155+
This is the recommended way if you want to implement semantic search in your app - use the `VectorStore` and `Embeddings` classes directly.
173156

174157
```tsx
175158
import React, { useEffect, useState } from 'react';
@@ -188,14 +171,14 @@ import {
188171
LLAMA3_2_TOKENIZER_CONFIG,
189172
} from 'react-native-executorch';
190173

191-
const app = () => {
174+
const App = () => {
192175
const [embeddings, setEmbeddings] = useState<ExecuTorchEmbeddings | null>(null);
193176
const [llm, setLLM] = useState<ExecuTorchLLM | null>(null);
194177
const [vectorStore, setVectorStore] = useState<MemoryVectorStore | null>(null);
195178
const [response, setResponse] = useState<string | null>(null);
196179

197180
useEffect(() => {
198-
const initializeRAG = async () => {
181+
const initialize = async () => {
199182
// Instantiate and load the Embeddings Model
200183
// NOTE: Calling load on VectorStore will automatically load the embeddings model
201184
// so loading the embeddings model separately is not necessary in this case.
@@ -219,195 +202,17 @@ const app = () => {
219202
setLLM(llm);
220203
setVectorStore(vectorStore);
221204
};
222-
initializeRAG();
205+
initialize();
223206
}, []);
224-
225-
return (
226-
<Text>{response}</Text>
227-
);
228-
}
229-
```
230-
231-
## :book: API Reference
232-
233-
### Hooks
234-
235-
#### `useRAG(params: UseRAGParams)`
236-
237-
A React hook for Retrieval Augmented Generation (RAG). Manages the RAG system lifecycle, loading, unloading, generation, and document storage.
238-
239-
**Parameters:**
240-
241-
* `params`: An object containing:
242-
* `vectorStore`: An instance of a class that implements `VectorStore` interface.
243-
* `llm`: An instance of a class that implements `LLM` interface.
244-
* `preventLoad` (optional): A boolean to defer loading the RAG system.
245-
246-
**Returns:** An object with the following properties:
247-
248-
* `response` (`string`): The current generated text from the LLM.
249-
* `isReady` (`boolean`): True if the RAG system (Vector Store and LLM) is loaded.
250-
* `isGenerating` (`boolean`): True if the LLM is currently generating a response.
251-
* `isStoring` (`boolean`): True if a document operation (add, update, delete) is in progress.
252-
* `error` (`string | null`): The last error message, if any.
253-
* `generate`: A function to generate text. See `RAG.generate()` for details.
254-
* `interrupt`: A function to stop the current generation. See `RAG.interrupt()` for details.
255-
* `splitAddDocument`: A function to split and add a document. See `RAG.splitAddDocument()` for details.
256-
* `addDocument`: Adds a document. See `RAG.addDocument()` for details.
257-
* `updateDocument`: Updates a document. See `RAG.updateDocument()` for details.
258-
* `deleteDocument`: Deletes a document. See `RAG.deleteDocument()` for details.
259-
260-
### Classes
261-
262-
#### `RAG`
263-
264-
The core class for managing the RAG workflow.
265-
266-
**`constructor(params: RAGParams)`**
267-
268-
* `params`: An object containing:
269-
* `vectorStore`: An instance that implements `VectorStore` interface.
270-
* `llm`: An instance that implements `LLM` interface.
271-
272-
**Methods:**
273-
274-
* `async load(): Promise<this>`: Initializes the vector store and loads the LLM.
275-
* `async unload(): Promise<void>`: Unloads the vector store and LLM.
276-
* `async generate(input: Message[] | string, options?: { augmentedGeneration?: boolean; k?: number; predicate?: (value: SearchResult) => boolean; questionGenerator?: Function; promptGenerator?: Function; callback?: (token: string) => void }): Promise<string>` Generates a response.
277-
* `input` (`Message[] | string`): A string or an array of `Message` objects.
278-
* `options` (object, optional): Generation options.
279-
* `augmentedGeneration` (`boolean`, optional): If `true` (default), retrieves context from the vector store to augment the prompt.
280-
* `k` (`number`, optional): Number of documents to retrieve (default: `3`).
281-
* `predicate` (`function`, optional): Function to filter retrieved documents (default: includes all).
282-
* `questionGenerator` (`function`, optional): Custom question generator.
283-
* `promptGenerator` (`function`, optional): Custom prompt generator.
284-
* `callback` (`function`, optional): A function that receives tokens as they are generated.
285-
* `async splitAddDocument(document: string, metadataGenerator?: (chunks: string[]) => Record<string, any>[], textSplitter?: TextSplitter): Promise<string[]>`: Splits a document into chunks and adds them to the vector store.
286-
* `async addDocument(document: string, metadata?: Record<string, any>): Promise<string>`: Adds a single document to the vector store.
287-
* `async updateDocument(id: string, document?: string, metadata?: Record<string, any>): Promise<void>`: Updates a document in the vector store.
288-
* `async deleteDocument(id: string): Promise<void>`: Deletes a document from the vector store.
289-
* `async interrupt(): Promise<void>`: Interrupts the ongoing LLM generation.
290-
291-
#### `MemoryVectorStore`
292-
293-
An in-memory implementation of the `VectorStore` interface. Useful for development and testing without persistent storage or when you don't need to save documents across app restarts.
294-
295-
**`constructor(params: { embeddings: Embeddings })`**
296207

297-
* `params`: Requires an `embeddings` instance to generate vectors for documents.
298-
299-
* `async load(): Promise<this>`: Loads the Embeddings model.
300-
301-
* `async unload(): Promise<void>`: Unloads the Embeddings model.
302-
303-
### Interfaces (for Custom Components)
304-
305-
These interfaces define the contracts for creating your own custom components.
306-
307-
#### `Embeddings`
308-
309-
* `load: () => Promise<this>`: Loads the embedding model.
310-
* `unload: () => Promise<void>`: Unloads the model.
311-
* `embed: (text: string) => Promise<number[]>`: Generates an embedding for a given text.
312-
313-
#### `LLM`
314-
315-
* `load: () => Promise<this>`: Loads the language model.
316-
* `interrupt: () => Promise<void>`: Stops the current text generation.
317-
* `unload: () => Promise<void>`: Unloads the model.
318-
* `generate: (messages: Message[], callback: (token: string) => void) => Promise<string>`: Generates a response from a list of messages, streaming tokens to the callback.
319-
320-
#### `VectorStore`
321-
322-
* `load: () => Promise<this>`: Initializes the vector store.
323-
* `unload: () => Promise<void>`: Unloads the vector store and releases resources.
324-
* `add(document: string, metadata?: Record<string, any>): Promise<string>`: Adds a document.
325-
* `update(id: string, document?: string, metadata?: Record<string, any>): Promise<void>`: Updates a document.
326-
* `delete(id: string): Promise<void>`: Deletes a document.
327-
* `similaritySearch(query: string, k?: number, predicate?: (value: SearchResult) => boolean): Promise<SearchResult[]>`: Searches for `k` similar documents. Which can be filtered with an optional `predicate` function.
328-
329-
#### `TextSplitter`
330-
331-
* `splitText: (text: string) => Promise<string[]>`: Splits text into an array of chunks.
332-
333-
### Text Splitters
334-
335-
The library provides wrappers around common `langchain` text splitters. All splitters are initialized with `{ chunkSize: number, chunkOverlap: number }`.
336-
337-
* `RecursiveCharacterTextSplitter`: Splits text recursively by different characters. (Default in `RAG` class).
338-
* `CharacterTextSplitter`: Splits text by a fixed character count.
339-
* `TokenTextSplitter`: Splits text by token count.
340-
* `MarkdownTextSplitter`: Splits text while preserving Markdown structure.
341-
* `LatexTextSplitter`: Splits text while preserving LaTeX structure.
342-
343-
### Utilities
344-
345-
* `uuidv4(): string`: Generates a compliant Version 4 UUID. Not cryptographically secure.
346-
* `cosine(a: number[], b: number[]): number`: Calculates the cosine similarity between two vectors.
347-
* `dotProduct(a: number[], b: number[]): number`: Calculates the dot product of two vectors.
348-
* `magnitude(a: number[]): number`: Calculates the Euclidean magnitude of a vector.
349-
350-
### Types
351-
352-
```typescript
353-
interface Message {
354-
role: 'user' | 'assistant' | 'system';
355-
content: string;
356-
}
357-
358-
type ResourceSource = string | number | object;
359-
360-
interface SearchResult {
361-
id: string;
362-
content: string;
363-
metadata?: Record<string, any>;
364-
similarity: number;
365-
}
208+
return <Text>{response}</Text>;
209+
};
366210
```
367211

368212
## :jigsaw: Using Custom Components
369213

370214
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.
371215

372-
```typescript
373-
interface Embeddings {
374-
load: () => Promise<this>;
375-
unload: () => Promise<void>;
376-
embed: (text: string) => Promise<number[]>;
377-
}
378-
379-
interface LLM {
380-
load: () => Promise<this>;
381-
interrupt: () => Promise<void>;
382-
unload: () => Promise<void>;
383-
generate: (
384-
messages: Message[],
385-
callback: (token: string) => void
386-
) => Promise<string>;
387-
}
388-
389-
interface TextSplitter {
390-
splitText: (text: string) => Promise<string[]>;
391-
}
392-
393-
interface VectorStore {
394-
load: () => Promise<this>;
395-
unload: () => Promise<void>;
396-
add(document: string, metadata?: Record<string, any>): Promise<string>;
397-
update(
398-
id: string,
399-
document?: string,
400-
metadata?: Record<string, any>
401-
): Promise<void>;
402-
delete(id: string): Promise<void>;
403-
similaritySearch(
404-
query: string,
405-
k?: number,
406-
predicate?: (value: SearchResult) => boolean
407-
): Promise<SearchResult[]>;
408-
}
409-
```
410-
411216
## :electric_plug: Plugins
412217

413218
* [`@react-native-rag/executorch`](packages/executorch/README.md): On-device inference with `react-native-executorch`.

docs/.nojekyll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.

docs/assets/hierarchy.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)