-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
151 lines (139 loc) · 3.91 KB
/
App.tsx
File metadata and controls
151 lines (139 loc) · 3.91 KB
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
import React, { useState } from 'react';
import {
SafeAreaView,
ScrollView,
TextInput,
Button,
Text,
View,
StyleSheet,
ActivityIndicator,
} from 'react-native';
const App = () => {
const [messages, setMessages] = useState([
{ text: 'Merhaba! Size nasıl yardımcı olabilirim?', isBot: true, role: 'assistant' }, // Bot başlangıç mesajı
]);
const [inputText, setInputText] = useState('');
const [isLoading, setIsLoading] = useState(false);
const handleSend = async () => {
if (!inputText.trim()) return;
// Kullanıcının mesajını ekleyin
const userMessage = { text: inputText, isBot: false, role: 'user' }; // Kullanıcı mesajını 'user' rolüyle ekledik
setMessages((prev) => [...prev, userMessage]);
setInputText('');
setIsLoading(true);
try {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
...messages.map((msg) => ({ role: msg.isBot ? 'assistant' : 'user', content: msg.text })), // Mesajin kimden oldugunu soyler
{ role: 'user', content: inputText }, // kullanici mesaj yazar
],
}),
});
if (!response.ok) {
const errorDetails = await response.text();
console.error('API request failed:', response.status, errorDetails);
throw new Error(`API request failed with status ${response.status}`);
}
const data = await response.json();
const botMessage = { text: data.choices[0].message.content.trim(), isBot: true, role: 'assistant' };
setMessages((prev) => [...prev, botMessage]);
} catch (error) {
console.error('Error sending message:', error);
const errorMessage = { text: 'Üzgünüm, bir hata oluştu. Lütfen tekrar deneyin.', isBot: true, role: 'assistant' };
setMessages((prev) => [...prev, errorMessage]);
} finally {
setIsLoading(false);
}
};
return (
<SafeAreaView style={styles.container}>
<ScrollView
style={styles.chatContainer}
contentContainerStyle={styles.messages}
keyboardShouldPersistTaps="handled"
>
{messages.map((msg, index) => (
<View
key={index}
style={[styles.message, msg.isBot ? styles.botMessage : styles.userMessage]}
>
<Text style={styles.messageText}>{msg.text}</Text>
</View>
))}
{isLoading && (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color="#007bff" />
</View>
)}
</ScrollView>
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
value={inputText}
onChangeText={setInputText}
placeholder="Bir mesaj yazın..."
/>
<Button title="Gönder" onPress={handleSend} />
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
},
chatContainer: {
flex: 1,
padding: 10,
},
messages: {
flexGrow: 1,
justifyContent: 'flex-end',
},
message: {
marginVertical: 5,
padding: 10,
borderRadius: 10,
maxWidth: '80%',
},
botMessage: {
backgroundColor: '#e0e0e0',
alignSelf: 'flex-start',
},
userMessage: {
backgroundColor: '#007bff',
alignSelf: 'flex-end',
color: '#fff',
},
messageText: {
color: '#000',
},
inputContainer: {
flexDirection: 'row',
alignItems: 'center',
padding: 10,
borderTopWidth: 1,
borderColor: '#ccc',
},
input: {
flex: 1,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
padding: 10,
marginRight: 10,
},
loadingContainer: {
padding: 20,
alignItems: 'center',
},
});
export default App;