I am working on a react native chat app, and I would like to use openai api to implement auto replying.
There is stream available from openai's api, but it uses SSE, which seems not working well in react native.
Is there any way that I can use text streaming in my project?
I have tried most of the advices mentioned here and other solutions, but they were not working as expected.
- External libraries: I have successfully implemented streaming in React (using Next.js) with libraries like
openai-streams
,react-chat-stream
,openai-streaming-hooks
.etc . However, these libraries do not work in react native. - Fetch for Streaming: someone mentioned fetch is available for streaming, I tried it, along with libraries like
@async-util/fetch
,react-native-fetch-api
too. But they weren't working too. - React Native WebView: it can make requests and send the response back to the app, inspired by this repo, but I think it is not feasible for long term usage as there might be more and more functions coming to the app in the future, I think it will be difficult to maintain as using this method requires injecting js code.
- SSE Packages: i tried packages such as
react-native-event-source
,react-native-sse
. However, I'm still encountering issues where the functionality is not working as expected. I'm unsure if the problem lies with my implementation or with the packages.
I am so confused right now. Is there any guidance or working examples to achieve the streaming response?
Here are two proposed solutions that I think is the most appropriate and will keep trying, please tell me if there are any alternative approaches that I should consider or improvements to those methods, I would greatly appreciate the advice.
- implement SSE, but I am relatively new to it, so it would be great if there are some specific boilerplate code or examples provided.
- use a server (like axios?) to receive the stream response and return back to the app, but I am not sure whether the server should be written in the app itself or outside the app. Also, as I am using Expo, I noticed the upcoming ExpoSDK include api routes, does that mean I can write the request in the app later?
Thank you for your time.
Respond to zoran404:
Yes, I have tried. The react-native-polyfill-globals/auto
is added because without it will raise error Property 'TextEncoder' doesn't exist
. Here are my approach and I am really not sure about it.
import 'react-native-polyfill-globals/auto'
import { fetch } from 'react-native-fetch-api'
async function fetchStreamMessage() {
const url = 'https://api.openai.com/v1/chat/completions'
fetch(url, {
reactNative: { textStreaming: true },
method: 'POST',
headers: {
Authorization: `Bearer ${OPENAI_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
messages: [{ role: 'user', content: 'Tell me a joke' }],
model: 'gpt-4',
stream: true
})
})
.then((response: Response) => response.body)
.then((body: ReadableStream) => {
const reader = body.getReader()
console.log(reader)
})
}