neptune-web commited on
Commit
2377124
·
1 Parent(s): c739e98

feature(#74): make api for instant answers

Browse files
Files changed (1) hide show
  1. Extension/src/pages/Panel/Panel.jsx +54 -100
Extension/src/pages/Panel/Panel.jsx CHANGED
@@ -7,7 +7,18 @@ import './Panel.css';
7
 
8
  const {Footer, Content} = Layout;
9
 
10
- let currentATags = []
 
 
 
 
 
 
 
 
 
 
 
11
  let prompt = ""
12
 
13
  const Panel = () => {
@@ -16,6 +27,10 @@ const Panel = () => {
16
  const [isLoading, setLoadingStatus] = useState(false);
17
  const chat_box = useRef(null);
18
 
 
 
 
 
19
  const handleQuestionUpdated = (event) => {
20
  if (event.key === "Enter" && !isLoading) {
21
  addMessage(question, true);
@@ -28,6 +43,10 @@ const Panel = () => {
28
  setQuestion(event.target.value);
29
  };
30
 
 
 
 
 
31
  const addMessage = (message, type, isLoading = false) => {
32
  if (message === "") return;
33
 
@@ -45,24 +64,10 @@ const Panel = () => {
45
  }]);
46
  };
47
 
48
- const sendNotification = async () => {
49
- const params = {
50
- "confs": {
51
- "openai_key": "string",
52
- "pinecone_key": "string",
53
- "pinecone_env": "string",
54
- "firebase_key": "string",
55
- "token": "",
56
- "uuid": "",
57
- "settings": {
58
- "temperature": 0.6
59
- }
60
- },
61
- "message": question
62
- }
63
-
64
- setQuestion("")
65
-
66
  const requestOptions = {
67
  method: 'POST', headers: {
68
  'Content-Type': 'application/json'
@@ -70,10 +75,21 @@ const Panel = () => {
70
  }
71
 
72
  setLoadingStatus(true)
73
- const response = await fetch('https://ttt246-brain.hf.space/sendNotification', requestOptions)
74
  const data = await response.json()
75
  setLoadingStatus(false)
76
 
 
 
 
 
 
 
 
 
 
 
 
77
  if (data.result === undefined || data.result == null) {
78
  return
79
  }
@@ -141,20 +157,10 @@ const Panel = () => {
141
  break
142
  case 'select_item_detail_info':
143
  addMessage("I need more detail info to select item", false)
144
- setLoadingStatus(true)
145
- let data = await selectItem()
146
- setLoadingStatus(false)
147
-
148
- console.log(data)
149
-
150
- if(data.result.program === "select_item")
151
- window.open(data.result.content, '_blank', 'noreferrer')
152
- break
153
- case 'select_item':
154
- addMessage("select item", false)
155
  break
156
  case 'ask_website':
157
- askAboutCurrentWebsite();
158
  break
159
  default:
160
  break
@@ -166,44 +172,27 @@ const Panel = () => {
166
 
167
  const selectItem = async () => {
168
  const params = {
169
- "confs": {
170
- "openai_key": "string",
171
- "pinecone_key": "string",
172
- "pinecone_env": "string",
173
- "firebase_key": "string",
174
- "token": "",
175
- "uuid": "",
176
- "settings": {
177
- "temperature": 0.6
178
- }
179
- },
180
  "prompt": prompt,
181
  "items": scrapeATags()
182
  }
183
- console.log(params)
 
 
184
 
185
- const requestOptions = {
186
- method: 'POST', headers: {
187
- 'Content-Type': 'application/json'
188
- }, body: JSON.stringify(params)
 
189
  }
190
-
191
- setLoadingStatus(true)
192
- const response = await fetch('https://ttt246-brain.hf.space/browser/item', requestOptions)
193
- const data = await response.json()
194
- setLoadingStatus(false)
195
-
196
- return data
197
  }
198
 
199
- useEffect(() => {
200
- chat_box.current.scrollTop = chat_box.current.scrollHeight;
201
- }, [messages]);
202
-
203
- useEffect(() => {
204
- currentATags = scrapeATags()
205
- }, [])
206
-
207
  const scrapeATags = () => {
208
  const links = []
209
 
@@ -224,50 +213,15 @@ const Panel = () => {
224
 
225
  const scrapeWebsites = () => {
226
  const data = []
227
-
228
  const tags = document.querySelectorAll(['a', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7'])
229
-
230
  tags.forEach((tag) => {
231
  const content = tag.textContent || ''
232
-
233
- data.push(content)
234
  })
235
-
236
- return data
237
- }
238
-
239
- const askAboutCurrentWebsite = async () => {
240
- const params = {
241
- "confs": {
242
- "openai_key": "string",
243
- "pinecone_key": "string",
244
- "pinecone_env": "string",
245
- "firebase_key": "string",
246
- "token": "",
247
- "uuid": "",
248
- "settings": {
249
- "temperature": 0.6
250
- }
251
- },
252
- "prompt": prompt,
253
- "items": scrapeWebsites()
254
- }
255
-
256
- const requestOptions = {
257
- method: 'POST', headers: {
258
- 'Content-Type': 'application/json'
259
- }, body: JSON.stringify(params)
260
- }
261
-
262
- setLoadingStatus(true)
263
- const response = await fetch('https://ttt246-brain.hf.space/browser/ask_website', requestOptions)
264
- const data = await response.json()
265
- setLoadingStatus(false)
266
-
267
  return data
268
  }
269
 
270
-
271
  return (
272
  <Layout className="main-layout">
273
  <div className="header">
 
7
 
8
  const {Footer, Content} = Layout;
9
 
10
+ const confs = {
11
+ "openai_key": "string",
12
+ "pinecone_key": "string",
13
+ "pinecone_env": "string",
14
+ "firebase_key": "string",
15
+ "token": "",
16
+ "uuid": "",
17
+ "settings": {
18
+ "temperature": 0.6
19
+ }
20
+ }
21
+
22
  let prompt = ""
23
 
24
  const Panel = () => {
 
27
  const [isLoading, setLoadingStatus] = useState(false);
28
  const chat_box = useRef(null);
29
 
30
+ /*
31
+ * methods for states in ui
32
+ * lifecycle methods
33
+ */
34
  const handleQuestionUpdated = (event) => {
35
  if (event.key === "Enter" && !isLoading) {
36
  addMessage(question, true);
 
43
  setQuestion(event.target.value);
44
  };
45
 
46
+ useEffect(() => {
47
+ chat_box.current.scrollTop = chat_box.current.scrollHeight;
48
+ }, [messages]);
49
+
50
  const addMessage = (message, type, isLoading = false) => {
51
  if (message === "") return;
52
 
 
64
  }]);
65
  };
66
 
67
+ /*
68
+ * methods for sending request to server
69
+ */
70
+ const sendRequest = async (params, url) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  const requestOptions = {
72
  method: 'POST', headers: {
73
  'Content-Type': 'application/json'
 
75
  }
76
 
77
  setLoadingStatus(true)
78
+ const response = await fetch(url, requestOptions)
79
  const data = await response.json()
80
  setLoadingStatus(false)
81
 
82
+ return data
83
+ }
84
+
85
+ const sendNotification = async () => {
86
+ const params = {
87
+ "confs": confs,
88
+ "message": question
89
+ }
90
+ setQuestion("")
91
+
92
+ const data = sendRequest(params, 'https://ttt246-brain.hf.space/sendNotification')
93
  if (data.result === undefined || data.result == null) {
94
  return
95
  }
 
157
  break
158
  case 'select_item_detail_info':
159
  addMessage("I need more detail info to select item", false)
160
+ selectItem()
 
 
 
 
 
 
 
 
 
 
161
  break
162
  case 'ask_website':
163
+ askAboutCurrentWebsite()
164
  break
165
  default:
166
  break
 
172
 
173
  const selectItem = async () => {
174
  const params = {
175
+ "confs": confs,
 
 
 
 
 
 
 
 
 
 
176
  "prompt": prompt,
177
  "items": scrapeATags()
178
  }
179
+ const data = sendRequest(params, 'https://ttt246-brain.hf.space/browser/item')
180
+ window.open(data.result.content, '_blank', 'noreferrer')
181
+ }
182
 
183
+ const askAboutCurrentWebsite = async () => {
184
+ const params = {
185
+ "confs": confs,
186
+ "prompt": prompt,
187
+ "items": scrapeWebsites()
188
  }
189
+ const data = sendRequest(params, 'https://ttt246-brain.hf.space/browser/ask_website')
190
+ addMessage(data.result.content, false)
 
 
 
 
 
191
  }
192
 
193
+ /*
194
+ * methods for scraping websites
195
+ */
 
 
 
 
 
196
  const scrapeATags = () => {
197
  const links = []
198
 
 
213
 
214
  const scrapeWebsites = () => {
215
  const data = []
 
216
  const tags = document.querySelectorAll(['a', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7'])
 
217
  tags.forEach((tag) => {
218
  const content = tag.textContent || ''
219
+ if(content !== '')
220
+ data.push(content)
221
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  return data
223
  }
224
 
 
225
  return (
226
  <Layout className="main-layout">
227
  <div className="header">