Skip to content

Commit 58e1309

Browse files
russellwheatleymikehardy
authored andcommitted
chore: improve vertexai example
1 parent 3ae9fad commit 58e1309

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

tests/test-app/examples/vertexai/base-64-media.js

Lines changed: 19 additions & 0 deletions
Large diffs are not rendered by default.

tests/test-app/examples/vertexai.js renamed to tests/test-app/examples/vertexai/vertexai.js

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,72 @@
1-
import React from 'react';
2-
import { AppRegistry, Button, View } from 'react-native';
1+
import React, { useState } from 'react';
2+
import { AppRegistry, Button, View, Text, Pressable } from 'react-native';
33

44
import { getApp } from '@react-native-firebase/app';
55
import { getVertexAI, getGenerativeModel, Schema } from '@react-native-firebase/vertexai';
6+
import {
7+
PDF_BASE_64,
8+
POEM_BASE_64,
9+
VIDEO_BASE_64,
10+
IMAGE_BASE_64,
11+
EMOJI_BASE_64,
12+
} from './base-64-media';
13+
14+
// eslint-disable-next-line react/prop-types
15+
function OptionSelector({ selectedOption, setSelectedOption }) {
16+
const options = ['image', 'pdf', 'video', 'audio', 'emoji'];
17+
18+
return (
19+
<View style={{ flexDirection: 'row', flexWrap: 'wrap', margin: 10 }}>
20+
{options.map(option => {
21+
const isSelected = selectedOption === option;
22+
return (
23+
<Pressable
24+
key={option}
25+
onPress={() => setSelectedOption(option)}
26+
style={{
27+
paddingVertical: 10,
28+
paddingHorizontal: 15,
29+
margin: 5,
30+
borderRadius: 8,
31+
borderWidth: 1,
32+
borderColor: isSelected ? '#007bff' : '#ccc',
33+
backgroundColor: isSelected ? '#007bff' : '#fff',
34+
}}
35+
>
36+
<Text style={{ color: isSelected ? '#fff' : '#000', fontSize: 16 }}>
37+
{option.toUpperCase()}
38+
</Text>
39+
</Pressable>
40+
);
41+
})}
42+
</View>
43+
);
44+
}
645

746
function App() {
47+
const [selectedOption, setSelectedOption] = useState('image');
48+
const getMediaDetails = option => {
49+
switch (option) {
50+
case 'image':
51+
return { data: IMAGE_BASE_64.trim(), mimeType: 'image/jpeg', prompt: 'What can you see?' };
52+
case 'pdf':
53+
return {
54+
data: PDF_BASE_64.trim(),
55+
mimeType: 'application/pdf',
56+
prompt: 'What can you see?',
57+
};
58+
case 'video':
59+
return { data: VIDEO_BASE_64.trim(), mimeType: 'video/mp4', prompt: 'What can you see?' };
60+
case 'audio':
61+
return { data: POEM_BASE_64.trim(), mimeType: 'audio/mp3', prompt: 'What can you hear?' };
62+
case 'emoji':
63+
return { data: EMOJI_BASE_64.trim(), mimeType: 'image/png', prompt: 'What can you see?' };
64+
default:
65+
console.error('Invalid option selected');
66+
return null;
67+
}
68+
};
69+
870
return (
971
<View>
1072
<View style={{ height: 90 }} />
@@ -48,21 +110,24 @@ function App() {
48110
}
49111
}}
50112
/>
113+
<Text style={{ margin: 10, fontSize: 16 }}>Select a File Type for multi-modal input:</Text>
114+
<OptionSelector selectedOption={selectedOption} setSelectedOption={setSelectedOption} />
51115
<Button
52116
title="Generate Content Stream multi-modal"
53117
onPress={async () => {
54118
try {
55119
const app = getApp();
56120
const vertexai = getVertexAI(app);
57121
const model = getGenerativeModel(vertexai, { model: 'gemini-1.5-flash' });
58-
const prompt = 'What can you see?';
59-
const base64Emoji =
60-
'iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=';
122+
const mediaDetails = getMediaDetails(selectedOption);
123+
if (!mediaDetails) return;
124+
125+
const { data, mimeType, prompt } = mediaDetails;
61126

62127
// Call generateContentStream with the text and images
63128
const response = await model.generateContentStream([
64129
prompt,
65-
{ inlineData: { mimeType: 'image/png', data: base64Emoji } },
130+
{ inlineData: { mimeType, data } },
66131
]);
67132

68133
let text = '';

tests/test-app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*
44
* comment out all imports but the one you wish to run
55
*/
6-
require('./examples/vertexai');
6+
require('./examples/vertexai/vertexai');

0 commit comments

Comments
 (0)