-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
126 lines (121 loc) · 3.45 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
import {
Button,
FlatList,
SafeAreaView,
Text,
TextInput,
View,
} from "react-native";
import { OpenAI, isReactElement, useChat } from "react-native-gen-ui";
import { z } from "zod";
const openAi = new OpenAI({
apiKey: process.env.EXPO_PUBLIC_OPENAI_API_KEY ?? "",
model: process.env.EXPO_PUBLIC_OPENAI_MODEL || "gpt-4",
});
export default function App() {
const { input, messages, isLoading, handleSubmit, onInputChange } = useChat({
openAi,
initialMessages: [
{ content: "Hi! How can I help you today?", role: "assistant" },
],
onSuccess: (messages) => console.log("Chat success:", messages),
onError: (error) => console.error("Chat error:", error),
tools: {
// Example function - roll a dice
rollDice: {
description: "Roll a dice",
parameters: z.object({}),
render: async function* () {
return {
component: <></>,
data: {
// Random number between 1 and 6
result: Math.floor(Math.random() * 6) + 1,
},
};
},
},
},
});
return (
<SafeAreaView
style={{
flex: 1,
}}
>
<View style={{ flex: 1, display: "flex", paddingHorizontal: 20 }}>
<FlatList
data={messages}
inverted
style={{ flexGrow: 1 }}
fadingEdgeLength={10}
keyExtractor={(_, index) => index.toString()}
contentContainerStyle={{
flexDirection: "column-reverse",
padding: 12,
}}
renderItem={({ item, index }) => {
const isLast = index === messages.length - 1;
if (isReactElement(item)) {
// Message can React component or string (see function calling section for more details)
return item;
}
switch (item.role) {
case "user":
// User sent messages
return (
<Text
style={{
color: "blue",
paddingVertical: 8,
}}
key={index}
>
{item.content?.toString()}
</Text>
);
case "assistant":
// Assistant responses
return (
<Text key={index} style={{ paddingVertical: 8 }}>
{item.content?.toString()}
</Text>
);
default:
// This includes tool calls, tool results and system messages
// Those are visible to the model, but here we hide them to the user
return null;
}
}}
/>
<View
style={{
flexShrink: 0,
display: "flex",
flexDirection: "row",
alignItems: "center",
gap: 10,
}}
>
{/* Text input for user to type messages + send button */}
<TextInput
value={input}
style={{
flex: 1,
padding: 10,
borderWidth: 1,
color: "black",
borderColor: "black",
}}
onChangeText={onInputChange}
/>
<Button
onPress={() => handleSubmit(input)}
title="Send"
disabled={isLoading}
/>
</View>
</View>
</SafeAreaView>
);
}