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

openai-react-native

v0.3.6

Published

OpenAI React Native API Client without polyfills

Downloads

130

Readme

Open AI React Native Client

The goal of this library is to use React Native SSE and Expo FileSystem instead of polyfills to support calling the OpenAI API directly from React Native with streaming and file upload support. The package uses the same types and API as the OpenAI Node SDK wherever possible.

[!CAUTION] This package is meant to be used with a proxy to OpenAI like the one Backmesh provides. The baseURL parameter for this OpenAI client is thus mandatory. If you do not use a proxy and set the baseURL to https://api.openai.com/v1, you are basically exposing your Open AI API key on the internet! You should never expose any secrets in the bundle of a web or mobile app. The correct usage of this client is to create an endpoint on a proxy server for communication with Open AI and then use that endpoint with a user generated auth JWT in your app.

Contributions and Feature Requests

If you would like to contribute or request a feature, please join our Discord and ask questions in the #openai-react-native channel or create a pull request or issue.

Setup

Install the package

npm i openai-react-native

And then instantiate the client:

import OpenAI from 'openai-react-native';

const client = new OpenAI({
  baseURL:
    'https://edge.backmesh.com/v1/proxy/PyHU4LvcdsQ4gm2xeniAFhMyuDl2/yWo35DdTROVMT52N0qs4/',
  // The backmesh proxy uses your auth provider's JWT to authorize access
  apiKey: supabase.auth.session().access_token,
});

Usage

The streaming APIs uses an EventSource from react-native-sse under the hood to provide a required typed onData stream event callback and three optional ones: onDone, onOpen and onError.

client.chat.completions.stream(
  {
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: 'Hello, world!' }],
  },
  (data) => {
    console.log(data.choices[0].delta.content);
    const content = data.choices[0].delta.content;
    if (content) {
      setText((prevText) => prevText + content); // Handle the streaming completion data here
    }
  },
  {
    onError: (error) => {
      console.error('SSE Error:', error); // Handle any errors here
    },
    onOpen: () => {
      console.log('SSE connection for completion opened.'); // Handle when the connection is opened
    },
  }
);

The file upload API is async, requires a purpose string and leverages the Expo File System so only a filepath needs to be provided.

try {
  const filePath = FileSystem.documentDirectory + 'data.pdf'; // Adjust the path as needed
  const file = await client.files.create(filePath, 'fine-tune');
  console.log(file);
} catch (error) {
  console.error('File creation error:', error);
}

Check the example for more details

Coverage

Beta Coverage

License

MIT