-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimplementation.js
47 lines (44 loc) · 1.17 KB
/
implementation.js
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
function search_via_perplexity(params, userSettings) {
const keyword = params.keyword;
const model = userSettings.model || 'sonar';
const systemMessage = userSettings.systemMessage || 'Be precise and concise.';
const key = userSettings.apiKey;
if (!key) {
throw new Error(
'Please set the Perplexity API Key in the plugin settings.'
);
}
return fetch('https://api.perplexity.ai/chat/completions', {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/json',
authorization: 'Bearer ' + key,
},
body: JSON.stringify({
model: model,
messages: [
{
role: 'system',
content: systemMessage,
},
{
role: 'user',
content: keyword,
},
],
}),
})
.then((r) => r.json())
.then((response) => {
const content = response.choices.map((c) => c.message.content).join(' ');
const citations = response.citations;
return (
content +
(citations
? '\n\n Citations:\n' +
citations.map((c, index) => `[${index + 1}] ${c}`).join('\n')
: '')
);
});
}