generated from pot-app/pot-app-recognize-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
71 lines (67 loc) · 2.04 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
async function recognize(base64, lang, options) {
const { config, utils } = options;
const { tauriFetch: fetch } = utils;
let { model = "gpt-4o", apiKey, requestPath, customPrompt } = config;
if (!requestPath) {
requestPath = "https://api.openai.com";
}
if (!/https?:\/\/.+/.test(requestPath)) {
requestPath = `https://${requestPath}`;
}
if (requestPath.endsWith('/')) {
requestPath = requestPath.slice(0, -1);
}
if (!requestPath.endsWith('/chat/completions')) {
requestPath += '/v1/chat/completions';
}
if (!customPrompt) {
customPrompt = "Just recognize the text in the image. Do not offer unnecessary explanations.";
}else{
customPrompt = customPrompt.replaceAll("$lang", lang);
}
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
const body = {
model,
messages: [
{
"role": "system",
"content": [
{
"type": "text",
"text": customPrompt
}
],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": `data:image/png;base64,${base64}`,
"detail": "high"
},
},
],
}
],
}
let res = await fetch(requestPath, {
method: 'POST',
url: requestPath,
headers: headers,
body: {
type: "Json",
payload: body
}
});
if (res.ok) {
let result = res.data;
return result.choices[0].message.content;
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}