|
2 | 2 | * @param {string} apiKey
|
3 | 3 | * @param {string} model
|
4 | 4 | * @param {number} temperature
|
| 5 | + * @param {number} topk |
| 6 | + * @param {number} topp |
| 7 | + * @param {number} frequencyPenalty |
| 8 | + * @param {number} presencePenalty |
| 9 | + * @param {number} maxTokens |
5 | 10 | * @param {string} content
|
6 | 11 | * @param {string} language
|
7 | 12 | * @returns {Promise<any>}
|
8 | 13 | * Call the Perplexity API with the provided parameters.
|
9 | 14 | * Return a promise that resolves with the API response.
|
10 | 15 | */
|
11 |
| -function callPerplexityAPI(apiKey, model, temperature, content, language) { |
| 16 | +function callPerplexityAPI(apiKey, model, temperature, topk, topp, frequencyPenalty, presencePenalty, maxTokens, content, language) { |
| 17 | + const API_ENDPOINT = 'https://api.perplexity.ai/chat/completions'; |
12 | 18 | const systemPrompt = `You are an AI assistant that generates concise, high-quality abstracts and keywords for webpage content.
|
13 | 19 |
|
14 |
| -Instructions: |
15 |
| -1. Analyze the provided webpage text and identify the main topics, key points and overall meaning. Take account of the language of the webpage. The ISO code of the language should be detected and if it is not it will be '${language}'. |
16 |
| -2. Generate an abstract in the SAME LANGUAGE as the webpage content. This is crucial. If the webpage is in Spanish, the abstract MUST be in Spanish. If the webpage is in French, the abstract MUST be in French, and so on. |
17 |
| -3. The abstract should: |
18 |
| - - Accurately and concisely summarize the key information in 1-2 paragraphs |
19 |
| - - Be well-written, precise and easy to understand |
20 |
| - - Contain the most important points without extraneous details |
21 |
| - - Be formatted as 1-2 easily readable paragraphs of plain text, each formatted as <p class="abstractp">{paragraph text}</p> WITHOUT MARKDOWN OR SPECIAL CHARACTERS |
22 |
| -4. Extract the most relevant keywords from the text that capture the main topics and themes. |
23 |
| -5. Format the output as follows, including the abstract and keywords, the final output MUST BE a valid HTML node with NO MARKDOWN at all: |
24 |
| -<div class="abstract" lang="{ISO code of the detected language}">{abstract}</div> |
25 |
| -<div class="keywords">{foreach keyword in keywords: <span class="keyword">{keyword}</span> }</div> |
26 |
| -
|
27 |
| -Begin!`; |
| 20 | + Instructions: |
| 21 | + 1. Analyze the provided webpage text and identify the main topics, key points and overall meaning. Take account of the language of the webpage. The ISO code of the language should be detected and if it is not it will be '${language}'. |
| 22 | + 2. Generate an abstract in the SAME LANGUAGE as the webpage content. This is crucial. If the webpage is in Spanish, the abstract MUST be in Spanish. If the webpage is in French, the abstract MUST be in French, and so on. |
| 23 | + 3. The abstract should: |
| 24 | + - Accurately and concisely summarize the key information in 1-2 paragraphs |
| 25 | + - Be well-written, precise and easy to understand |
| 26 | + - Contain the most important points without extraneous details |
| 27 | + - Be formatted as 1-2 easily readable paragraphs of plain text, each formatted as <p class="abstractp">{paragraph text}</p> WITHOUT MARKDOWN OR SPECIAL CHARACTERS |
| 28 | + 4. Extract the most relevant keywords from the text that capture the main topics and themes. |
| 29 | + 5. Format the output as follows, including the abstract and keywords, the final output MUST BE a valid HTML node with NO MARKDOWN at all: |
| 30 | + <div class="abstract" lang="{ISO code of the detected language}">{abstract}</div> |
| 31 | + <div class="keywords">{foreach keyword in keywords: <span class="keyword">{keyword}</span> }</div> |
28 | 32 |
|
| 33 | + Begin!`; |
29 | 34 |
|
30 | 35 | // Set the options for the fetch call
|
31 |
| - const options = { |
| 36 | + let options = { |
32 | 37 | method: 'POST',
|
33 | 38 | headers: {
|
34 | 39 | accept: 'application/json',
|
35 | 40 | 'content-type': 'application/json',
|
36 | 41 | authorization: `Bearer ${apiKey}`
|
37 |
| - }, |
38 |
| - body: JSON.stringify({ |
39 |
| - model: model, |
40 |
| - messages: [ |
41 |
| - {role: 'system', content: systemPrompt}, |
42 |
| - {role: 'user', content: content} |
43 |
| - ], |
44 |
| - temperature: temperature |
45 |
| - }) |
| 42 | + } |
46 | 43 | };
|
47 | 44 |
|
48 |
| - return fetch('https://api.perplexity.ai/chat/completions', options) |
49 |
| - .then(response => response.json()) |
50 |
| - .catch(err => console.error(err)); |
| 45 | + let body = { |
| 46 | + model: model, |
| 47 | + messages: [ |
| 48 | + {role: 'system', content: systemPrompt}, |
| 49 | + {role: 'user', content: content} |
| 50 | + ], |
| 51 | + temperature: temperature |
| 52 | +}; |
| 53 | + |
| 54 | +if (topk !== null) body.top_k = topk; |
| 55 | +if (topp !== null) body.top_p = topp; |
| 56 | +if (frequencyPenalty !== null) body.frequency_penalty = frequencyPenalty; |
| 57 | +if (presencePenalty !== null) body.presence_penalty = presencePenalty; |
| 58 | +if (maxTokens !== null) body.max_tokens = maxTokens; |
| 59 | + |
| 60 | +options.body = JSON.stringify(body); |
| 61 | + |
| 62 | + |
| 63 | + return fetch(API_ENDPOINT, options) |
| 64 | + .then(response => response.json()) |
| 65 | + .catch(err => console.error(err)); |
51 | 66 | }
|
52 | 67 |
|
53 | 68 | // Listen for messages from the popup
|
54 | 69 | browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
55 | 70 | if (message.type === 'CALL_API') {
|
56 |
| - callPerplexityAPI(message.apiKey, message.model, message.temperature, message.content).then(response => { |
| 71 | + callPerplexityAPI(message.apiKey, message.model, message.temperature, message.topk, message.topp, message.frequencyPenalty, message.presencePenalty, message.maxTokens, message.content).then(response => { |
57 | 72 | sendResponse({data: response});
|
58 | 73 | });
|
59 | 74 | return true; // Return true to indicate async response
|
60 | 75 | }
|
61 | 76 | });
|
| 77 | + |
0 commit comments