react-native-openai
v0.6.2
Published
OpenAI API for React Native
Downloads
725
Readme
https://github.com/candlefinance/react-native-openai/assets/12258850/44a496dc-68bc-44ee-9224-07c302121e94
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
- [x] Chat
- [ ] Models
- [ ] Completions
- [ ] Edits
- [x] Images
- [ ] Embeddings
- [ ] Files
- [ ] Moderations
- [ ] Fine-tunes
- [ ] Speech to text
- [ ] Function calling
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