You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

react-native-openai

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-openai

OpenAI API for React Native

Source
npmnpm
Version
0.2.1
Version published
Weekly downloads
193
-25.77%
Maintainers
1
Weekly downloads
 
Created
Source
Candle / OpenAI

npm downloads discord users online

Lightweight OpenAI API for React Native


Currently, this library only supports the Chat API. We are working on adding support for the other APIs. If you would like to contribute, please join our Discord and ask questions in the #oss channel or create a pull request.

The goal is to make this library take advantage of the native APIs like URLSession and Android's Ktor engine for better performance and reliability.

Features

Installation

yarn add react-native-openai

Basic Usage

  • Create a new OpenAI instance with your API key and organization ID.
  • Call stream with your messages to generate a streaming completion.
  • Check out the documentation for more information on the available methods.
import OpenAI from 'react-native-openai';

const openAI = new OpenAI('API_KEY', 'ORG_ID');

const [result, setResult] = React.useState('');

React.useEffect(() => {
 openAI.chat.addListener('onChatMessageReceived', (payload) => {
   setResult((message) => {
     const newMessage = payload.choices[0]?.delta.content;
     if (newMessage) {
       return message + newMessage;
     }
     return message;
   });
 });

 return () => {
   openAI.chat.removeListener('onChatMessageReceived');
 };
  }, [openAI]);

// Create a new completion
func ask(question: string) {
   openAI.chat.stream({
      messages: [
        {
          role: 'user',
          content: question,
        },
      ],
      model: 'gpt-3.5-turbo',
  });
}

Credit

Thank you to Dylan Shine & Mouaad Aallam for making openai-kit and openai-kotlin which this library is based on.

License

MIT

Keywords

react-native

FAQs

Package last updated on 17 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts