Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import React from "react";
import React from 'react';
import {
StyleSheet,
Text,
Button,
TextInput,
SafeAreaView,
} from "react-native";
import * as FileSystem from "expo-file-system";
import { Pipeline } from "react-native-transformers";
import presets from "./presets.json";
} from 'react-native';
import * as FileSystem from 'expo-file-system';
import { Pipeline } from 'react-native-transformers';
import presets from './presets.json';

export default function App() {
const [progress, setProgress] = React.useState<number>();
const [input, setInput] = React.useState<string>("We love local LLM");
const [input, setInput] = React.useState<string>('We love local LLM');
const [output, setOutput] = React.useState<string>();

const loadModel = async (preset: {
Expand All @@ -21,23 +21,23 @@ export default function App() {
onnx_path: string;
options?: any;
}) => {
console.log("loading");
console.log('loading');
await Pipeline.TextGeneration.init(preset.model, preset.onnx_path, {
verbose: true,
fetch: async (url) => {
try {
console.log("Checking file... " + url);
const fileName = url.split("/").pop()!;
console.log('Checking file... ' + url);
const fileName = url.split('/').pop()!;
const localPath = FileSystem.documentDirectory + fileName;

// Check if the file already exists
const fileInfo = await FileSystem.getInfoAsync(localPath);
if (fileInfo.exists) {
console.log("File already exists: " + localPath);
console.log('File already exists: ' + localPath);
return localPath;
}
console.log("Downloading... " + url);

console.log('Downloading... ' + url);
const downloadResumable = FileSystem.createDownloadResumable(
url,
localPath,
Expand All @@ -46,22 +46,22 @@ export default function App() {
setProgress(totalBytesWritten / totalBytesExpectedToWrite);
}
);

const result = await downloadResumable.downloadAsync();
if (!result) {
throw new Error("Download failed.");
throw new Error('Download failed.');
}
console.log("Downloaded to: " + result.uri);

console.log('Downloaded to: ' + result.uri);
return result.uri;
} catch (error) {
console.error("Download error:", error);
console.error('Download error:', error);
return null;
}
},
...preset.options,
});
console.log("loaded");
console.log('loaded');
};

const AutoComplete = () => {
Expand Down Expand Up @@ -92,11 +92,11 @@ export default function App() {
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
alignItems: 'center',
justifyContent: 'center',
},
input: {
borderWidth: 1,
borderColor: "black",
borderColor: 'black',
},
});
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = {
preset: 'react-native',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: [
'<rootDir>/example/node_modules',
'<rootDir>/lib/',
],
transformIgnorePatterns: [
'node_modules/(?!(' +
'react-native|' +
Expand Down
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"clean": "del-cli lib",
"prepare": "bob build",
"release": "release-it --only-version",
"postinstall": "patch-package"
"postinstall": "patch-package",
"docs": "typedoc src/index.tsx --out docs --exclude '**/*.test.*' --exclude '**/__tests__/**' --skipErrorChecking"
},
"keywords": [
"react-native",
Expand Down Expand Up @@ -74,13 +75,15 @@
"del-cli": "^5.1.0",
"eslint": "^9.22.0",
"eslint-config-prettier": "^10.1.1",
"eslint-plugin-ft-flow": "^3.0.11",
"eslint-plugin-prettier": "^5.2.3",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"react": "19.0.0",
"react-native": "0.79.2",
"react-native-builder-bob": "^0.40.8",
"release-it": "^17.10.0",
"typedoc": "^0.28.5",
"typescript": "^5.8.3"
},
"peerDependencies": {
Expand All @@ -91,13 +94,6 @@
"example"
],
"packageManager": "yarn@3.6.1",
"jest": {
"preset": "react-native",
"modulePathIgnorePatterns": [
"<rootDir>/example/node_modules",
"<rootDir>/lib/"
]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
Expand Down Expand Up @@ -154,6 +150,7 @@
},
"dependencies": {
"@huggingface/transformers": "github:mybigday/transformers.js-rn#merge",
"onnxruntime-react-native": "^1.21.0",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"text-encoding-polyfill": "^0.6.7"
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/__mocks__/@huggingface/transformers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
env: { allowRemoteModels: true, allowLocalModels: false },
AutoTokenizer: {
from_pretrained: jest.fn().mockResolvedValue(
Object.assign(
jest.fn((_text, _options) => ({ input_ids: [1, 2, 3, 4] })),
{
decode: jest.fn((_tokens, _options) => 'decoded text'),
encode: jest.fn((_text, _options) => ({ input_ids: [1, 2, 3, 4] })),
call: jest.fn((_text, _options) => ({ input_ids: [1, 2, 3, 4] })),
}
)
),
},
};
50 changes: 16 additions & 34 deletions src/__tests__/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,23 @@ global.fetch = jest.fn(() =>
// Mock InferenceSession
jest.mock('onnxruntime-react-native', () => ({
InferenceSession: {
create: jest
.fn()
.mockResolvedValue({
run: jest
.fn()
.mockResolvedValue({
logits: {
data: new Float32Array([0.1, 0.2, 0.3, 0.4]),
dims: [1, 1, 4],
type: 'float32',
},
}),
release: jest.fn(),
create: jest.fn().mockResolvedValue({
run: jest.fn().mockResolvedValue({
logits: {
data: new Float32Array([0.1, 0.2, 0.3, 0.4]),
dims: [1, 1, 4],
type: 'float32',
},
}),
release: jest.fn(),
}),
},
env: { logLevel: 'error' },
Tensor: jest
.fn()
.mockImplementation((type, data, dims) => ({
type,
data,
dims,
size: data.length,
dispose: jest.fn(),
})),
}));

// Mock transformers
jest.mock('@huggingface/transformers', () => ({
env: { allowRemoteModels: true, allowLocalModels: false },
AutoTokenizer: {
from_pretrained: jest
.fn()
.mockResolvedValue({
decode: jest.fn((_tokens, _options) => 'decoded text'),
}),
},
Tensor: jest.fn().mockImplementation((type, data, dims) => ({
type,
data,
dims,
size: data.length,
dispose: jest.fn(),
})),
}));
18 changes: 9 additions & 9 deletions src/__tests__/text-embedding.model.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TextEmbedding } from "../models/text-embedding";
import { InferenceSession } from "onnxruntime-react-native";
import { TextEmbedding } from '../models/text-embedding';
import { InferenceSession } from 'onnxruntime-react-native';

describe("TextEmbedding Model", () => {
describe('TextEmbedding Model', () => {
let model: TextEmbedding;

beforeEach(() => {
Expand All @@ -12,17 +12,17 @@ describe("TextEmbedding Model", () => {
await model.release();
});

it("should initialize properly", () => {
it('should initialize properly', () => {
expect(model).toBeInstanceOf(TextEmbedding);
});

it("should throw error when session is undefined", async () => {
it('should throw error when session is undefined', async () => {
await expect(model.embed([1n, 2n, 3n])).rejects.toThrow(
"Session is undefined",
'Session is undefined'
);
});

it("should throw error when no embedding output is found", async () => {
it('should throw error when no embedding output is found', async () => {
// Mock session run to return empty outputs
const mockRun = jest.fn().mockResolvedValue({});
(model as any).sess = {
Expand All @@ -31,11 +31,11 @@ describe("TextEmbedding Model", () => {
} as Partial<InferenceSession>;

await expect(model.embed([1n, 2n, 3n])).rejects.toThrow(
"No embedding output found in model outputs",
'No embedding output found in model outputs'
);
});

it("should properly calculate mean embeddings", async () => {
it('should properly calculate mean embeddings', async () => {
// Mock session run to return sample embeddings
const mockEmbeddings = new Float32Array([1, 2, 3, 4, 5, 6]); // 2 tokens, 3 dimensions
const mockRun = jest.fn().mockResolvedValue({
Expand Down
19 changes: 5 additions & 14 deletions src/__tests__/text-embedding.pipeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@
// Mock the TextEmbedding model
jest.mock('../models/text-embedding', () => {
return {
TextEmbedding: jest
.fn()
.mockImplementation(() => ({
load: jest.fn().mockResolvedValue(undefined),
embed: jest.fn().mockResolvedValue(new Float32Array([0.1, 0.2, 0.3])),
release: jest.fn().mockResolvedValue(undefined),
})),
TextEmbedding: jest.fn().mockImplementation(() => ({
load: jest.fn().mockResolvedValue(undefined),
embed: jest.fn().mockResolvedValue(new Float32Array([0.1, 0.2, 0.3])),
release: jest.fn().mockResolvedValue(undefined),
})),
};
});

// Create a callable tokenizer mock
const createCallableTokenizer = () => {

Check failure on line 15 in src/__tests__/text-embedding.pipeline.test.tsx

View workflow job for this annotation

GitHub Actions / lint

'createCallableTokenizer' is assigned a value but never used
const tokenizer = jest.fn().mockResolvedValue({ input_ids: [1n, 2n, 3n] });
return tokenizer;
};

jest.mock('@huggingface/transformers', () => ({
env: { allowRemoteModels: true, allowLocalModels: false },
AutoTokenizer: {
from_pretrained: jest.fn().mockResolvedValue(createCallableTokenizer()),
},
}));

describe('TextEmbedding Pipeline', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down
Loading
Loading