npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-transformers

v1.0.0-alpha.0

Published

Execute LLM from huggingface on mobile locally with onnxruntime.

Downloads

75

Readme

react-native-transformers

react-native-transformers is a React Native library for incorporating transformer models into your mobile applications. It supports both iOS and Android platforms, allowing you to leverage advanced AI models directly on your device.

Features

  • On-device transformer model support
  • Compatible with iOS and Android
  • Simple API for model loading and text generation

Installation

To use react-native-transformers, you need to install onnxruntime-react-native as a peer dependency. Follow the steps below:

1. Install the peer dependency:

npm install onnxruntime-react-native

2. Install react-native-transformers:

npm install react-native-transformers

3. Configure React-Native or Expo

  • Link the onnxruntime-react-native library:

    npx react-native link onnxruntime-react-native
  • Install the Expo plugin configuration in app.json or app.config.js:

    {
      "expo": {
        "plugins": [
          "onnxruntime-react-native"
        ],
      }
    }

4. Babel Configuration

You need to add the babel-plugin-transform-import-meta plugin to your Babel configuration (e.g., .babelrc or babel.config.js):

{
  "plugins": ["babel-plugin-transform-import-meta"]
}

Usage

Here is an example of how to use react-native-transformers in an Expo application:

import React from "react";
import { View, Text, Button, TextInput } from "react-native";
import { Pipeline } from "react-native-transformers";

export default function App() {
  const [output, setOutput] = React.useState("");

  // Function to initialize the model
  const loadModel = async () => {
    await Pipeline.TextGeneration.init("Felladrin/onnx-Llama-160M-Chat-v1", "onnx/decoder_model_merged.onnx");
  };

  // Function to generate text
  const generateText = () => {
    Pipeline.TextGeneration.generate("Hello world", setOutput);
  };

  return (
    <View>
      <Button title="Load Model" onPress={loadModel} />
      <Button title="Generate Text" onPress={generateText} />
      <Text>Output: {output}</Text>
    </View>
  );
}

API

Pipeline.TextGeneration

The Pipeline.TextGeneration module provides methods for initializing the model, generating text, and releasing resources.

Methods

init(model_name: string, onnx_path: string, options?: Partial<InitOptions>): Promise<void>

Initializes the text generation pipeline with the specified model and ONNX path.

  • Parameters:
    • model_name (string): The name of the model to load.
    • onnx_path (string): The path to the ONNX model.
    • options (Partial, optional): Additional initialization options.
      • max_tokens (number): The maximum number of tokens for text generation.
      • verbose (boolean): Enables verbose logging.
      • externalData (boolean): Indicates if external data is used.
      • fetch (function): Function to fetch external data.
      • executionProviders (InferenceSession.ExecutionProviderConfig[]): List of execution providers for ONNX runtime.
      • show_special (boolean): Shows special tokens in the output.
generate(prompt: string, callback?: (text: string) => void): Promise<string>

Generates text based on the given prompt.

  • Parameters:

    • prompt (string): The input prompt for text generation.
    • callback (function, optional): Optional callback function to handle intermediate text.
  • Returns:

    • (Promise): The generated text.
release(): Promise<void>

Releases the resources used by the model.

Contributing

Contributions are welcome! See the contributing guide to learn how to contribute to the repository and the development workflow.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

This library relies on the ONNX Runtime for running the models efficiently on mobile devices.

External Links

These links provide additional information on how to configure and utilize the various components used by react-native-transformers.