import { StyleSheet, Text, TextInput } from 'react-native'; import React, { useState, useEffect } from 'react'; import OpenAI, { ChatCompletion } from 'openai-react-native'; import { Button, View } from 'react-native'; import * as FileSystem from 'expo-file-system'; import { initializeApp } from 'firebase/app'; import { getAuth, signInAnonymously } from "firebase/auth"; // Initialize Firebase // TODO replace with your Firebase Project config const firebaseConfig = { apiKey: 'AIzaSyAddbefeeNCjWxChVGerxW1lZqDN8XlWUU', authDomain: 'openai-react-native.firebaseapp.com', projectId: 'openai-react-native', }; export default function HomeScreen() { const [text, setText] = useState(''); const [fileDetails, setFileDetails] = useState(''); const [fileContent, setFileContent] = useState(''); const [files, setFiles] = useState(''); const [client, setClient] = useState(null); const [userInput, setUserInput] = useState(''); // New state for user input useEffect(() => { const initializeClient = async () => { initializeApp(firebaseConfig); const auth = getAuth(); const creds = await signInAnonymously(auth); const newClient = new OpenAI({ baseURL: 'https://edge.backmesh.com/v1/proxy/PyHU4LvcdsQ4gm2xeniAFhMyuDl2/K2G8ucCHwh5zN6CLEGeL/v1', apiKey: await creds.user.getIdToken(), }); setClient(newClient); }; initializeClient(); }, []); const startStreaming = async () => { if (!client) return; setText(''); client.chat.completions.stream( { model: 'gpt-4o-mini', messages: [{ role: 'user', content: userInput }], }, (data: ChatCompletion) => { 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: () => { setUserInput(''); console.log('SSE connection for completion opened.'); // Handle when the connection is opened }, onDone: () => { console.log('SSE connection for completion closed.'); // Handle when the connection is opened }, } ); }; const uploadFile = async () => { if (!client) return; try { const filePath = FileSystem.documentDirectory + 'example.txt'; await FileSystem.writeAsStringAsync(filePath, 'Hello, world!'); const file = await client.files.create( filePath, 'fine-tune', ); setFileDetails(JSON.stringify(file, null, 4)); // const content = await client.files.content(file.id); // setFileContent(JSON.stringify(content, null, 4)); const files = await client.files.list(); setFiles(JSON.stringify(files, null, 4)); } catch (err) { console.error(err); } }; return (