-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
93 lines (76 loc) · 1.97 KB
/
Copy pathindex.ts
File metadata and controls
93 lines (76 loc) · 1.97 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import type { AdapterOptions } from "./types.js";
import type { ImageVisionAdapter } from "adminforth";
export default class ImageVisionAdapterOpenAI implements ImageVisionAdapter {
options: AdapterOptions;
constructor(options: AdapterOptions) {
this.options = options;
this.options.model = options.model || 'gpt-4.1-mini';
}
validate() {
if (!this.options.openAiApiKey) {
throw new Error("API Key is required");
}
}
inputFileExtensionSupported(): string[] {
return ['png', 'jpeg', 'jpg', 'webp', 'gif'];
}
async generate(params: {
prompt: string,
inputFileUrls: string[],
}): Promise<{
response: string;
error?: string;
}> {
return await this.scanImage({ prompt: params.prompt, inputFileUrls: params.inputFileUrls });
}
private async scanImage({
prompt,
inputFileUrls,
}: {
prompt: string,
inputFileUrls: string[],
}): Promise<{
response: string;
error?: string;
}> {
const headers = {
Authorization: `Bearer ${this.options.openAiApiKey}`,
'Content-Type': 'application/json',
};
const content = [
{ type: "input_text", text: `${prompt}` },
...inputFileUrls.map(url => ({
type: "input_image",
image_url: url,
})),
];
process.env.HEAVY_DEBUG && console.log("Request body:", JSON.stringify({
model: this.options.model,
input: [
{
role: "user",
content: content,
}
]
}));
const resp = await fetch("https://api.openai.com/v1/responses", {
method: "POST",
headers: headers,
body: JSON.stringify({
model: this.options.model,
input: [
{
role: "user",
content: content,
}
]
})
});
const data = await resp.json();
process.env.HEAVY_DEBUG && console.log("Response:", data);
return {
response: data,
error: data.error
};
}
}