feat: images using object service storage

This commit is contained in:
Hk-Gosuto
2023-12-10 21:23:40 +08:00
parent b84da5e120
commit 600a7d2197
6 changed files with 142 additions and 28 deletions

View File

@@ -74,7 +74,8 @@ export class ChatGPTApi implements LLMApi {
}
async chat(options: ChatOptions) {
const messages = options.messages.map((v) => {
const messages: any[] = [];
for (const v of options.messages) {
let message: {
role: string;
content: { type: string; text?: string; image_url?: { url: string } }[];
@@ -87,15 +88,25 @@ export class ChatGPTApi implements LLMApi {
text: v.content,
});
if (v.image_url) {
message.content.push({
type: "image_url",
image_url: {
url: v.image_url,
},
});
await fetch(v.image_url)
.then((response) => response.arrayBuffer())
.then((buffer) => {
const base64Data = btoa(
String.fromCharCode(...new Uint8Array(buffer)),
);
message.content.push({
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${base64Data}`,
},
});
})
.catch((error) => {
console.error(error);
});
}
return message;
});
messages.push(message);
}
const modelConfig = {
...useAppConfig.getState().modelConfig,
@@ -104,7 +115,6 @@ export class ChatGPTApi implements LLMApi {
model: options.config.model,
},
};
const requestPayload = {
messages,
stream: options.config.stream,
@@ -177,7 +187,6 @@ export class ChatGPTApi implements LLMApi {
};
controller.signal.onabort = finish;
fetchEventSource(chatPath, {
...chatPayload,
async onopen(res) {

View File

@@ -0,0 +1,19 @@
import { getAuthHeaders } from "../api";
export class FileApi {
async upload(file: any): Promise<void> {
const formData = new FormData();
formData.append("file", file);
var headers = getAuthHeaders();
var res = await fetch("/api/file/upload", {
method: "POST",
body: formData,
headers: {
...headers,
},
});
const resJson = await res.json();
console.log(resJson);
return resJson.fileName;
}
}