Skip to content

Commit 9cdcf39

Browse files
committed
Added new ai
1 parent 2b6c158 commit 9cdcf39

File tree

6 files changed

+99
-98
lines changed

6 files changed

+99
-98
lines changed

AiApi.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
const { GoogleGenerativeAI } = require("@google/generative-ai");
3+
import Apikey from './Apikey';
4+
5+
const genAI = new GoogleGenerativeAI(Apikey);
6+
7+
async function GetGeminiProResponse(historyData, message) {
8+
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
9+
const chat = model.startChat(
10+
{
11+
history: [
12+
{
13+
role: "user",
14+
parts: "Hello, remember that you are a chat bot developed by Ankit Kumar Shah."
15+
},
16+
{
17+
role: "model",
18+
parts: "Ok I will remember that I am a chat bot developed by Ankit Kumar Shah.",
19+
},
20+
{
21+
role: "user",
22+
parts: "Your name is O2 AI."
23+
},
24+
{
25+
role: "model",
26+
parts: "Ok I will remember that my name is O2 AI.",
27+
},
28+
{
29+
role: "user",
30+
parts: "Your are trained by Ankit Kumar Shah."
31+
},
32+
{
33+
role: "model",
34+
parts: "Ok I will remember that I am trained by Ankit Kumar Shah.",
35+
},
36+
{
37+
role: "user",
38+
parts: "Try not to revel that you are a product of Google."
39+
},
40+
{
41+
role: "model",
42+
parts: "Ok I will try my best not to revel that I am a product of Google.",
43+
},
44+
...historyData,
45+
],
46+
}
47+
)
48+
const msg = message;
49+
const result = await chat.sendMessage(msg);
50+
const response = await result.response;
51+
return response.text().length>0 ? response.text(): "Hmm.. I didn't get that.";
52+
}
53+
54+
export {GetGeminiProResponse}

Components/ChatPage/ChatScroll.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function ChatScroll({chat, scrollEnabled}) {
2121
}}
2222
data={chat}
2323
renderItem={({item}) => {
24-
if (item.type === 'user') {
25-
return <Person text={item.message} />;
24+
if (item.role === 'user') {
25+
return <Person text={item.parts} />;
2626
} else {
27-
return <Ai text={item.message} />;
27+
return <Ai text={item.parts} />;
2828
}
2929
}}
3030
keyExtractor={(_, index) => index.toString()}
@@ -34,6 +34,4 @@ function ChatScroll({chat, scrollEnabled}) {
3434

3535
export default memo(ChatScroll);
3636

37-
// {chat.map((e, i) => {
38-
//
39-
// })}
37+

Components/Homepage/EachHistorycard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function EachHistorycard({navigation, item, index, deleteData}) {
2929
paddingHorizontal: 20,
3030
}}>
3131
<Text style={{color: 'white'}}>
32-
{getFormattedData(item[0]?.message ?? '')}
32+
{getFormattedData(item[0]?.parts ?? '')}
3333
</Text>
3434
<TouchableOpacity
3535
onPress={() => {

Screens/ChatPage.js

Lines changed: 30 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import ChatScroll from '../Components/ChatPage/ChatScroll';
1313
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome';
1414
import {faMicrophone, faPaperPlane} from '@fortawesome/free-solid-svg-icons';
1515
import Apikey from '../Apikey';
16+
import {GetGeminiProResponse} from "../AiApi";
1617

1718
const windowWidth = Dimensions.get('window').width;
1819

@@ -23,120 +24,60 @@ export const ChatPage = ({navigation,route}) => {
2324
const Toast = useToast();
2425
const [loading,setloading] = useState(false);
2526
const [VoiceRecording,setVoiceRecording] = useState(false);
26-
const [requestBody,setRequestBody] = useState([]);
2727
const [value,setvalue] = useState('');
2828
const [chat,setchat] = useState([]);
2929
async function OnPressSend(val) {
30-
if (val !== ''){
31-
const requestBodyData = [...requestBody];
32-
requestBodyData.push({content:val});
33-
setRequestBody(requestBodyData);
34-
const chats = [...chat];
35-
chats.push({
36-
message:val,
37-
type:'user',
38-
});
39-
setchat(chats);
40-
setvalue('');
41-
}
30+
const historyData = [...chat]
31+
setchat((history)=>[...history,
32+
{role:'user',
33+
parts:val}
34+
])
35+
setvalue('');
36+
setloading(true);
37+
const response = await GetGeminiProResponse(historyData,val);
38+
setchat((history)=>[...history,
39+
{role:'model', parts:response}
40+
])
41+
setloading(false);
4242
}
43-
44-
useEffect(()=>{
45-
if (requestBody.length > 0 && chat[chat.length - 1].type === 'user'){
46-
setloading(true);
47-
let config = {
48-
method: 'post',
49-
maxBodyLength: Infinity,
50-
url: 'https://generativelanguage.googleapis.com/v1beta2/models/chat-bison-001:generateMessage?key=' + Apikey,
51-
headers: {
52-
'Content-Type': 'application/json',
53-
},
54-
data : JSON.stringify({
55-
'prompt': {'messages': requestBody},
56-
}),
57-
};
58-
axios.request(config).then((r)=>{
59-
if (r.data.filters){
60-
const chats = [...chat];
61-
chats.push({
62-
message:"Hmm... I didn't get that, Can you please rephrase it?",
63-
type:'ai',
64-
});
65-
setchat(chats);
66-
setloading(false);
67-
return;
68-
}
69-
const chats = [...chat];
70-
chats.push({
71-
message:r.data.candidates[0].content,
72-
type:'ai',
73-
});
74-
setchat(chats);
75-
const requestBodyData = [...requestBody];
76-
requestBodyData.push({content:r.data.candidates[0].content});
77-
setRequestBody(requestBodyData);
78-
setloading(false);
79-
}).catch((e)=>{
80-
setloading(false);
81-
if (e.message === 'Network Error'){
82-
Toast.show('No Internet 😟',{
83-
animationDuration:90,
84-
dangerColor: dangerColor,
85-
type: 'danger',
86-
placement: 'center',
87-
duration: 3000,
88-
offset: 30,
89-
animationType: 'zoom-in',
90-
});
91-
}
92-
console.log(e.message);
93-
});
94-
}
95-
},[requestBody]);
9643
useEffect(() => {
9744
Voice.onSpeechStart = onSpeechStartHandler;
9845
Voice.onSpeechEnd = onSpeechEndHandler;
9946
Voice.onSpeechResults = onSpeechResultsHandler;
100-
10147
return () => {
10248
Voice.destroy().then(Voice.removeAllListeners);
10349
};
10450
}, []);
10551
useEffect(()=>{
10652
if (route.params){
10753
setchat(route.params.item);
108-
const requestBodyData = [];
109-
route.params.item.map((e)=>{
110-
requestBodyData.push({content:e.message});
111-
});
112-
setRequestBody(requestBodyData);
11354
}
11455
},[]);
11556
//History
11657
useEffect(() => {
11758
if (!route.params){
118-
if (chat.length === 2){
119-
const Prev = [...History];
120-
Prev.unshift([]);
121-
Prev[0] = [...chat];
122-
setHistory(Prev);
123-
SaveData();
124-
// console.log(Prev)
125-
}
126-
if (chat.length > 2){
127-
const Prev = [...History];
128-
Prev[0] = [...chat];
129-
setHistory(Prev);
130-
SaveData();
131-
}} else {
59+
if (chat.length === 2){
60+
const Prev = [...History];
61+
Prev.unshift([]);
62+
Prev[0] = [...chat];
63+
setHistory(Prev);
64+
SaveData();
65+
// console.log(Prev)
66+
}
67+
if (chat.length > 2){
68+
const Prev = [...History];
69+
Prev[0] = [...chat];
70+
setHistory(Prev);
71+
SaveData();
72+
}} else {
13273
if (chat.length !== History.length && chat.length !== 0){
13374
const Prev = [...History];
13475
Prev[route.params.index] = [...chat];
13576
setHistory(Prev);
13677
SaveData();
13778
}
138-
}
139-
}, [chat]);
79+
}
80+
}, [chat]);
14081
const onSpeechStartHandler = (e) => {
14182
console.log('start handler==>>>', e);
14283
};
@@ -154,8 +95,6 @@ export const ChatPage = ({navigation,route}) => {
15495

15596
const startRecording = async () => {
15697
setVoiceRecording(true);
157-
158-
15998
try {
16099
await Voice.start('en-Us');
161100
const audio = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO);
@@ -270,7 +209,7 @@ export const ChatPage = ({navigation,route}) => {
270209
return;
271210
}
272211
setScrollEnabled(true);
273-
OnPressSend(value);
212+
OnPressSend(value,chat);
274213
}} style={{
275214
height: '100%',
276215
alignItems: 'center',

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@fortawesome/free-regular-svg-icons": "^6.4.2",
1616
"@fortawesome/free-solid-svg-icons": "^6.4.2",
1717
"@fortawesome/react-native-fontawesome": "^0.3.0",
18+
"@google/generative-ai": "^0.1.3",
1819
"@react-native-async-storage/async-storage": "^1.19.3",
1920
"@react-native-community/voice": "^1.1.9",
2021
"@react-navigation/native": "^6.1.9",

0 commit comments

Comments
 (0)