openai-react-native
v0.3.6
Published
OpenAI React Native API Client without polyfills
Downloads
163
Maintainers
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
- [x] Chat Completion
- [x] Models
- [x] Files
- [x] Moderations
- [ ] Images
- [ ] Audio
- [ ] Embeddings
Beta Coverage
- [x] Threads
- [x] Assistants
License
MIT