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',
});
const openAI = new OpenAI({
host: 'my-custom-host.com',
});
Chat API
const [result, setResult] = React.useState('');
openAI.chat.addListener('onChatMessageReceived', (payload) => {
setResult((message) => {
const newMessage = payload.choices[0]?.delta.content;
if (newMessage) {
return message + newMessage;
}
return message;
});
});
openAI.chat.stream({
messages: [
{
role: 'user',
content: 'How do I star a repo?',
},
],
model: 'gpt-3.5-turbo',
});
await openAI.chate.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