-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuggingface-api-fetch.js
30 lines (29 loc) · 1.07 KB
/
huggingface-api-fetch.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
// Listen for messages from the parent window
window.addEventListener('message', function(event) {
if (event.data.type === 'fetchRequest') {
const apiKey = event.data.apiKey;
const messages = event.data.messages;
fetch('https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
"model": "Qwen/Qwen2.5-72B-Instruct",
"messages": messages,
"max_tokens": 500,
"stream": false
})
})
.then(response => response.json())
.then(data => {
window.opener.postMessage({ type: 'chatResponse', data: data }, '*');
window.close();
})
.catch(error => {
window.opener.postMessage({ type: 'error', error: error.toString() }, '*');
window.close();
});
}
}, false);