-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
227 lines (213 loc) · 6.19 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, { useState } from 'react';
import {
SafeAreaView,
ScrollView,
TouchableOpacity,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import { OpenAIApi, Configuration } from 'openai';
import { setupURLPolyfill } from 'react-native-url-polyfill';
import DietComponent from './components/DietComponent';
const OPENAI_API_KEY = 'xxx'; // your openai token should be added here
const config = new Configuration({
apiKey: OPENAI_API_KEY,
});
const openai = new OpenAIApi(config);
const iterate = (obj: any) => {
Object.keys(obj).forEach(key => {
console.log('key ', key, 'value ', obj[key])
if (typeof obj[key] === 'object' && obj[key] !== null) {
iterate(obj[key])
}
})
}
const createCompletionBMI = async (
setAiResponse: any,
bodyMass: string,
height: string,
limitations: string,
sex: string,
gain: string
) => {
setupURLPolyfill();
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
temperature: 0.1,
max_tokens: 300,
messages: [
{"role": "system", "content": `You are a dietitian, and you are recommending proper diet to clients for the best BMI level which you also should provide`},
{"role": "user", "content": `Set up diet for me where my weight is ${bodyMass} kg, my height is ${height} cm and I'm a ${limitations} I'm ${sex} and I want to ${gain}.`}
]
});
setAiResponse(response.data.choices[0].message?.content)
} catch (error) {
console.log('error', error)
}
}
const createCompletionDiet = async (
setAiResponse: any,
bodyMass: string,
height: string,
limitations: string,
sex: string,
gain: string
) => {
setupURLPolyfill();
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
temperature: 0.1,
max_tokens: 700,
messages: [
{"role": "user", "content": `Please make for me one day diet with breakfast, lunch and dinner. I need this in JSON format. I need only one JSON object nothing else so you can't provide me any other content then this JSON. This JSON can be inside string. Info about me: mass-${bodyMass}, height-${height}, sex-${sex}, diet-${limitations} and I want to ${gain} weight. My parser can not parse an array of multiple objects.`}
]
});
if(response.data.choices[0].message?.content) {
const dietObject = JSON.parse(response.data.choices[0].message?.content)
setAiResponse(dietObject)
}
} catch (error) {
console.log('error', error)
}
}
function App(): JSX.Element {
const [response, setResponse] = useState()
const [bodyMass, setBodyMass] = useState<string>()
const [height, setHeight] = useState<string>()
const [dietType, setDietType] = useState<string>()
const [gain, setGain] = useState<string>()
const [sex, setSex] = useState<string>()
return (
<SafeAreaView style={styles.container}>
<ScrollView>
<View style={styles.inputsContainer}>
<View style={styles.inputContainer}>
<Text style={styles.text}>
Body mass in kg
</Text>
<TextInput
style={styles.input}
onChangeText={setBodyMass}
value={bodyMass}
placeholder="Body Mass"
keyboardType='numeric'
/>
</View>
<View style={styles.inputContainer}>
<Text style={styles.text}>
Height in cm
</Text>
<TextInput
style={styles.input}
onChangeText={setHeight}
value={height}
placeholder="Height"
keyboardType='numeric'
/>
</View>
<View style={styles.inputContainer}>
<Text style={styles.text}>
Sex
</Text>
<TextInput
style={styles.input}
onChangeText={setSex}
value={sex}
placeholder="Sex"
keyboardType='default'
/>
</View>
<View style={styles.inputContainer}>
<Text style={styles.text}>
Gain
</Text>
<TextInput
style={styles.input}
onChangeText={setGain}
value={gain}
placeholder="gain"
keyboardType='default'
/>
</View>
<View style={styles.inputContainer}>
<Text style={styles.text}>
Diet types
</Text>
<TextInput
style={styles.input}
onChangeText={setDietType}
value={dietType}
placeholder="Diet type"
keyboardType='default'
/>
</View>
</View>
<DietComponent data={response} />
</ScrollView>
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={() => {
createCompletionBMI(setResponse, bodyMass, height, dietType)
}}
style={styles.button}>
<Text style={styles.buttonText}>SEND REQUEST BMI</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
createCompletionDiet(setResponse, bodyMass, height, dietType, sex, gain)
}}
style={styles.button}>
<Text style={styles.buttonText}>SEND REQUEST TOMMOROW DIET</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 32,
},
input: {
fontSize: 20
},
inputContainer: {
paddingHorizontal: 32,
},
text: {
fontSize: 20
},
inputsContainer: {
flexDirection: 'row',
justifyContent: 'center'
},
response: {
fontSize: 16,
color: 'black'
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'center',
},
button: {
backgroundColor: 'lightyellow',
borderRadius: 32,
margin: 8
},
buttonText: {
fontSize: 32,
paddingHorizontal: 64,
paddingVertical: 16,
},
});
export default App;