The goal of this library is to leverage React Native SSE and Expo FileSystem implementations to support calling the OpenAI 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.
If you would like to contribute or request a feature, please join our Discord and ask questions in the #open-source channel or create a pull request or issue.
Install the package
npm i backmesh/openai-react-native
And then instantiate the client:
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,
});
The streaming APIs return an EventSource as implemented by react-native-sse and also optionally provides onDone
, onOpen
, onError
and onData
callbacks.
const eventSource = 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
}
},
(error) => {
console.error('SSE Error:', error); // Handle any errors here
},
() => {
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. It was inspired by this thread.
try {
const filePath = FileSystem.documentDirectory + 'data.pdf'; // Adjust the path as needed
const file = await client.files.expo(filePath, 'fine-tune');
console.log(file);
} catch (error) {
console.error('File creation error:', error);
}
MIT