-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
49 lines (47 loc) · 1.3 KB
/
api.js
File metadata and controls
49 lines (47 loc) · 1.3 KB
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
48
49
export const providerConfigs = {
openai: {
url: 'https://api.openai.com/v1/chat/completions',
errorPrefix: 'OpenAI API error:'
},
openrouter: {
url: 'https://openrouter.ai/api/v1/chat/completions',
errorPrefix: 'OpenRouter API error:'
}
};
export async function getSettings() {
return await chrome.storage.sync.get([
'sidepanelProvider',
'openaiApiKey',
'sidepanelModel',
'sidepanelOpenrouterApiKey',
'sidepanelOpenrouterModel'
]);
}
export async function callChatAPI(provider, apiKey, model, prompt) {
let config;
if (provider === 'openai') {
config = providerConfigs.openai;
} else if (provider === 'openrouter') {
config = providerConfigs.openrouter;
} else {
throw new Error(`Unsupported provider: ${provider}`);
}
const response = await fetch(config.url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
messages: [{ role: 'user', content: prompt }],
temperature: 0.2
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`${config.errorPrefix} ${response.status} ${errorText}`);
}
const data = await response.json();
return data.choices[0].message.content.trim();
}