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
0
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

latest
Source
npmnpm
Version
0.6.2
Version published
Maintainers
0
Created
Source

https://github.com/candlefinance/react-native-openai/assets/12258850/44a496dc-68bc-44ee-9224-07c302121e94



OpenAI for React Native


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.

If you would like to contribute, please join our Discord and ask questions in the #oss channel or create a pull request.

Features

Installation

Requires iOS 15+ and Android minSdkVersion = 24.

yarn add react-native-openai

Basic Usage

import OpenAI from 'react-native-openai';

const openAI = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  organization: 'YOUR_ORGANIZATION',
});

// Alternatively (recommended), you can use your own backend and not hardcode an API key in your app i.e. https://my-custom-domain.com/v1/chat/completions (follow the same API as OpenAI until pathPrefix is fixed on Android)
const openAI = new OpenAI({
  host: 'my-custom-host.com',
});

Chat API

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

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

// Send a message
openAI.chat.stream({
  messages: [
    {
      role: 'user',
      content: 'How do I star a repo?',
    },
  ],
  model: 'gpt-3.5-turbo',
});

// Alternatively, you can use the create method if `stream` is false
await openAI.chat.create(...)

Image API

const result = await openAI.image.create({
  prompt: 'An awesome Candle logo',
  n: 3,
  size: '512x512',
});

setImages(result.data.map((image) => image.url));

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 02 Nov 2024

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