Browse by type

npm install cactus-react-native react-native-nitro-modules
Get started with Cactus in just a few lines of code:
import { CactusLM, type CactusLMMessage } from 'cactus-react-native';
// Create a new instance
const cactusLM = new CactusLM();
// Download the model
await cactusLM.download({
onProgress: (progress) => console.log(`Download: ${Math.round(progress * 100)}%`)
});
// Generate a completion
const messages: CactusLMMessage[] = [
{ role: 'user', content: 'What is the capital of France?' }
];
const result = await cactusLM.complete({ messages });
console.log(result.response); // "The capital of France is Paris."
// Clean up resources
await cactusLM.destroy();
Using the React Hook:
import { useCactusLM } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM();
useEffect(() => {
// Download the model if not already available
if (!cactusLM.isDownloaded) {
cactusLM.download();
}
}, []);
const handleGenerate = () => {
// Generate a completion
cactusLM.complete({
messages: [{ role: 'user', content: 'Hello!' }],
});
};
if (cactusLM.isDownloading) {
return (
<Text>
Downloading model: {Math.round(cactusLM.downloadProgress * 100)}%
</Text>
);
}
return (
<>
<Button onPress={handleGenerate} title="Generate" />
<Text>{cactusLM.completion}</Text>
</>
);
};
Choose model quantization and NPU acceleration with Pro models.
import { CactusLM } from 'cactus-react-native';
// Use int8 for better accuracy (default)
const cactusLM = new CactusLM({
model: 'lfm2-vl-450m',
options: {
quantization: 'int8', // 'int4' or 'int8'
pro: false
}
});
// Use pro models for NPU acceleration
const cactusPro = new CactusLM({
model: 'lfm2-vl-450m',
options: {
quantization: 'int8',
pro: true
}
});
Generate text responses from the model by providing a conversation history.
import { CactusLM, type CactusLMMessage } from 'cactus-react-native';
const cactusLM = new CactusLM();
const messages: CactusLMMessage[] = [{ role: 'user', content: 'Hello, World!' }];
const onToken = (token: string) => { console.log('Token:', token) };
const result = await cactusLM.complete({ messages, onToken });
console.log('Completion result:', result);
import { useCactusLM, type CactusLMMessage } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM();
const handleComplete = async () => {
const messages: CactusLMMessage[] = [{ role: 'user', content: 'Hello, World!' }];
const result = await cactusLM.complete({ messages });
console.log('Completion result:', result);
};
return (
<>
<Button title="Complete" onPress={handleComplete} />
<Text>{cactusLM.completion}</Text>
</>
);
};
Vision allows you to pass images along with text prompts, enabling the model to analyze and understand visual content.
import { CactusLM, type CactusLMMessage } from 'cactus-react-native';
// Vision-capable model
const cactusLM = new CactusLM({ model: 'lfm2-vl-450m' });
const messages: CactusLMMessage[] = [
{
role: 'user',
content: "What's in the image?",
images: ['path/to/your/image'],
},
];
const result = await cactusLM.complete({ messages });
console.log('Response:', result.response);
import { useCactusLM, type CactusLMMessage } from 'cactus-react-native';
const App = () => {
// Vision-capable model
const cactusLM = useCactusLM({ model: 'lfm2-vl-450m' });
const handleAnalyze = async () => {
const messages: CactusLMMessage[] = [
{
role: 'user',
content: "What's in the image?",
images: ['path/to/your/image'],
},
];
await cactusLM.complete({ messages });
};
return (
<>
<Button title="Analyze Image" onPress={handleAnalyze} />
<Text>{cactusLM.completion}</Text>
</>
);
};
Enable the model to generate function calls by defining available tools and their parameters.
import { CactusLM, type CactusLMMessage, type CactusLMTool } from 'cactus-react-native';
const tools: CactusLMTool[] = [
{
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name',
},
},
required: ['location'],
},
},
];
const cactusLM = new CactusLM();
const messages: CactusLMMessage[] = [
{ role: 'user', content: "What's the weather in San Francisco?" },
];
const result = await cactusLM.complete({ messages, tools });
console.log('Response:', result.response);
console.log('Function calls:', result.functionCalls);
import { useCactusLM, type CactusLMMessage, type CactusLMTool } from 'cactus-react-native';
const tools: CactusLMTool[] = [
{
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City name',
},
},
required: ['location'],
},
},
];
const App = () => {
const cactusLM = useCactusLM();
const handleComplete = async () => {
const messages: CactusLMMessage[] = [
{ role: 'user', content: "What's the weather in San Francisco?" },
];
const result = await cactusLM.complete({ messages, tools });
console.log('Response:', result.response);
console.log('Function calls:', result.functionCalls);
};
return <Button title="Complete" onPress={handleComplete} />;
};
RAG allows you to provide a corpus of documents that the model can reference during generation, enabling it to answer questions based on your data.
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);
import { useCactusLM, type CactusLMMessage } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM({
corpusDir: 'path/to/your/corpus', // Directory containing .txt files
});
const handleAsk = async () => {
const messages: CactusLMMessage[] = [
{ role: 'user', content: 'What information is in the documents?' },
];
await cactusLM.complete({ messages });
};
return (
<>
<Button title="Ask Question" onPress={handleAsk} />
<Text>{cactusLM.completion}</Text>
</>
);
};
Convert text into tokens using the model's tokenizer.
import { CactusLM } from 'cactus-react-native';
const cactusLM = new CactusLM();
const result = await cactusLM.tokenize({ text: 'Hello, World!' });
console.log('Token IDs:', result.tokens);
import { useCactusLM } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM();
const handleTokenize = async () => {
const result = await cactusLM.tokenize({ text: 'Hello, World!' });
console.log('Token IDs:', result.tokens);
};
return <Button title="Tokenize" onPress={handleTokenize} />;
};
Calculate perplexity scores for a window of tokens within a sequence.
import { CactusLM } from 'cactus-react-native';
const cactusLM = new CactusLM();
const tokens = [123, 456, 789, 101, 112];
const result = await cactusLM.scoreWindow({
tokens,
start: 1,
end: 3,
context: 2
});
console.log('Score:', result.score);
import { useCactusLM } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM();
const handleScoreWindow = async () => {
const tokens = [123, 456, 789, 101, 112];
const result = await cactusLM.scoreWindow({
tokens,
start: 1,
end: 3,
context: 2
});
console.log('Score:', result.score);
};
return <Button title="Score Window" onPress={handleScoreWindow} />;
};
Convert text and images into numerical vector representations that capture semantic meaning, useful for similarity search and semantic understanding.
import { CactusLM } from 'cactus-react-native';
const cactusLM = new CactusLM();
const result = await cactusLM.embed({ text: 'Hello, World!' });
console.log('Embedding vector:', result.embedding);
console.log('Embedding vector length:', result.embedding.length);
import { useCactusLM } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM();
const handleEmbed = async () => {
const result = await cactusLM.embed({ text: 'Hello, World!' });
console.log('Embedding vector:', result.embedding);
console.log('Embedding vector length:', result.embedding.length);
};
return <Button title="Embed" onPress={handleEmbed} />;
};
import { CactusLM } from 'cactus-react-native';
const cactusLM = new CactusLM({ model: 'lfm2-vl-450m' });
const result = await cactusLM.imageEmbed({ imagePath: 'path/to/your/image.jpg' });
console.log('Image embedding vector:', result.embedding);
console.log('Embedding vector length:', result.embedding.length);
import { useCactusLM } from 'cactus-react-native';
const App = () => {
const cactusLM = useCactusLM({ model: 'lfm2-vl-450m' });
const handleImageEmbed = async () => {
const result = await cactusLM.imageEmbed({ imagePath: 'path/to/your/image.jpg' });
console.log('Image embedding vector:', result.embedding);
console.log('Embedding vector length:', result.embedding.length);
};
return <Button title="Embed Image" onPress={handleImageEmbed} />;
};
The CactusSTT class provides audio transcription and audio embedding capabilities using speech-to-text models such as Whisper and Moonshine.
Transcribe audio to text with streaming support. Accepts either a file path or raw PCM audio samples.
import { CactusSTT } from 'cactus-react-native';
const cactusSTT = new CactusSTT({ model: 'whisper-small' });
// Transcribe from file path
const result = await cactusSTT.transcribe({
audio: 'path/to/audio.wav',
onToken: (token) => console.log('Token:', token)
});
console.log('Transcription:', result.response);
// Or transcribe from raw PCM samples
const pcmSamples: number[] = [/* ... */];
const result2 = await cactusSTT.transcribe({
audio: pcmSamples,
onToken: (token) => console.log('Token:', token)
});
console.log('Transcription:', result2.response);
import { useCactusSTT } from 'cactus-react-native';
const App = () => {
const cactusSTT = useCactusSTT({ model: 'whisper-small' });
const handleTranscribe = async () => {
// Transcribe from file path
const result = await cactusSTT.transcribe({
audio: 'path/to/audio.wav',
});
console.log('Transcription:', result.response);
const pcmSamples: number[] = [/* ... */];
const result2 = await cactusSTT.transcribe({
audio: pcmSamples,
});
console.log('Transcription:', result2.response);
};
return (
<>
<Button onPress={handleTranscribe} title="Transcribe" />
<Text>{cactusSTT.transcription}</Text>
</>
);
};
Transcribe audio in real-time with incremental results. Each call to streamTranscribeProcess feeds an audio chunk and returns the currently confirmed and pending text.
import { CactusSTT } from 'cactus-react-native';
const cactusSTT = new CactusSTT({ model: 'whisper-small' });
await cactusSTT.streamTranscribeStart({
confirmationThreshold: 0.99, // confidence required to confirm text
minChunkSize: 32000, // minimum samples before processing
});
const audioChunk: number[] = [/* PCM samples as bytes */];
const result = await cactusSTT.streamTranscribeProcess({ audio: audioChunk });
console.log('Confirmed:', result.confirmed);
console.log('Pending:', result.pending);
const final = await cactusSTT.streamTranscribeStop();
console.log('Final confirmed:', final.confirmed);
```tsx import { useCactusSTT } from 'cactus-react-native';
const App = () => { const cactusSTT = useCactusSTT({ model: 'whisper-small' });
const handleStart = async () => { await cactusSTT.streamTranscribeStart({ confirmationThreshold: 0.99 }); };
const handleChunk = async (audioChunk: number[]) => { const result = await cactusSTT.streamTranscribeProcess({ audio: audioChunk }); console.log('Confirmed:', result.confirmed); console.log('Pending:', result.pending); };
const handleStop = async () => { const final = await cactusSTT.streamTranscribeStop
$ claude mcp add cactus-react-native \
-- python -m otcore.mcp_server <graph>